Search in sources :

Example 51 with MotechDataService

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());
}
Also used : BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) EntityRecord(org.motechproject.mds.web.domain.EntityRecord) EntityDto(org.motechproject.mds.dto.EntityDto) RelationshipsUpdate(org.motechproject.mds.web.domain.RelationshipsUpdate) Matchers.anyString(org.mockito.Matchers.anyString) DefaultMotechDataService(org.motechproject.mds.service.DefaultMotechDataService) MotechDataService(org.motechproject.mds.service.MotechDataService) Test(org.junit.Test)

Example 52 with MotechDataService

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");
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) MotechDataService(org.motechproject.mds.service.MotechDataService) Test(org.junit.Test)

Aggregations

MotechDataService (org.motechproject.mds.service.MotechDataService)52 EntityDto (org.motechproject.mds.dto.EntityDto)26 FieldDto (org.motechproject.mds.dto.FieldDto)14 ArrayList (java.util.ArrayList)13 List (java.util.List)9 Test (org.junit.Test)8 Entity (org.motechproject.mds.domain.Entity)4 LookupDto (org.motechproject.mds.dto.LookupDto)4 ObjectNotFoundException (org.motechproject.mds.exception.object.ObjectNotFoundException)4 LookupExecutor (org.motechproject.mds.lookup.LookupExecutor)4 BasicEntityRecord (org.motechproject.mds.web.domain.BasicEntityRecord)4 Bundle (org.osgi.framework.Bundle)4 InvocationTargetException (java.lang.reflect.InvocationTargetException)3 Collection (java.util.Collection)3 LookupExecutionException (org.motechproject.mds.exception.lookup.LookupExecutionException)3 ObjectReadException (org.motechproject.mds.exception.object.ObjectReadException)3 DefaultMotechDataService (org.motechproject.mds.service.DefaultMotechDataService)3 EntityRecord (org.motechproject.mds.web.domain.EntityRecord)3 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2