Search in sources :

Example 1 with RelationshipsUpdate

use of org.motechproject.mds.web.domain.RelationshipsUpdate in project motech by motech.

the class InstanceServiceTest method buildRelationshipUpdate.

private RelationshipsUpdate buildRelationshipUpdate() {
    EntityRecord relatedRecord = new EntityRecord(null, 1L, new ArrayList<>());
    RelationshipsUpdate relationshipsUpdate = new RelationshipsUpdate();
    relationshipsUpdate.getAddedIds().add(4L);
    relationshipsUpdate.getAddedIds().add(5L);
    relationshipsUpdate.getAddedNewRecords().add(relatedRecord);
    return relationshipsUpdate;
}
Also used : BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) EntityRecord(org.motechproject.mds.web.domain.EntityRecord) RelationshipsUpdate(org.motechproject.mds.web.domain.RelationshipsUpdate)

Example 2 with RelationshipsUpdate

use of org.motechproject.mds.web.domain.RelationshipsUpdate in project motech by motech.

the class InstanceServiceTest method shouldReturnRelatedInstances.

@Test
public void shouldReturnRelatedInstances() {
    mockDataService();
    mockAnotherEntity();
    mockEntity();
    mockSampleFields();
    mockAnotherEntityFields();
    mockTestClassEntity();
    mockTestClassService();
    mockTestClassFields();
    when(serviceForAnotherSample.findById(INSTANCE_ID)).thenReturn(sampleForRelationshipTesting());
    QueryParams queryParams = new QueryParams(1, 2, new Order(Constants.Util.ID_FIELD_NAME, Order.Direction.ASC));
    Records<BasicEntityRecord> records = instanceService.getRelatedFieldValue(ANOTHER_ENTITY_ID, INSTANCE_ID, "testClasses", new RelationshipsUpdate(), queryParams);
    assertNotNull(records);
    // page 1
    assertEquals(Integer.valueOf(1), records.getPage());
    // 2 pages total
    assertEquals(Integer.valueOf(2), records.getTotal());
    // 3 records total
    assertEquals(Integer.valueOf(3), records.getRecords());
    assertEquals(asList(1L, 2L), extract(records.getRows(), on(BasicEntityRecord.class).getFieldByName("id").getValue()));
    RelationshipsUpdate filter = new RelationshipsUpdate();
    filter.setRemovedIds(Arrays.asList(1L, 2L));
    filter.setAddedIds(Arrays.asList(50L));
    when(testClassMotechDataService.findByIds(filter.getAddedIds())).thenReturn(Arrays.asList(new TestClass(50)));
    records = instanceService.getRelatedFieldValue(ANOTHER_ENTITY_ID, INSTANCE_ID, "testClasses", filter, queryParams);
    assertNotNull(records);
    // page 1
    assertEquals(Integer.valueOf(1), records.getPage());
    // 1 page total
    assertEquals(Integer.valueOf(1), records.getTotal());
    // 2 records total
    assertEquals(Integer.valueOf(2), records.getRecords());
    // 1L and 2L removed, 50L added
    assertEquals(asList(3L, 50L), extract(records.getRows(), on(BasicEntityRecord.class).getFieldByName("id").getValue()));
}
Also used : Order(org.motechproject.mds.util.Order) RelationshipsUpdate(org.motechproject.mds.web.domain.RelationshipsUpdate) QueryParams(org.motechproject.mds.query.QueryParams) BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) Test(org.junit.Test)

Example 3 with RelationshipsUpdate

use of org.motechproject.mds.web.domain.RelationshipsUpdate in project motech by motech.

the class InstanceServiceImpl method buildRelatedInstances.

private Object buildRelatedInstances(MotechDataService service, Class<?> parameterType, Class<?> argumentType, Object fieldValue, Object relatedObject) throws IllegalAccessException, InstantiationException, ClassNotFoundException, NoSuchMethodException, CannotCompileException, NoSuchFieldException {
    Object parsedValue;
    RelationshipsUpdate relationshipsUpdate = mapper.convertValue(fieldValue, RelationshipsUpdate.class);
    EntityDto relatedEntity = getEntity(argumentType.getName());
    List<FieldDto> entityFields = getEntityFields(relatedEntity.getId());
    if (Collection.class.isAssignableFrom(parameterType)) {
        parsedValue = parseRelationshipCollection(service, (Class<? extends Collection>) parameterType, argumentType, (Collection) relatedObject, relationshipsUpdate, entityFields);
    } else {
        parsedValue = parseRelationshipValue(service, argumentType, relationshipsUpdate, entityFields);
    }
    return parsedValue;
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) RelationshipsUpdate(org.motechproject.mds.web.domain.RelationshipsUpdate) Collection(java.util.Collection) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 4 with RelationshipsUpdate

use of org.motechproject.mds.web.domain.RelationshipsUpdate in project motech by motech.

the class InstanceServiceTest method shouldUpdateRelatedFields.

