use of org.motechproject.mds.service.MotechDataService in project motech by motech.
the class InstanceServiceTest method shouldCreateInstanceOfSubclassedEntityWithRelation.
@Test
public void shouldCreateInstanceOfSubclassedEntityWithRelation() {
mockEntity(SubclassSample.class, ENTITY_ID, entity);
mockDataService(SubclassSample.class, motechDataService);
when(motechDataService.retrieve("id", INSTANCE_ID)).thenReturn(new SubclassSample());
when(entityService.getEntityFieldsForUI(ENTITY_ID)).thenReturn(asList(FieldTestHelper.fieldDto(1L, "superclassInteger", Integer.class.getName(), "Superclass Integer", 7), FieldTestHelper.fieldDto(2L, "subclassString", String.class.getName(), "Subclass String", "test"), FieldTestHelper.fieldDto(3L, "superclassRelation", TypeDto.ONE_TO_ONE_RELATIONSHIP.getTypeClass(), "Superclass Relationship", null)));
long relationEntityId = ANOTHER_ENTITY_ID;
long relationInstanceId = INSTANCE_ID + 1;
EntityDto relationEntity = mock(EntityDto.class);
MotechDataService relationDataService = mock(MotechDataService.class);
mockEntity(TestSample.class, relationEntityId, relationEntity);
mockDataService(TestSample.class, relationDataService);
TestSample relatedInstance = new TestSample("test sample", 42);
when(relationDataService.retrieve("id", relationInstanceId)).thenReturn(relatedInstance);
when(relationDataService.findById(relationInstanceId)).thenReturn(relatedInstance);
RelationshipsUpdate relationshipsUpdate = new RelationshipsUpdate();
relationshipsUpdate.getAddedIds().add(relationInstanceId);
EntityRecord createRecord = new EntityRecord(null, ENTITY_ID, asList(FieldTestHelper.fieldRecord("superclassInteger", Integer.class.getName(), "", 77), FieldTestHelper.fieldRecord("subclassString", String.class.getName(), "", "test test"), FieldTestHelper.fieldRecord(TypeDto.ONE_TO_ONE_RELATIONSHIP, "superclassRelation", "", relationshipsUpdate)));
ArgumentCaptor<SubclassSample> createCaptor = ArgumentCaptor.forClass(SubclassSample.class);
instanceService.saveInstance(createRecord);
verify(motechDataService).create(createCaptor.capture());
SubclassSample instance = createCaptor.getValue();
assertEquals(77, (int) instance.getSuperclassInteger());
assertEquals("test test", instance.getSubclassString());
assertNotNull(instance.getSuperclassRelation());
assertEquals(relatedInstance.getStrField(), instance.getSuperclassRelation().getStrField());
assertEquals(relatedInstance.getIntField(), instance.getSuperclassRelation().getIntField());
}
use of org.motechproject.mds.service.MotechDataService in project motech by motech.
the class MdsRestBundleIT method testLookups.
@Test
public void testLookups() throws Exception {
getLogger().info("Testing lookups via REST");
final MotechDataService dataService = getDataService();
// create some records
// make sure to use spaces
List<String> recordJsons = new ArrayList<>();
recordJsons.add(recordJsonString("myStr 1", 5));
recordJsons.add(recordJsonString("myStr 2", 10));
recordJsons.add(recordJsonString("myStr 3", 10));
recordJsons.add(recordJsonString("myStr 4", 15));
recordJsons.add(recordJsonString("myStr 5", 15));
for (String json : recordJsons) {
HttpPost post = new HttpPost(ENTITY_URL);
post.setEntity(new StringEntity(json, ContentType.APPLICATION_JSON));
HttpResponse response = getHttpClient().execute(post);
assertNotNull(response);
assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
}
// test single return lookup
verifySingleLookup("myStr 1", 5);
verifySingleLookup("myStr 2", 10);
verifySingleLookup("myStr 3", 10);
verifySingleLookup("myStr 4", 15);
verifySingleLookup("myStr 5", 15);
// test multiple return lookup
verifyMultiLookup(5, "myStr 1");
verifyMultiLookup(10, "myStr 2", "myStr 3");
verifyMultiLookup(15, "myStr 4", "myStr 5");
}
Aggregations