Search in sources :

Example 1 with LookupDto

use of org.motechproject.mds.dto.LookupDto in project motech by motech.

the class MdsDummyDataGeneratorImpl method prepareDummyEntity.

private void prepareDummyEntity(int number, int fieldsPerEntity, int lookupsPerEntity) throws IOException {
    EntityDto entityDto = new EntityDto(Long.valueOf(number), entityPrefix.concat(String.valueOf(number)));
    entityDto = entityService.createEntity(entityDto);
    List<FieldDto> fields = new ArrayList<>();
    for (int i = 0; i < fieldsPerEntity; i++) {
        TypeDto type = pickRandomFieldType();
        fields.add(new FieldDto(null, entityDto.getId(), type, new FieldBasicDto(fieldPrefix.concat(String.valueOf(i)), fieldPrefix.concat(String.valueOf(i))), false, null, null, settingsFor(type), null));
    }
    entityService.addFields(entityDto, fields);
    List<LookupDto> lookups = new ArrayList<>();
    for (int i = 0; i < lookupsPerEntity; i++) {
        List<LookupFieldDto> lookupFields = new ArrayList<>();
        List<FieldDto> entityFields = entityService.getFields(entityDto.getId());
        int amountOfFields = RAND.nextInt(entityFields.size());
        for (int j = 0; j < amountOfFields; j++) {
            lookupFields.add(new LookupFieldDto(null, entityFields.get(j).getBasic().getName(), LookupFieldType.VALUE));
        }
        lookups.add(new LookupDto(lookupPrefix.concat(String.valueOf(i)), RAND.nextBoolean(), RAND.nextBoolean(), lookupFields, false));
    }
    entityService.addLookups(entityDto.getId(), lookups);
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) FieldBasicDto(org.motechproject.mds.dto.FieldBasicDto) LookupDto(org.motechproject.mds.dto.LookupDto) ArrayList(java.util.ArrayList) TypeDto(org.motechproject.mds.dto.TypeDto) FieldDto(org.motechproject.mds.dto.FieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto) LookupFieldDto(org.motechproject.mds.dto.LookupFieldDto)

Example 2 with LookupDto

use of org.motechproject.mds.dto.LookupDto in project motech by motech.

the class InstanceServiceTest method mockLookups.

private void mockLookups() {
    LookupDto lookup = new LookupDto(TestDataService.LOOKUP_1_NAME, true, true, asList(FieldTestHelper.lookupFieldDto(1L, "strField")), true, "singleObject", asList("strField"));
    when(entityService.getLookupByName(ENTITY_ID, TestDataService.LOOKUP_1_NAME)).thenReturn(lookup);
    Map<String, FieldDto> mapping = new HashMap<>();
    mapping.put("strField", FieldTestHelper.fieldDto(1L, "strField", String.class.getName(), "String field", "Default"));
    when(entityService.getLookupFieldsMapping(ENTITY_ID, TestDataService.LOOKUP_1_NAME)).thenReturn(mapping);
    lookup = new LookupDto(TestDataService.LOOKUP_2_NAME, false, true, asList(FieldTestHelper.lookupFieldDto(1L, "strField")), false, "multiObject", asList("strField"));
    when(entityService.getLookupByName(ENTITY_ID, TestDataService.LOOKUP_2_NAME)).thenReturn(lookup);
    when(entityService.getLookupFieldsMapping(ENTITY_ID, TestDataService.LOOKUP_2_NAME)).thenReturn(mapping);
    lookup = new LookupDto(TestDataService.NULL_EXPECTING_LOOKUP_NAME, false, true, asList(FieldTestHelper.lookupFieldDto(3L, "dtField")), false, "nullParamExpected", asList("dtField"));
    when(entityService.getLookupByName(ENTITY_ID, TestDataService.NULL_EXPECTING_LOOKUP_NAME)).thenReturn(lookup);
    mapping = new HashMap<>();
    mapping.put("dtField", FieldTestHelper.fieldDto(3L, "dtField", DateTime.class.getName(), "DateTime field", null));
    when(entityService.getLookupFieldsMapping(ENTITY_ID, TestDataService.NULL_EXPECTING_LOOKUP_NAME)).thenReturn(mapping);
}
Also used : HashMap(java.util.HashMap) LookupDto(org.motechproject.mds.dto.LookupDto) Matchers.anyString(org.mockito.Matchers.anyString) FieldDto(org.motechproject.mds.dto.FieldDto)