@Test
public void shouldUpdateRelatedFields() {
    TestSample test1 = new TestSample("someString", 4);
    TestSample test2 = new TestSample("otherString", 5);
    TestSample test3 = new TestSample("sample", 6);
    RelationshipsUpdate oneToOneUpdate = new RelationshipsUpdate();
    oneToOneUpdate.getAddedIds().add(6L);
    RelationshipsUpdate oneToManyUpdate = buildRelationshipUpdate();
    List<FieldRecord> fieldRecords = asList(FieldTestHelper.fieldRecord("title", String.class.getName(), "String field", "Default"), FieldTestHelper.fieldRecord(TypeDto.ONE_TO_MANY_RELATIONSHIP, "testSamples", "Related field", oneToManyUpdate), FieldTestHelper.fieldRecord(TypeDto.ONE_TO_ONE_RELATIONSHIP, "testSample", "Other Related field", oneToOneUpdate));
    EntityRecord entityRecord = new EntityRecord(null, ANOTHER_ENTITY_ID, fieldRecords);
    mockSampleFields();
    mockDataService();
    mockEntity();
    EntityDto entityWithRelatedField = mock(EntityDto.class);
    when(entityService.getEntity(ANOTHER_ENTITY_ID)).thenReturn(entityWithRelatedField);
    when(entityWithRelatedField.getClassName()).thenReturn(AnotherSample.class.getName());
    when(entityWithRelatedField.getId()).thenReturn(ENTITY_ID + 1);
    ServiceReference serviceReferenceForClassWithRelatedField = mock(ServiceReference.class);
    MotechDataService serviceForClassWithRelatedField = mock(MotechDataService.class);
    when(bundleContext.getServiceReference(ClassName.getInterfaceName(AnotherSample.class.getName()))).thenReturn(serviceReferenceForClassWithRelatedField);
    when(bundleContext.getService(serviceReferenceForClassWithRelatedField)).thenReturn(serviceForClassWithRelatedField);
    when(motechDataService.findById(4L)).thenReturn(test1);
    when(motechDataService.findById(5L)).thenReturn(test2);
    when(motechDataService.findById(6L)).thenReturn(test3);
    when(motechDataService.findByIds(oneToManyUpdate.getAddedIds())).thenReturn(Arrays.asList(test1, test2));
    when(entityService.getEntityFieldsForUI(ANOTHER_ENTITY_ID)).thenReturn(asList(FieldTestHelper.fieldDto(5L, "title", String.class.getName(), "String field", "Default"), FieldTestHelper.fieldDto(6L, "testSamples", TypeDto.ONE_TO_MANY_RELATIONSHIP.getTypeClass(), "Related field", null)));
    ArgumentCaptor<AnotherSample> captor = ArgumentCaptor.forClass(AnotherSample.class);
    instanceService.saveInstance(entityRecord, null);
    verify(serviceForClassWithRelatedField).create(captor.capture());
    AnotherSample capturedValue = captor.getValue();
    assertEquals(capturedValue.getTestSample(), test3);
    assertEquals(capturedValue.getTestSamples().size(), 3);
    assertEquals(capturedValue.getTitle(), "Default");
    assertTrue(capturedValue.getTestSamples().contains(test1));
    assertFalse(capturedValue.getTestSamples().contains(test3));
    assertTrue(capturedValue.getTestSamples().contains(test2));
}
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) FieldRecord(org.motechproject.mds.web.domain.FieldRecord) BasicFieldRecord(org.motechproject.mds.web.domain.BasicFieldRecord) Matchers.anyString(org.mockito.Matchers.anyString) DefaultMotechDataService(org.motechproject.mds.service.DefaultMotechDataService) MotechDataService(org.motechproject.mds.service.MotechDataService) ServiceReference(org.osgi.framework.ServiceReference) Test(org.junit.Test)

Example 5 with RelationshipsUpdate

use of org.motechproject.mds.web.domain.RelationshipsUpdate 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)

Aggregations

RelationshipsUpdate (org.motechproject.mds.web.domain.RelationshipsUpdate)6 BasicEntityRecord (org.motechproject.mds.web.domain.BasicEntityRecord)5 Test (org.junit.Test)3 EntityDto (org.motechproject.mds.dto.EntityDto)3 EntityRecord (org.motechproject.mds.web.domain.EntityRecord)3 Matchers.anyString (org.mockito.Matchers.anyString)2 QueryParams (org.motechproject.mds.query.QueryParams)2 DefaultMotechDataService (org.motechproject.mds.service.DefaultMotechDataService)2 MotechDataService (org.motechproject.mds.service.MotechDataService)2 Collection (java.util.Collection)1 FieldDto (org.motechproject.mds.dto.FieldDto)1 Order (org.motechproject.mds.util.Order)1 BasicFieldRecord (org.motechproject.mds.web.domain.BasicFieldRecord)1 FieldRecord (org.motechproject.mds.web.domain.FieldRecord)1 ServiceReference (org.osgi.framework.ServiceReference)1 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)1 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)1