Search in sources :

Example 1 with DraftData

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

the class EntityControllerTest method shouldNotSaveTemporaryChangeIfEntityNotExists.

@Test
public void shouldNotSaveTemporaryChangeIfEntityNotExists() throws Exception {
    doThrow(new EntityNotFoundException(100L)).when(entityService).saveDraftEntityChanges(eq(100L), any(DraftData.class));
    controller.perform(post("/entities/100/draft").body(new ObjectMapper().writeValueAsString(new DraftData()).getBytes()).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isConflict()).andExpect(content().string(ENTITY_NOT_FOUND));
}
Also used : DraftData(org.motechproject.mds.dto.DraftData) EntityNotFoundException(org.motechproject.mds.exception.entity.EntityNotFoundException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.junit.Test)

Example 2 with DraftData

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

the class EntityControllerTest method shouldAbandonChanges.

@Test
public void shouldAbandonChanges() throws Exception {
    DraftData data = new DraftData();
    data.setEdit(true);
    data.setValues(new HashMap<String, Object>());
    data.getValues().put(DraftData.PATH, "basic.displayName");
    data.getValues().put(DraftData.FIELD_ID, "2");
    data.getValues().put(DraftData.VALUE, singletonList("test"));
    controller.perform(post("/entities/9007/draft").body(new ObjectMapper().writeValueAsBytes(data)).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
    verify(entityService).saveDraftEntityChanges(eq(9007l), draftDataCaptor.capture());
    DraftData capuredData = draftDataCaptor.getValue();
    assertNotNull(capuredData);
    assertTrue(capuredData.isEdit());
    assertEquals("basic.displayName", capuredData.getPath());
    controller.perform(post("/entities/9007/abandon").contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
    verify(entityService).abandonChanges(9007L);
}
Also used : DraftData(org.motechproject.mds.dto.DraftData) Matchers.anyString(org.mockito.Matchers.anyString) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) Test(org.junit.Test)

Example 3 with DraftData

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

the class EntityControllerTest method shouldSaveTemporaryChange.

@Test
public void shouldSaveTemporaryChange() throws Exception {
    DraftData data = new DraftData();
    data.setEdit(true);
    data.setValues(new HashMap<>());
    data.getValues().put(DraftData.PATH, "basic.displayName");
    data.getValues().put(DraftData.FIELD_ID, "2");
    data.getValues().put(DraftData.VALUE, singletonList("test"));
    ResultActions actions = controller.perform(get("/entities/9007")).andExpect(status().isOk());
    EntityDto entity = new ObjectMapper().readValue(actions.andReturn().getResponse().getContentAsByteArray(), EntityDto.class);
    actions = controller.perform(get("/entities/9007/fields")).andExpect(status().isOk());
    List<FieldDto> fields = new ObjectMapper().readValue(actions.andReturn().getResponse().getContentAsByteArray(), new TypeReference<List<FieldDto>>() {
    });
    FieldDto fieldDto = findFieldById(fields, 12L);
    // before change
    assertFalse(entity.isModified());
    assertNotNull(fieldDto);
    assertEquals("ID", fieldDto.getBasic().getDisplayName());
    // change
    controller.perform(post("/entities/9007/draft").body(new ObjectMapper().writeValueAsBytes(data)).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk());
    verify(entityService).saveDraftEntityChanges(eq(9007L), draftDataCaptor.capture());
    DraftData captured = draftDataCaptor.getValue();
    assertTrue(captured.isEdit());
    assertEquals("basic.displayName", captured.getPath());
}
Also used : EntityDto(org.motechproject.mds.dto.EntityDto) DraftData(org.motechproject.mds.dto.DraftData) Collections.singletonList(java.util.Collections.singletonList) Arrays.asList(java.util.Arrays.asList) List(java.util.List) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) ResultActions(org.springframework.test.web.server.ResultActions) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) FieldDto(org.motechproject.mds.dto.FieldDto) Test(org.junit.Test)

Example 4 with DraftData

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

the class DraftBuilder method forFieldRemoval.

public static DraftData forFieldRemoval(Long fieldId) {
    Map<String, Object> values = new HashMap<>();
    values.put(DraftData.FIELD_ID, fieldId);
    DraftData draftData = new DraftData();
    draftData.setValues(values);
    draftData.setRemove(true);
    return draftData;
}
Also used : HashMap(java.util.HashMap) DraftData(org.motechproject.mds.dto.DraftData)

Example 5 with DraftData

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

the class MdsBundleIT method verifyComboboxDataMigration.

private void verifyComboboxDataMigration() throws NoSuchFieldException, ClassNotFoundException, NoSuchMethodException, IllegalAccessException, InvocationTargetException {
    Long entityId = entityService.getEntityByClassName(FOO_CLASS).getId();
    Long fieldId = getFieldIdByName(entityService.getFields(entityId), "someEnum");
    DraftData draft = DraftBuilder.forFieldEdit(fieldId, "settings.2.value", true);
    entityService.saveDraftEntityChanges(entityId, draft);
    entityService.commitChanges(entityId);
    SchemaHolder schemaHolder = entityService.getSchema();
    generator.regenerateMdsDataBundle(schemaHolder, true);
    service = (MotechDataService) ServiceRetriever.getService(bundleContext, ClassName.getInterfaceName(FOO_CLASS), true);
    assertValuesEqual(getExpectedComboboxValues(), getValues(service.detachedCopyAll(service.retrieveAll())));
}
Also used : DraftData(org.motechproject.mds.dto.DraftData) SchemaHolder(org.motechproject.mds.dto.SchemaHolder)

Aggregations

DraftData (org.motechproject.mds.dto.DraftData)16 Test (org.junit.Test)11 HashMap (java.util.HashMap)6 EntityDto (org.motechproject.mds.dto.EntityDto)5 FieldDto (org.motechproject.mds.dto.FieldDto)5 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)4 EntityDraft (org.motechproject.mds.domain.EntityDraft)3 LookupFieldDto (org.motechproject.mds.dto.LookupFieldDto)3 SchemaHolder (org.motechproject.mds.dto.SchemaHolder)2 ArrayList (java.util.ArrayList)1 Arrays.asList (java.util.Arrays.asList)1 Collections.singletonList (java.util.Collections.singletonList)1 HashSet (java.util.HashSet)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Matchers.anyString (org.mockito.Matchers.anyString)1 Field (org.motechproject.mds.domain.Field)1 FieldMetadata (org.motechproject.mds.domain.FieldMetadata)1 FieldSetting (org.motechproject.mds.domain.FieldSetting)1 Lookup (org.motechproject.mds.domain.Lookup)1