Example 3 with LookupDto

use of org.motechproject.mds.dto.LookupDto in project motech by motech.

the class EntityServiceImplTest method shouldAddNewLookup.

@Test
public void shouldAddNewLookup() {
    // given
    long entityId = 1L;
    LookupDto lookupDto = new LookupDto("lookupName", true, false, null, false);
    lookupDto.getLookupFields().add(lookupFieldDto("zero"));
    lookupDto.getLookupFields().add(lookupFieldDto("two"));
    int fieldCount = 4;
    List<Field> fields = new ArrayList<>();
    String[] fieldNames = { "zero", "one", "two", "three" };
    for (long idx = 0; idx < fieldCount; ++idx) {
        Field field = mock(Field.class);
        doReturn(idx).when(field).getId();
        doReturn(field).when(entity).getField(idx);
        String fieldName = fieldNames[(int) idx];
        doReturn(field).when(entity).getField(fieldName);
        doReturn(fieldName).when(field).getName();
        if (idx % 2 == 0) {
            fields.add(field);
        }
    }
    // when
    doReturn(entity).when(allEntities).retrieveById(entityId);
    doReturn(null).when(entity).getLookupById(anyLong());
    entityService.addLookups(entityId, asList(lookupDto));
    // then
    verify(allEntities).retrieveById(entityId);
    verify(entity).getLookupByName("lookupName");
    verify(entity).addLookup(lookupCaptor.capture());
    for (long idx = 0; idx < fieldCount; ++idx) {
        int wantedNumberOfInvocations = (int) Math.abs(idx % 2 - 1);
        verify(entity, times(wantedNumberOfInvocations)).getField(fieldNames[(int) idx]);
    }
    Lookup lookup = lookupCaptor.getValue();
    assertEquals(lookupDto.getLookupName(), lookup.getLookupName());
    assertEquals(lookupDto.isSingleObjectReturn(), lookup.isSingleObjectReturn());
    assertEquals(lookupDto.isExposedViaRest(), lookup.isExposedViaRest());
    assertEquals(fields, lookup.getFields());
}
Also used : Field(org.motechproject.mds.domain.Field) LookupDto(org.motechproject.mds.dto.LookupDto) ArrayList(java.util.ArrayList) Lookup(org.motechproject.mds.domain.Lookup) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 4 with LookupDto

use of org.motechproject.mds.dto.LookupDto in project motech by motech.

the class EntityServiceImplTest method shouldUpdateExistingLookup.

