Search in sources :

Example 21 with EntityRecord

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

the class InstanceServiceTest method shouldThrowExceptionWhileSavingInstanceInNonEditableEntity.

@Test(expected = EntityInstancesNonEditableException.class)
public void shouldThrowExceptionWhileSavingInstanceInNonEditableEntity() {
    EntityDto nonEditableEntity = new EntityDto();
    nonEditableEntity.setNonEditable(true);
    nonEditableEntity.setId(ANOTHER_ENTITY_ID);
    EntityRecord entityRecord = new EntityRecord(null, ANOTHER_ENTITY_ID, new ArrayList<FieldRecord>());
    when(entityService.getEntity(ANOTHER_ENTITY_ID)).thenReturn(nonEditableEntity);
    instanceService.saveInstance(entityRecord);
}
Also used : BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) EntityRecord(org.motechproject.mds.web.domain.EntityRecord) EntityDto(org.motechproject.mds.dto.EntityDto) FieldRecord(org.motechproject.mds.web.domain.FieldRecord) BasicFieldRecord(org.motechproject.mds.web.domain.BasicFieldRecord) Test(org.junit.Test)

Example 22 with EntityRecord

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

the class InstanceServiceTest method shouldThrowExceptionWhileSavingInstanceWithReadOnlyPermission.

@Test(expected = SecurityException.class)
public void shouldThrowExceptionWhileSavingInstanceWithReadOnlyPermission() {
    EntityDto entityDto = new EntityDto();
    entityDto.setReadOnlySecurityMode(SecurityMode.EVERYONE);
    entityDto.setSecurityMode(SecurityMode.NO_ACCESS);
    EntityRecord entityRecord = new EntityRecord(null, ANOTHER_ENTITY_ID, new ArrayList<FieldRecord>());
    when(entityService.getEntity(ANOTHER_ENTITY_ID)).thenReturn(entityDto);
    instanceService.saveInstance(entityRecord);
}
Also used : BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) EntityRecord(org.motechproject.mds.web.domain.EntityRecord) EntityDto(org.motechproject.mds.dto.EntityDto) FieldRecord(org.motechproject.mds.web.domain.FieldRecord) BasicFieldRecord(org.motechproject.mds.web.domain.BasicFieldRecord) Test(org.junit.Test)

Example 23 with EntityRecord

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

the class InstanceServiceTest method shouldAcceptUserWithNoPermissionWhileReadingInstanceWithNoSecurityMode.

@Test
public void shouldAcceptUserWithNoPermissionWhileReadingInstanceWithNoSecurityMode() {
    EntityDto entityDto = new EntityDto();
    entityDto.setReadOnlySecurityMode(null);
    entityDto.setSecurityMode(null);
    entityDto.setClassName(TestSample.class.getName());
    EntityRecord entityRecord = new EntityRecord(ANOTHER_ENTITY_ID, null, new ArrayList<>());
    when(entityService.getEntity(ANOTHER_ENTITY_ID)).thenReturn(entityDto);
    mockDataService();
    instanceService.getEntityRecords(entityRecord.getId());
    verify(entityService).getEntityFieldsForUI(ANOTHER_ENTITY_ID);
}
Also used : BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) EntityRecord(org.motechproject.mds.web.domain.EntityRecord) EntityDto(org.motechproject.mds.dto.EntityDto) Test(org.junit.Test)

Example 24 with EntityRecord

use of org.motechproject.mds.web.domain.EntityRecord 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 25 with EntityRecord

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

the class InstanceServiceTest method testUpdateCreate.

private void testUpdateCreate(boolean edit) throws ClassNotFoundException {
    final DateTime dtValue = DateUtil.now();
    mockSampleFields();
    mockDataService();
    mockEntity();
    when(motechDataService.retrieve("id", INSTANCE_ID)).thenReturn(new TestSample());
    List<FieldRecord> fieldRecords = asList(FieldTestHelper.fieldRecord("strField", String.class.getName(), "", "this is a test"), FieldTestHelper.fieldRecord("intField", Integer.class.getName(), "", 16), FieldTestHelper.fieldRecord("timeField", Time.class.getName(), "", "10:17"), FieldTestHelper.fieldRecord("dtField", DateTime.class.getName(), "", dtValue));
    Long id = (edit) ? INSTANCE_ID : null;
    EntityRecord record = new EntityRecord(id, ENTITY_ID, fieldRecords);
    MDSClassLoader.getInstance().loadClass(TestSample.class.getName());
    instanceService.saveInstance(record);
    ArgumentCaptor<TestSample> captor = ArgumentCaptor.forClass(TestSample.class);
    if (edit) {
        verify(motechDataService).update(captor.capture());
    } else {
        verify(motechDataService).create(captor.capture());
    }
    TestSample sample = captor.getValue();
    assertEquals("this is a test", sample.getStrField());
    assertEquals(Integer.valueOf(16), sample.getIntField());
    assertEquals(new Time(10, 17), sample.getTimeField());
    assertEquals(dtValue, sample.getDtField());
}
Also used : BasicEntityRecord(org.motechproject.mds.web.domain.BasicEntityRecord) EntityRecord(org.motechproject.mds.web.domain.EntityRecord) Matchers.anyLong(org.mockito.Matchers.anyLong) FieldRecord(org.motechproject.mds.web.domain.FieldRecord) BasicFieldRecord(org.motechproject.mds.web.domain.BasicFieldRecord) Time(org.motechproject.commons.date.model.Time) DateTime(org.joda.time.DateTime) DateTime(org.joda.time.DateTime)

Aggregations

BasicEntityRecord (org.motechproject.mds.web.domain.BasicEntityRecord)27 EntityRecord (org.motechproject.mds.web.domain.EntityRecord)27 Test (org.junit.Test)16 BasicFieldRecord (org.motechproject.mds.web.domain.BasicFieldRecord)15 FieldRecord (org.motechproject.mds.web.domain.FieldRecord)15 EntityDto (org.motechproject.mds.dto.EntityDto)11 ArrayList (java.util.ArrayList)5 FieldDto (org.motechproject.mds.dto.FieldDto)5 Matchers.anyString (org.mockito.Matchers.anyString)4 RequestMapping (org.springframework.web.bind.annotation.RequestMapping)4 List (java.util.List)3 MotechDataService (org.motechproject.mds.service.MotechDataService)3 RelationshipsUpdate (org.motechproject.mds.web.domain.RelationshipsUpdate)3 ResponseBody (org.springframework.web.bind.annotation.ResponseBody)3 InvocationTargetException (java.lang.reflect.InvocationTargetException)2 Collection (java.util.Collection)2 Predicate (java.util.function.Predicate)2 CannotCompileException (javassist.CannotCompileException)2 FieldNotFoundException (org.motechproject.mds.exception.field.FieldNotFoundException)2 ObjectNotFoundException (org.motechproject.mds.exception.object.ObjectNotFoundException)2