@Test public void shouldUseCorrectClassLoaderWhenCreatingInstances() throws ClassNotFoundException { mockSampleFields(); mockEntity(); mockDataService(); Bundle ddeBundle = mock(Bundle.class); Bundle entitiesBundle = mock(Bundle.class); when(bundleContext.getBundles()).thenReturn(new Bundle[] {ddeBundle, entitiesBundle}); when(entity.getBundleSymbolicName()).thenReturn("org.motechproject.test"); when(entitiesBundle.getSymbolicName()) .thenReturn(Constants.BundleNames.MDS_ENTITIES_SYMBOLIC_NAME); when(ddeBundle.getSymbolicName()).thenReturn("org.motechproject.test"); Class testClass = TestSample.class; when(entitiesBundle.loadClass(testClass.getName())).thenReturn(testClass); when(ddeBundle.loadClass(testClass.getName())).thenReturn(testClass); EntityRecord entityRecord = new EntityRecord(null, ENTITY_ID, Collections.<FieldRecord>emptyList()); when(entity.isDDE()).thenReturn(true); instanceService.saveInstance(entityRecord); verify(ddeBundle).loadClass(TestSample.class.getName()); when(entity.isDDE()).thenReturn(false); instanceService.saveInstance(entityRecord); verify(entitiesBundle).loadClass(TestSample.class.getName()); verify(motechDataService, times(2)).create(any(TestSample.class)); }
@Test public void shouldCountForLookup() { mockSampleFields(); mockEntity(); mockLookups(); mockLookupService(); Map<String, Object> lookupMap = new HashMap<>(); lookupMap.put("strField", TestDataService.LOOKUP_1_EXPECTED_PARAM); long count = instanceService.countRecordsByLookup(ENTITY_ID, TestDataService.LOOKUP_1_NAME, lookupMap); assertEquals(1L, count); lookupMap.put("strField", TestDataService.LOOKUP_2_EXPECTED_PARAM); count = instanceService.countRecordsByLookup(ENTITY_ID, TestDataService.LOOKUP_2_NAME, lookupMap); assertEquals(22L, count); lookupMap.clear(); lookupMap.put("dtField", null); count = instanceService.countRecordsByLookup( ENTITY_ID, TestDataService.NULL_EXPECTING_LOOKUP_NAME, lookupMap); assertEquals(2, count); }
@Test public void shouldHandleNullParamsForLookups() { mockSampleFields(); mockEntity(); mockLookups(); mockLookupService(); Map<String, Object> lookupMap = new HashMap<>(); lookupMap.put("dtField", null); List<BasicEntityRecord> result = instanceService.getEntityRecordsFromLookup( ENTITY_ID, TestDataService.NULL_EXPECTING_LOOKUP_NAME, lookupMap, queryParams()); assertNotNull(result); assertEquals(2, result.size()); BasicEntityRecord entityRecord = result.get(0); List<? extends BasicFieldRecord> fieldRecords = entityRecord.getFields(); assertCommonBasicFieldRecordFields(fieldRecords); assertEquals( asList("three", 3, null, null, null), extract(fieldRecords, on(FieldRecord.class).getValue())); entityRecord = result.get(1); fieldRecords = entityRecord.getFields(); assertCommonBasicFieldRecordFields(fieldRecords); assertEquals( asList("four", 4, null, null, null), extract(fieldRecords, on(FieldRecord.class).getValue())); }
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()); }
@Test public void shouldRetrieveInstancesBasedOnAMultiReturnLookup() { mockSampleFields(); mockEntity(); mockLookups(); mockLookupService(); Map<String, Object> lookupMap = new HashMap<>(); lookupMap.put("strField", TestDataService.LOOKUP_2_EXPECTED_PARAM); List<BasicEntityRecord> result = instanceService.getEntityRecordsFromLookup( ENTITY_ID, TestDataService.LOOKUP_2_NAME, lookupMap, queryParams()); assertNotNull(result); assertEquals(2, result.size()); BasicEntityRecord entityRecord = result.get(0); List<? extends BasicFieldRecord> fieldRecords = entityRecord.getFields(); assertCommonBasicFieldRecordFields(fieldRecords); assertEquals( asList("one", 1, null, null, null), extract(fieldRecords, on(FieldRecord.class).getValue())); entityRecord = result.get(1); fieldRecords = entityRecord.getFields(); assertCommonBasicFieldRecordFields(fieldRecords); assertEquals( asList("two", 2, null, null, null), extract(fieldRecords, on(FieldRecord.class).getValue())); }
@Test(expected = ObjectNotFoundException.class) public void shouldThrowObjectNotFoundExceptionWhenNoInstanceFound() { mockDataService(); mockSampleFields(); mockEntity(); instanceService.getEntityInstance(ENTITY_ID, INSTANCE_ID); }
@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); assertEquals(Integer.valueOf(1), records.getPage()); // page 1 assertEquals(Integer.valueOf(2), records.getTotal()); // 2 pages total assertEquals(Integer.valueOf(3), records.getRecords()); // 3 records total 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); assertEquals(Integer.valueOf(1), records.getPage()); // page 1 assertEquals(Integer.valueOf(1), records.getTotal()); // 1 page total assertEquals(Integer.valueOf(2), records.getRecords()); // 2 records total // 1L and 2L removed, 50L added assertEquals( asList(3L, 50L), extract(records.getRows(), on(BasicEntityRecord.class).getFieldByName("id").getValue())); }
@Test(expected = EntityInstancesNonEditableException.class) public void shouldThrowExceptionWhileDeletingInstanceInNonEditableEntity() { EntityDto nonEditableEntity = new EntityDto(); nonEditableEntity.setNonEditable(true); when(entityService.getEntity(ANOTHER_ENTITY_ID)).thenReturn(nonEditableEntity); instanceService.deleteInstance(ANOTHER_ENTITY_ID, INSTANCE_ID); }
@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()); }
@Test public void shouldCountAllEntities() { mockSampleFields(); mockDataService(); mockEntity(); when(motechDataService.count()).thenReturn(56L); assertEquals(56L, instanceService.countRecords(ENTITY_ID)); }
@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); }
@Test(expected = SecurityException.class) public void shouldThrowExceptionWhileReadingInstanceWithoutAnyPermission() { EntityDto entityDto = new EntityDto(); entityDto.setReadOnlySecurityMode(SecurityMode.NO_ACCESS); entityDto.setSecurityMode(SecurityMode.NO_ACCESS); EntityRecord entityRecord = new EntityRecord(ANOTHER_ENTITY_ID, null, new ArrayList<FieldRecord>()); when(entityService.getEntity(ANOTHER_ENTITY_ID)).thenReturn(entityDto); instanceService.getEntityRecords(entityRecord.getId()); }
@Test(expected = ObjectUpdateException.class) public void shouldThrowExceptionWhileUpdatingReadonlyField() throws ClassNotFoundException { mockDataService(); mockEntity(); when(motechDataService.retrieve("id", INSTANCE_ID)).thenReturn(new TestSample()); List<FieldRecord> fieldRecords = asList( FieldTestHelper.fieldRecord("strField", String.class.getName(), "", "CannotEditThis")); fieldRecords.get(0).setNonEditable(true); EntityRecord record = new EntityRecord(INSTANCE_ID, ENTITY_ID, fieldRecords); instanceService.saveInstance(record); }
@Test public void shouldLoadBlobField() throws InstanceNotFoundException { EntityDto entityDto = new EntityDto(); entityDto.setReadOnlySecurityMode(null); entityDto.setSecurityMode(null); entityDto.setClassName(TestSample.class.getName()); when(entityService.getEntity(ENTITY_ID + 1)).thenReturn(entityDto); mockDataService(); TestSample instance = Mockito.mock(TestSample.class); when(motechDataService.findById(ENTITY_ID + 1)).thenReturn(instance); instanceService.getInstanceField(12l, ENTITY_ID + 1, "blobField"); verify(motechDataService).getDetachedField(instance, "blobField"); }
@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); }
@Test public void shouldAutoPopulateOwnerAndCreator() { when(entityService.getEntityFieldsForUI(ENTITY_ID)) .thenReturn( asList( FieldTestHelper.fieldDto(1L, "owner", String.class.getName(), "String field", null), FieldTestHelper.fieldDto( 1L, "creator", String.class.getName(), "String field", null))); mockEntity(); setUpSecurityContext(); EntityRecord record = instanceService.newInstance(ENTITY_ID); List<FieldRecord> fieldRecords = record.getFields(); assertEquals( asList("motech", "motech"), extract(fieldRecords, on(FieldRecord.class).getValue())); }
@Test public void shouldReturnNewInstances() { mockSampleFields(); mockEntity(); EntityRecord record = instanceService.newInstance(ENTITY_ID); assertNotNull(record); assertEquals(Long.valueOf(ENTITY_ID), record.getEntitySchemaId()); assertNull(record.getId()); List<FieldRecord> fieldRecords = record.getFields(); assertCommonFieldRecordFields(fieldRecords); assertEquals( asList("Default", 7, null, null, null), extract(fieldRecords, on(FieldRecord.class).getValue())); }
@Test public void shouldNotUpdateGridSizeWhenUsernameIsBlank() { EntityDto entityDto = new EntityDto(); entityDto.setReadOnlySecurityMode(null); entityDto.setSecurityMode(null); entityDto.setClassName(TestSample.class.getName()); EntityRecord entityRecord = new EntityRecord(ENTITY_ID + 1, null, new ArrayList<FieldRecord>()); when(entityService.getEntity(ENTITY_ID + 1)).thenReturn(entityDto); mockDataService(); instanceService.getEntityRecords(entityRecord.getId(), new QueryParams(1, 100)); verify(entityService).getEntityFieldsForUI(ENTITY_ID + 1); verify(userPreferencesService, never()).updateGridSize(anyLong(), anyString(), anyInt()); }
@Test public void shouldNotAutoPopulateOwnerAndCreatorForNonEditableFields() { FieldDto ownerField = FieldTestHelper.fieldDto(1L, "owner", String.class.getName(), "String field", null); ownerField.setNonEditable(true); FieldDto creatorField = FieldTestHelper.fieldDto(1L, "creator", String.class.getName(), "String field", null); creatorField.setNonEditable(true); when(entityService.getEntityFieldsForUI(ENTITY_ID)) .thenReturn(asList(ownerField, creatorField)); mockEntity(); setUpSecurityContext(); EntityRecord record = instanceService.newInstance(ENTITY_ID); List<FieldRecord> fieldRecords = record.getFields(); assertEquals(asList(null, null), extract(fieldRecords, on(FieldRecord.class).getValue())); }
@Test public void shouldUpdateGridSize() { setUpSecurityContext(); EntityDto entityDto = new EntityDto(); entityDto.setReadOnlySecurityMode(null); entityDto.setSecurityMode(null); entityDto.setClassName(TestSample.class.getName()); EntityRecord entityRecord = new EntityRecord(ENTITY_ID + 1, null, new ArrayList<>()); when(entityService.getEntity(ENTITY_ID + 1)).thenReturn(entityDto); mockDataService(); instanceService.getEntityRecords(entityRecord.getId(), new QueryParams(1, 100)); verify(entityService).getEntityFieldsForUI(ENTITY_ID + 1); verify(userPreferencesService).updateGridSize(ENTITY_ID + 1, "motech", 100); }
@Test public void shouldReturnEntityInstance() { mockDataService(); mockSampleFields(); mockEntity(); when(motechDataService.retrieve("id", INSTANCE_ID)) .thenReturn(new TestSample("Hello world", 99)); EntityRecord record = instanceService.getEntityInstance(ENTITY_ID, INSTANCE_ID); assertNotNull(record); assertEquals(Long.valueOf(ENTITY_ID), record.getEntitySchemaId()); assertEquals(Long.valueOf(INSTANCE_ID), record.getId()); List<FieldRecord> fieldRecords = record.getFields(); assertCommonFieldRecordFields(fieldRecords); assertEquals( asList("Hello world", 99, null, null, null), extract(fieldRecords, on(FieldRecord.class).getValue())); }
@Test public void shouldReturnInstancesFromTrash() { mockDataService(); mockSampleFields(); mockEntity(); QueryParams queryParams = new QueryParams(1, 10); when(trashService.getInstancesFromTrash(anyString(), eq(queryParams))) .thenReturn(sampleCollection()); List<BasicEntityRecord> records = instanceService.getTrashRecords(ENTITY_ID, queryParams); verify(trashService).getInstancesFromTrash(anyString(), eq(queryParams)); assertNotNull(records); assertEquals(records.size(), 1); // Make sure all fields that were in the instance are still available assertEquals(records.get(0).getFields().size(), 5); // should not perform update when username is null or blank verify(userPreferencesService, never()).updateGridSize(anyLong(), anyString(), anyInt()); }
@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)); }
@Test(expected = EntityNotFoundException.class) public void shouldThrowEntityNotFoundException() { instanceService.getEntityInstance(ENTITY_ID, INSTANCE_ID); }