Search in sources :

Example 1 with Lookup

use of org.motechproject.mds.domain.Lookup in project motech by motech.

the class UserPreferencesServiceTest method getField2.

private Field getField2() {
    Field field = new Field(null, "sampleField2", "Display Name 2", true, false, false, false, false, false, "default 2", "tooltip 2", "placeholder 2", new HashSet<Lookup>());
    field.setUIDisplayable(true);
    return field;
}
Also used : Field(org.motechproject.mds.domain.Field) Lookup(org.motechproject.mds.domain.Lookup)

Example 2 with Lookup

use of org.motechproject.mds.domain.Lookup 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 3 with Lookup

use of org.motechproject.mds.domain.Lookup 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 4 with Lookup

use of org.motechproject.mds.domain.Lookup in project motech by motech.

the class EntityWriter method writeLookups.

private void writeLookups() throws IOException {
    jsonWriter.name("lookups");
    jsonWriter.beginArray();
    for (Lookup lookup : entity.getLookups()) {
        if (!lookup.isReadOnly()) {
            lookupWriter.writeLookup(lookup);
        }
    }
    jsonWriter.endArray();
}
Also used : Lookup(org.motechproject.mds.domain.Lookup)

Example 5 with Lookup

use of org.motechproject.mds.domain.Lookup in project motech by motech.

the class SwaggerGenerator method addLookupEndpoints.

private void addLookupEndpoints(SwaggerModel swaggerModel, Entity entity, Locale locale) {
    for (Lookup lookup : entity.getLookupsExposedByRest()) {
        String lookupUrl = ClassName.restLookupUrl(entity.getName(), entity.getModule(), entity.getNamespace(), lookup.getMethodName());
        swaggerModel.addPathEntry(lookupUrl, HttpMethod.GET, lookupPathEntry(entity, lookup, locale));
    }
}
Also used : Lookup(org.motechproject.mds.domain.Lookup)

Aggregations

Lookup (org.motechproject.mds.domain.Lookup)30 Field (org.motechproject.mds.domain.Field)16 Entity (org.motechproject.mds.domain.Entity)12 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)8 LookupDto (org.motechproject.mds.dto.LookupDto)5 HashSet (java.util.HashSet)4 MdsEntity (org.motechproject.mds.domain.MdsEntity)4 MdsVersionedEntity (org.motechproject.mds.domain.MdsVersionedEntity)4 HashMap (java.util.HashMap)3 FieldSetting (org.motechproject.mds.domain.FieldSetting)3 RestOptions (org.motechproject.mds.domain.RestOptions)3 Type (org.motechproject.mds.domain.Type)3 TypeSetting (org.motechproject.mds.domain.TypeSetting)3 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)3 LinkedList (java.util.LinkedList)2 Query (javax.jdo.Query)2 Matchers.anyString (org.mockito.Matchers.anyString)2 Tracking (org.motechproject.mds.domain.Tracking)2 FieldDto (org.motechproject.mds.dto.FieldDto)2