Search in sources :

Example 1 with FieldDescriptor

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

the class DataElementCategoryControllerDocumentation method testGetByIdOk.

@Test
@Override
public void testGetByIdOk() throws Exception {
    DataElementCategory cat = createDataElementCategory('A');
    manager.save(cat);
    MockHttpSession session = getSession("ALL");
    Schema schema = schemaService.getSchema(DataElementCategory.class);
    Set<FieldDescriptor> fieldDescriptors = TestUtils.getFieldDescriptors(schema);
    mvc.perform(get("/" + CategorySchemaDescriptor.PLURAL + "/{id}", cat.getUid()).session(session).accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)).andExpect(jsonPath("$.name").value("DataElementCategoryA")).andExpect(jsonPath("$.shortName").value("DataElementCategoryA")).andDo(documentPrettyPrint(ENDPOINT + "/id", responseFields(fieldDescriptors.toArray(new FieldDescriptor[fieldDescriptors.size()]))));
}
Also used : Schema(org.hisp.dhis.schema.Schema) DataElementCategory(org.hisp.dhis.dataelement.DataElementCategory) MockHttpSession(org.springframework.mock.web.MockHttpSession) FieldDescriptor(org.springframework.restdocs.payload.FieldDescriptor) Test(org.junit.Test) AbstractWebApiTest(org.hisp.dhis.webapi.documentation.controller.AbstractWebApiTest)

Example 2 with FieldDescriptor

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

the class AttributeControllerDocumentation method testCreate.

@Override
public void testCreate() throws Exception {
    InputStream input = new ClassPathResource("attribute/SQLViewAttribute.json").getInputStream();
    MockHttpSession session = getSession("ALL");
    Set<FieldDescriptor> fieldDescriptors = TestUtils.getFieldDescriptors(schema);
    mvc.perform(post(schema.getRelativeApiEndpoint()).session(session).contentType(TestUtils.APPLICATION_JSON_UTF8).content(ByteStreams.toByteArray(input))).andExpect(status().is(createdStatus)).andDo(documentPrettyPrint(schema.getPlural() + "/create", requestFields(fieldDescriptors.toArray(new FieldDescriptor[fieldDescriptors.size()]))));
}
Also used : InputStream(java.io.InputStream) MockHttpSession(org.springframework.mock.web.MockHttpSession) ClassPathResource(org.springframework.core.io.ClassPathResource) FieldDescriptor(org.springframework.restdocs.payload.FieldDescriptor)

Example 3 with FieldDescriptor

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

the class AbstractWebApiTest method testCreate.

@Test
public void testCreate() throws Exception {
    MockHttpSession session = getSession("ALL");
    T object = createTestObject(testClass, 'A');
    Set<FieldDescriptor> fieldDescriptors = TestUtils.getFieldDescriptors(schema);
    mvc.perform(post(schema.getRelativeApiEndpoint()).session(session).contentType(TestUtils.APPLICATION_JSON_UTF8).content(TestUtils.convertObjectToJsonBytes(object))).andExpect(status().is(createdStatus)).andDo(documentPrettyPrint(schema.getPlural() + "/create", requestFields(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)

Example 4 with FieldDescriptor

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

the class TestUtils method getFieldDescriptors.

public static Set<FieldDescriptor> getFieldDescriptors(Schema schema) {
    Set<FieldDescriptor> fieldDescriptors = new HashSet<>();
    Map<String, Property> persistedPropertyMap = schema.getPersistedProperties();
    Iterator<?> persistedItr = persistedPropertyMap.keySet().iterator();
    while (persistedItr.hasNext()) {
        Property p = persistedPropertyMap.get(persistedItr.next());
        String pName = p.isCollection() ? p.getCollectionName() : p.getName();
        FieldDescriptor f = fieldWithPath(pName).description(TestUtils.getFieldDescription(p));
        if (!p.isRequired()) {
            f.optional().type(p.getPropertyType());
        }
        fieldDescriptors.add(f);
    }
    Map<String, Property> nonPersistedPropertyMap = schema.getNonPersistedProperties();
    Iterator<?> nonPersistedItr = nonPersistedPropertyMap.keySet().iterator();
    while (nonPersistedItr.hasNext()) {
        Property p = nonPersistedPropertyMap.get(nonPersistedItr.next());
        String pName = p.isCollection() ? p.getCollectionName() : p.getName();
        FieldDescriptor f = fieldWithPath(pName).description(TestUtils.getFieldDescription(p));
        if (!p.isRequired()) {
            f.optional().type(p.getPropertyType());
        }
        fieldDescriptors.add(f);
    }
    return fieldDescriptors;
}
Also used : Property(org.hisp.dhis.schema.Property) HashSet(java.util.HashSet) FieldDescriptor(org.springframework.restdocs.payload.FieldDescriptor)

Example 5 with FieldDescriptor

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

the class AttributeControllerDocumentation method testGetAll.

@Override
public void testGetAll() throws Exception {
    InputStream input = new ClassPathResource("attribute/SQLViewAttribute.json").getInputStream();
    MockHttpSession session = getSession("ALL");
    mvc.perform(post(schema.getRelativeApiEndpoint()).session(session).contentType(TestUtils.APPLICATION_JSON_UTF8).content(ByteStreams.toByteArray(input))).andExpect(status().is(createdStatus)).andReturn();
    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(1)).andDo(documentPrettyPrint(schema.getPlural() + "/all", responseFields(fieldDescriptors.toArray(new FieldDescriptor[fieldDescriptors.size()]))));
}
Also used : InputStream(java.io.InputStream) ArrayList(java.util.ArrayList) MockHttpSession(org.springframework.mock.web.MockHttpSession) ClassPathResource(org.springframework.core.io.ClassPathResource) FieldDescriptor(org.springframework.restdocs.payload.FieldDescriptor)

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