I am trying to post form data as
"student.id=1&courses[10].course.id=10"
The command object classes we are trying to bind are as below ...
class StudentEnrollmentCmd {
Student student
Map<String,CourseCmd> courses;
}
class CourseCmd {
CourseDomain course
}
class CourseDomain {
Long id
}
was hoping that it will bind to
"StudentEnrollmentCmd -> courses -> course.id"
which seems to work in grails 2.2.4 but fails in 3.3.7 with the following exception
No such property: course.id for class: student.CourseCmd
Here is the test case which illustrates the problem
void 'databinding from request parameters'() {
given:
// request with simple formdata: student.id=1&courses[10].course.id=10
MockHttpServletRequest request = buildMockRequestWithParams('POST',['student.id':'1','courses[10].course.id':'10']);
DataBindingSource source = bindingSourceCreator.createDataBindingSource(null,null,request);
// databinder & command object
def binder = new SimpleDataBinder()
def obj = new StudentEnrollmentCmd()
when:
binder.bind(obj,source)
then:
// this should not throw an exception, but throws an exception
MissingPropertyException ex = thrown()
System.out.println ( "Exception again:" + ex.message );
// the following should work, but does not work
obj.student.id == 1
obj.courses['10'].course.id == 10
}
Here is the link to full spec... https://github.com/swzaidi/sample/blob/master/grails3.3.7/src/test/groovy/student/DataBindingSpec.groovy
Looking for some help on how to pass the form data so that it binds to above command objects properly.
Related
I have created a generic inquiry in Acumatica that returns work orders that are open. We know that there is a print action that can be applied however, we want to print out multiple instances of the work orders. So we do not have to print them individually. Is there currently a solution for mass printing or can someone point me in the right direction of maybe a work around? I know there is a mass action print all function that can be found however, it does not do anything.
Just like in any other action redirecting users to the generated report(s), the Service Orders' Print Service Order button throws a PXReportRequiredException to open generated report in new window:
public class ServiceOrderEntry : PXGraph<ServiceOrderEntry, FSServiceOrder>
{
...
public PXAction<FSServiceOrder> printServiceOrder;
[PXUIField]
[PXButton]
public virtual void PrintServiceOrder()
{
if (this.IsDirty) Actions.PressSave();
if (ServiceOrderRecords.Current == null) return;
Dictionary<string, string> serviceOrderParameters =
GetServiceOrderParameters(ServiceOrderRecords.Current, false);
if (serviceOrderParameters.Count != 0)
{
throw new PXReportRequiredException(serviceOrderParameters,
"SD641000", PXBaseRedirectException.WindowMode.NewWindow, string.Empty);
}
}
...
}
If you add Service Orders' Print Service Order action as a mass action to Generic Inquiry (as shown on the screenshots below), the GI mass action will generate and open Service Order report form only for the first selected Service Order. Processing of any consequent Service Order will not be possible due to the PXReportRequiredException thrown to show the report form generated for the first Service Order.
In order to merge Service Order report forms generated for several Service Orders into a single report, you should create a custom processing screen and use the AddSibling method on a PXReportRequiredException instance. The AddSibling method will append the reports generated for 2nd+ Service Order to the PXReportRequiredException instance initially created for the first processed Service Order. After all selected Service Orders has been processed, a single PXReportRequiredException will be thrown to redirect the user to Report Viewer displaying all generated reports at once.
public class PrintServiceOrderProcess : PXGraph<PrintServiceOrderProcess>
{
public PXCancel<FSServiceOrder> Cancel;
public PXProcessing<FSServiceOrder> ServiceOrderRecords;
public PrintServiceOrderProcess()
{
ServiceOrderRecords.SetProcessDelegate(list => PrintServiceOrders(list));
}
public static void PrintServiceOrders(IEnumerable<FSServiceOrder> list)
{
PXReportRequiredException ex = null;
foreach (var order in list)
{
Dictionary<string, string> parameters = new Dictionary<string, string>();
string srvOrdType = SharedFunctions
.GetFieldName<FSServiceOrder.srvOrdType>(true);
string refNbr = SharedFunctions
.GetFieldName<FSServiceOrder.refNbr>(true);
parameters[srvOrdType] = order.SrvOrdType;
parameters[refNbr] = order.RefNbr;
if (ex == null)
{
ex = new PXReportRequiredException(parameters, "SD641000", "SD641000");
}
else
{
ex.AddSibling("SD641000", parameters, false);
}
}
if (ex != null) throw ex;
}
}
In a Grails project I am looking at here, a filter puts a Domain object on the request...
class TokenFilters {
def filters = {
all( uri: '/hiphop/**' ) {
before = {
MyToken myToken = ...
request.myToken = myToken
MyToken looks like:
class MyToken {
String id
String token
static mapping = {
token( index: true )
id( generator: 'uuid' )
}
...
}
In my controller, the myToken is pulled off the request.
MyController {
myaction {
MyToken accessToken = request.myToken
All fine. I wish to write an integration test for the controller.
#Test
void testLogin() {
def mc = new MyController()
def myToken = new MyToken(1234);
// set the request parameters
mc.request.parameters = [myToken:myToken];
def message = mc.action();
assertTrue(message.indexOf("trans") > 0)
}
When I run this, I get:
Failure: testLogin(MyTests)
| java.lang.IllegalArgumentException: Parameter map value must be single value or array of type [java.lang.String]
at testLogin(MyTests.groovy:40)
So it looks like Grails will only let me a String or a single value and doesn't like me putting an object on the request in the Filter. Even thou it lets me put on the same object type in a Filter.
I'd really like to test this without going to Functional tests. Please help. I am using Grails 2.2.1
Thanks
The problem is that your code is passing parameters to the controller. Your emulating an HTTP request which can't handle objects. What you can do is:
mc.request.parameters = [myToken: '1234']
and then you're controller/filter would pull out the 1234 and look up MyToken. If you were testing the controller forwarding then you can put objects in the request. Not the other way around.
I see now that part of the problem is that you're trying to test a controller that is assuming data coming from a filter.
You've omitted some code, but assuming you are extending ControllerUnitTestCase then you have access to a mock request object. You should be able to simply do:
#Test
void testLogin() {
def mc = new MyController()
def myToken = new MyToken(1234);
// set the request parameters
request.myToken = myToken
def message = mc.action();
assertTrue(message.indexOf("trans") > 0)
}
My Integration-Test for my grails application is returning a null object when I try to get a domain object using grails dynamic get method.
This is a simplified example of my problem. Lets say I have a controller TrackerLogController that uses a service TrackerLogService to save an updated Log domain for another Tracker domain.
Domain Tracker:
class Tracker {
int id
String name
static hasMany = [logs: Log]
}
Domain Log:
class Log {
int id
String comment
static belongsTo = [tracker: Tracker]
}
Controller TrackerLogController save:
def TrackerLogService
def saveTrackerLog() {
def trackerId = params.trackerId
def trackerInstance = Tracker.get(trackerId)
Log log = TrackerLogService.saveTrackerLogs(trackerInstance, params.comment)
if( log.hasErrors() ){
//render error page
}
//render good page
}
Service TrackerLogService save:
Log saveTrackerLogs( Tracker tracker, String comment) {
Log log = new Log(tracker: tracker, comment: comment)
log.save()
return log
}
So now I want to write an Integration-Test for this service but I'm not sure if I should be writing one just for the simple logic in the controller (if error, error page else good page) I would think I would write a Unit test for that, and an Integration-Test to check the persistence in the Database.
This is what I have for my Integration-Test:
class TrackerLogServiceTests {
def trackerLogService
#Before
void setUp(){
def tracker = new Tracker(id: 123, name: "First")
tracker.save()
//Now even if I call Tracker.get(123) it will return a null value...
}
#Test
void testTrackerLogService() {
Tacker trackerInstance = Tracker.get(123) //I have tried findById as well
String commit = "This is a commit"
//call the service
Log log = trackerLogService.saveTrackerLogs(trackerInstance , commit)
//want to make sure I added the log to the tracker Instance
assertEquals log , trackerInstance.logs.findByCommit(commit)
}
}
So for this example my trackerInstance would be a null object. I know the Grails magic doesn't seem to work for Unit tests without Mocking, I thought for Intigration-Tests for persistence in the DB you would be able to use that grails magic.
You can't specify the id value unless you declare that it's "assigned". As it is now it's using an auto-increment, so your 123 value isn't used. It's actually ignored by the map constructor for security reasons, so you'd need to do this:
def tracker = new Tracker(name: "First")
tracker.id = 123
but then it would get overwritten by the auto-increment lookup. Use this approach instead:
class TrackerLogServiceTests {
def trackerLogService
private trackerId
#Before
void setUp(){
def tracker = new Tracker(name: "First")
tracker.save()
trackerId = tracker.id
}
#Test
void testTrackerLogService() {
Tacker trackerInstance = Tracker.get(trackerId)
String commit = "This is a commit"
//call the service
Log log = trackerLogService.saveTrackerLogs(trackerInstance , commit)
//want to make sure I added the log to the tracker Instance
assertEquals log , trackerInstance.logs.findByCommit(commit)
}
}
Also, unrelated - don't declare the id field unless it's a nonstandard type, e.g. a String. Grails adds that for you, along with the version field. All you need is
class Tracker {
String name
static hasMany = [logs: Log]
}
and
class Log {
String comment
static belongsTo = [tracker: Tracker]
}
I have a grails application that has a service that creates reports. The report is defined as:
class Report {
Date createDate
String reportType
List contents
static constraints = {
}
}
The service generates a report and populates contents as a list that is returned by createCriteria.
My problem is that my service claims to be saving the Report, no errors turn up, logging says that its all there, but when I go to call show from the controller on that report, it says contents is null.
Another relevant bit, my Service is called by an ActiveMQ message queue. The message originating from my report controller.
Controller:
class ReportController {
def scaffold = Report
def show = {
def rep = Report.get(params.id)
log.info("Report is " + (rep? "not null" : "null")) //says report is not null
log.info("Report content is " + (rep.contents? "not null" : "null")) //always says report.contents is null.
redirect(action: rep.reportType, model: [results: rep.contents, resultsTotal: rep.contents.size()])
}
}
My service that creates the report:
class ReportService {
static transactional = false
static expose = ['jms']
static destination = "Report"
void onMessage(msg)
{
this."$msg.reportType"(msg)
}
void totalQuery(msg)
{
def results = Result.createCriteria().list {
//This returns exactly what i need.
}
Report.withTransaction() {
def rep = new Report(createDate: new Date(), reportType: "totalQuery", contents: results)
log.info("Validation results: ${rep.validate()}")
if( !rep.save(flush: true) ) {
rep.errors.each {
log.error(it)
}
}
}
}
Is there something obvious that I'm missing here? My thought is that since all my unit tests work, that the hibernate context is not being passed through the message queue. But that would generate Exceptions wouldn't it? I've been beating my head on this problem for days, so a point in the right direction would be great.
Thanks,
You can't define an arbitrary List like that, so it's getting ignored and treated as transient. You'd get the same behavior if you had a def name field, since in both cases Hibernate doesn't know the data type, so it has no idea how to map it to the database.
If you want to refer to a collection of Results, then you need a hasMany:
class Report {
Date createDate
String reportType
static hasMany = [contents: Result]
}
If you need the ordered list, then also add in a List field with the same name, and instead of creating a Set (the default), it will be a List:
class Report {
Date createDate
String reportType
List contents
static hasMany = [contents: Result]
}
Your unit tests work because you're not accessing a database or using Hibernate. I think it's best to always integration test domain classes so you at least use the in-memory database, and mock the domain classes when testing controllers, services, etc.
I am using MvcContrib and the TestControllerBuilder to write tests for my controller.
I am writing the tests for my Error Handling Controller that looks like this:
public JsonResult HttpError()
{
Exception ex = null;
try
{
ex = (Exception)HttpContext.Application[Request.UserHostAddress.ToString()];
}
catch
{
}
if( ex != null )
{
return Json( new ErrorViewModel() { Message = ex.Message, Source = ex.Source, StackTrace = ex.StackTrace, Type = ex.GetType().Name}, JsonRequestBehavior.AllowGet );
}
else
{
return Json( new ErrorViewModel() { Message = "An error has occured." }, JsonRequestBehavior.AllowGet );
}
}
Basically, my global error handling puts the last exception into the Application store and this controller tries to pull it back out, convert it to Json, and return it (we are returning everything as Json because these methods are only getting called as Web Services).
To fully test this, I need for UserHostAddress to contain something predictable, but the objects setup by TestControllerBuilder leave that property null.
How can I make this work? I'm not sure how I can make this work in my test.
TestControllerBuilder uses Rhino.Mocks for mocking the HttpContext. Knowing this, you could put the Request object back into "record" mode and stub out a response:
controller.Request.BackToRecord();
controller.Request.Stub(r => r.UserHostAddress).Return("75.142.12.45");
controller.Request.Replay();
Do this after you've initialized the controller, but before your method call.