Search in sources :

Example 11 with FieldDescriptor

use of org.springframework.restdocs.payload.FieldDescriptor in project dhis2-core by dhis2.

the class AbstractWebApiTest method testGetAll.

@Test
public void testGetAll() throws Exception {
    Map<Class<? extends IdentifiableObject>, IdentifiableObject> defaultObjectMap = manager.getDefaults();
    IdentifiableObject defaultTestObject = defaultObjectMap.get(testClass);
    int valueToTest = defaultTestObject != null ? 5 : 4;
    manager.save(createTestObject(testClass, 'A'));
    manager.save(createTestObject(testClass, 'B'));
    manager.save(createTestObject(testClass, 'C'));
    manager.save(createTestObject(testClass, 'D'));
    MockHttpSession session = getSession("ALL");
    List<FieldDescriptor> fieldDescriptors = new ArrayList<>();
    fieldDescriptors.addAll(ResponseDocumentation.pager());
    fieldDescriptors.add(fieldWithPath(schema.getPlural()).description(schema.getPlural()));
    mvc.perform(get(schema.getRelativeApiEndpoint()).session(session).accept(TestUtils.APPLICATION_JSON_UTF8)).andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith(TestUtils.APPLICATION_JSON_UTF8)).andExpect(jsonPath("$." + schema.getPlural()).isArray()).andExpect(jsonPath("$." + schema.getPlural() + ".length()").value(valueToTest)).andDo(documentPrettyPrint(schema.getPlural() + "/all", responseFields(fieldDescriptors.toArray(new FieldDescriptor[fieldDescriptors.size()]))));
}
Also used : ArrayList(java.util.ArrayList) MockHttpSession(org.springframework.mock.web.MockHttpSession) IdentifiableObject(org.hisp.dhis.common.IdentifiableObject) FieldDescriptor(org.springframework.restdocs.payload.FieldDescriptor) DhisWebSpringTest(org.hisp.dhis.webapi.DhisWebSpringTest) Test(org.junit.Test)

Example 12 with FieldDescriptor

use of org.springframework.restdocs.payload.FieldDescriptor in project dhis2-core by dhis2.

the class AbstractWebApiTest method testGetByIdOk.

@Test
public void testGetByIdOk() throws Exception {
    MockHttpSession session = getSession("ALL");
    T object = createTestObject(testClass, 'A');
    manager.save(object);
    Set<FieldDescriptor> fieldDescriptors = TestUtils.getFieldDescriptors(schema);
    mvc.perform(get(schema.getRelativeApiEndpoint() + "/{id}", object.getUid()).session(session).accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)).andExpect(jsonPath("$.name").value(object.getName())).andDo(documentPrettyPrint(schema.getPlural() + "/id", responseFields(fieldDescriptors.toArray(new FieldDescriptor[fieldDescriptors.size()]))));
}
Also used : MockHttpSession(org.springframework.mock.web.MockHttpSession) FieldDescriptor(org.springframework.restdocs.payload.FieldDescriptor) DhisWebSpringTest(org.hisp.dhis.webapi.DhisWebSpringTest) Test(org.junit.Test)

Aggregations

FieldDescriptor (org.springframework.restdocs.payload.FieldDescriptor)12 MockHttpSession (org.springframework.mock.web.MockHttpSession)11 Test (org.junit.Test)8 ArrayList (java.util.ArrayList)5 AbstractWebApiTest (org.hisp.dhis.webapi.documentation.controller.AbstractWebApiTest)5 InputStream (java.io.InputStream)3 DhisWebSpringTest (org.hisp.dhis.webapi.DhisWebSpringTest)3 ClassPathResource (org.springframework.core.io.ClassPathResource)3 DataElement (org.hisp.dhis.dataelement.DataElement)2 DataElementCategory (org.hisp.dhis.dataelement.DataElementCategory)2 Schema (org.hisp.dhis.schema.Schema)2 HashSet (java.util.HashSet)1 IdentifiableObject (org.hisp.dhis.common.IdentifiableObject)1 DataElementCategoryOption (org.hisp.dhis.dataelement.DataElementCategoryOption)1 Property (org.hisp.dhis.schema.Property)1 MvcResult (org.springframework.test.web.servlet.MvcResult)1