Using SmartGWT EE 2.4. I want to load an Address for each User. Below is my DMI class and a junta test case. The test fails if the code is run as shown. It passes if the "resp.setData(dataList);" line is uncommented. Based on the quick start guide, though, it should pass even with the line commented out. The guide says on page 47 under "Adding DMI Business Logic"…
"For example, adding calculated values derived from DataSource data, or trimming data that the user is not allowed to see. Typically, use dsResponse.getRecords() and iterate over the returned records, adding or modifying properties. If records should be eliminated, pass the modified List of records to dsResponse.setData()."
Is there something missing in my code?
// ------ class under test ----------------------
public class UserDMI {
@SuppressWarnings({ "unchecked", "rawtypes" })
public DSResponse addAdressesToUsers(DSRequest dsRequest) throws Exception {
DSResponse resp = dsRequest.execute();
List<?> records = resp.getRecords();
for (Object obj : records) {
if (obj instanceof Map) {
DSRequest addrReq = new DSRequest();
addrReq.setDataSourceName("Address");
addrReq.setOperationType("fetch");
final Map record = (Map) obj;
Object addrId = record.get("address_id");
if (addrId != null) {
final HashMap crit = new HashMap();
crit.put("id", addrId);
addrReq.setCriteria(crit);
DSResponse r = addrReq.execute();
record.put("address", r.getDataMap());
}
}
}
// JUnit test passes only if the next line is un-commented.
// resp.setData(dataList);
return resp;
}
}
// ------ test ----------------------------------
public class UserDMITest {
@Test
public void addAdressesToUsers() throws Exception {
ISCInit.go();
UserDMI unit = new UserDMI();
DSRequest dsRequest = new DSRequest();
dsRequest.setDataSourceName("User");
dsRequest.setOperationType("fetch");
DSResponse resp = unit.addAdressesToUsers(dsRequest);
assertNotNull(resp);
final List<?> data = resp.getDataList();
assertNotNull(data);
for (Object o : data) {
assertTrue(o instanceof Map);
final Map m = (Map)o;
if (m.get("address_id") != null) {
assertTrue("User " + m.get("id") + " should have a fullAddress based on address id = "+
m.get("address_id") +".",
m.containsKey("address"));
final Map addrMap = (Map) m.get("address");
if (m.get("address_id") != null) assertNotNull(addrMap.get("city"));
} else {
assertFalse("Record " + m + " should not have a fullAddress.", m.containsKey("address"));
}
}
}
}
,boz
ps: thanks for this thread that helped me get my tests going: http://forums.smartclient.com/showthread.php?t=15058
"For example, adding calculated values derived from DataSource data, or trimming data that the user is not allowed to see. Typically, use dsResponse.getRecords() and iterate over the returned records, adding or modifying properties. If records should be eliminated, pass the modified List of records to dsResponse.setData()."
Is there something missing in my code?
// ------ class under test ----------------------
public class UserDMI {
@SuppressWarnings({ "unchecked", "rawtypes" })
public DSResponse addAdressesToUsers(DSRequest dsRequest) throws Exception {
DSResponse resp = dsRequest.execute();
List<?> records = resp.getRecords();
for (Object obj : records) {
if (obj instanceof Map) {
DSRequest addrReq = new DSRequest();
addrReq.setDataSourceName("Address");
addrReq.setOperationType("fetch");
final Map record = (Map) obj;
Object addrId = record.get("address_id");
if (addrId != null) {
final HashMap crit = new HashMap();
crit.put("id", addrId);
addrReq.setCriteria(crit);
DSResponse r = addrReq.execute();
record.put("address", r.getDataMap());
}
}
}
// JUnit test passes only if the next line is un-commented.
// resp.setData(dataList);
return resp;
}
}
// ------ test ----------------------------------
public class UserDMITest {
@Test
public void addAdressesToUsers() throws Exception {
ISCInit.go();
UserDMI unit = new UserDMI();
DSRequest dsRequest = new DSRequest();
dsRequest.setDataSourceName("User");
dsRequest.setOperationType("fetch");
DSResponse resp = unit.addAdressesToUsers(dsRequest);
assertNotNull(resp);
final List<?> data = resp.getDataList();
assertNotNull(data);
for (Object o : data) {
assertTrue(o instanceof Map);
final Map m = (Map)o;
if (m.get("address_id") != null) {
assertTrue("User " + m.get("id") + " should have a fullAddress based on address id = "+
m.get("address_id") +".",
m.containsKey("address"));
final Map addrMap = (Map) m.get("address");
if (m.get("address_id") != null) assertNotNull(addrMap.get("city"));
} else {
assertFalse("Record " + m + " should not have a fullAddress.", m.containsKey("address"));
}
}
}
}
,boz
ps: thanks for this thread that helped me get my tests going: http://forums.smartclient.com/showthread.php?t=15058
Comment