@Test
public void shouldUpdateExistingLookup() {
    // given
    long entityId = 1L;
    LookupDto lookupDto = new LookupDto("lookupName", true, false, null, false);
    lookupDto.getLookupFields().add(lookupFieldDto("zero"));
    lookupDto.getLookupFields().add(lookupFieldDto("two"));
    Lookup lookup = new Lookup();
    int fieldCount = 4;
    List<Field> fields = new ArrayList<>();
    String[] fieldNames = { "zero", "one", "two", "three" };
    for (long idx = 0; idx < fieldCount; ++idx) {
        Field field = mock(Field.class);
        doReturn(idx).when(field).getId();
        doReturn(field).when(entity).getField(idx);
        String fieldName = fieldNames[(int) idx];
        doReturn(field).when(entity).getField(fieldName);
        doReturn(fieldName).when(field).getName();
        if (idx % 2 == 0) {
            fields.add(field);
        }
    }
    // when
    doReturn(entity).when(allEntities).retrieveById(entityId);
    doReturn(lookup).when(entity).getLookupByName("lookupName");
    entityService.addLookups(entityId, asList(lookupDto));
    // then
    verify(allEntities).retrieveById(entityId);
    verify(entity).getLookupByName("lookupName");
    verify(entity, never()).addLookup(any(Lookup.class));
    assertEquals(lookupDto.getLookupName(), lookup.getLookupName());
    assertEquals(lookupDto.isSingleObjectReturn(), lookup.isSingleObjectReturn());
    assertEquals(lookupDto.isExposedViaRest(), lookup.isExposedViaRest());
    assertEquals(fields, lookup.getFields());
}
Also used : Field(org.motechproject.mds.domain.Field) LookupDto(org.motechproject.mds.dto.LookupDto) ArrayList(java.util.ArrayList) Lookup(org.motechproject.mds.domain.Lookup) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 5 with LookupDto

use of org.motechproject.mds.dto.LookupDto in project motech by motech.

the class EditableLookupsLoaderTest method shouldAddLookupIfJsonIsValid.

@Test
public void shouldAddLookupIfJsonIsValid() throws Exception {
    JsonLookupDto expectedJsonLookup = new JsonLookupDto(CLASS_NAME, ORIGIN_LOOKUP_NAME);
    LookupDto expectedLookup = loadLookup();
    when(bundle.getResource(MDS_LOOKUPS_JSON)).thenReturn(VALID_JSON);
    when(jsonLookupService.exists(CLASS_NAME, ORIGIN_LOOKUP_NAME)).thenReturn(false);
    editableLookupsLoader.addEditableLookups(output, bundle);
    verify(jsonLookupService, times(1)).exists(matches(CLASS_NAME), matches(ORIGIN_LOOKUP_NAME));
    verify(jsonLookupService, times(1)).createJsonLookup(jsonLookupCaptor.capture());
    assertLookupsEquals(expectedJsonLookup, jsonLookupCaptor.getValue());
    assertEquals(1, output.getLookupProcessorOutputs().size());
    assertEquals(1, output.getLookupProcessorOutputs().get(CLASS_NAME).size());
    assertEquals(expectedLookup, output.getLookupProcessorOutputs().get(CLASS_NAME).get(0));
}
Also used : JsonLookupDto(org.motechproject.mds.dto.JsonLookupDto) LookupDto(org.motechproject.mds.dto.LookupDto) JsonLookupDto(org.motechproject.mds.dto.JsonLookupDto) Test(org.junit.Test)

Aggregations

LookupDto (org.motechproject.mds.dto.LookupDto)54 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)32 FieldDto (org.motechproject.mds.dto.FieldDto)29 ArrayList (java.util.ArrayList)26 EntityDto (org.motechproject.mds.dto.EntityDto)24 Test (org.junit.Test)23 List (java.util.List)10 Method (java.lang.reflect.Method)9 Arrays.asList (java.util.Arrays.asList)8 FieldTestHelper.lookupFieldDto (org.motechproject.mds.testutil.FieldTestHelper.lookupFieldDto)7 HashMap (java.util.HashMap)6 Field (org.motechproject.mds.domain.Field)5 Lookup (org.motechproject.mds.domain.Lookup)5 AdvancedSettingsDto (org.motechproject.mds.dto.AdvancedSettingsDto)5 FieldBasicDto (org.motechproject.mds.dto.FieldBasicDto)5 Before (org.junit.Before)4 Matchers.anyString (org.mockito.Matchers.anyString)4 RestOptionsDto (org.motechproject.mds.dto.RestOptionsDto)4 LookupExecutor (org.motechproject.mds.lookup.LookupExecutor)4 MetadataDto (org.motechproject.mds.dto.MetadataDto)3