Search in sources :

Example 6 with FieldDescriptor

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

the class AttributeControllerDocumentation method testGetByIdOk.

@Override
public void testGetByIdOk() throws Exception {
    InputStream input = new ClassPathResource("attribute/SQLViewAttribute.json").getInputStream();
    MockHttpSession session = getSession("ALL");
    MvcResult postResult = mvc.perform(post(schema.getRelativeApiEndpoint()).session(session).contentType(TestUtils.APPLICATION_JSON_UTF8).content(ByteStreams.toByteArray(input))).andExpect(status().is(createdStatus)).andReturn();
    Set<FieldDescriptor> fieldDescriptors = TestUtils.getFieldDescriptors(schema);
    String uid = TestUtils.getCreatedUid(postResult.getResponse().getContentAsString());
    mvc.perform(get(schema.getRelativeApiEndpoint() + "/{id}", uid).session(session).accept(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith(MediaType.APPLICATION_JSON)).andExpect(jsonPath("$.name").value("sqlViewAttribute")).andDo(documentPrettyPrint(schema.getPlural() + "/id", responseFields(fieldDescriptors.toArray(new FieldDescriptor[fieldDescriptors.size()]))));
}
Also used : InputStream(java.io.InputStream) MockHttpSession(org.springframework.mock.web.MockHttpSession) MvcResult(org.springframework.test.web.servlet.MvcResult) ClassPathResource(org.springframework.core.io.ClassPathResource) FieldDescriptor(org.springframework.restdocs.payload.FieldDescriptor)

Example 7 with FieldDescriptor

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

the class DataElementCategoryControllerDocumentation method testGetAll.

@Test
@Override
public void testGetAll() throws Exception {
    manager.save(createDataElementCategory('A'));
    manager.save(createDataElementCategory('B'));
    manager.save(createDataElementCategory('C'));
    manager.save(createDataElementCategory('D'));
    MockHttpSession session = getSession("ALL");
    List<FieldDescriptor> fieldDescriptors = new ArrayList<>();
    fieldDescriptors.addAll(ResponseDocumentation.pager());
    fieldDescriptors.add(fieldWithPath(CategorySchemaDescriptor.PLURAL).description("Data element categories"));
    mvc.perform(get("/" + CategorySchemaDescriptor.PLURAL).session(session).accept(TestUtils.APPLICATION_JSON_UTF8)).andExpect(status().isOk()).andExpect(content().contentTypeCompatibleWith(TestUtils.APPLICATION_JSON_UTF8)).andExpect(jsonPath("$." + CategorySchemaDescriptor.PLURAL).isArray()).andExpect(jsonPath("$." + CategorySchemaDescriptor.PLURAL + ".length()").value(5)).andDo(documentPrettyPrint(CategorySchemaDescriptor.PLURAL + "/all", responseFields(fieldDescriptors.toArray(new FieldDescriptor[fieldDescriptors.size()]))));
}
Also used : ArrayList(java.util.ArrayList) 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 8 with FieldDescriptor

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

the class DataElementCategoryControllerDocumentation method testCreate.

@Test
@Override
public void testCreate() throws Exception {
    MockHttpSession session = getSession("F_CATEGORY_PUBLIC_ADD");
    DataElementCategoryOption categoryOptionA = createCategoryOption('A');
    DataElementCategoryOption categoryOptionB = createCategoryOption('B');
    DataElementCategoryOption categoryOptionC = createCategoryOption('C');
    DataElementCategory cat = createDataElementCategory('A', categoryOptionA, categoryOptionB, categoryOptionC);
    Schema schema = schemaService.getSchema(DataElementCategory.class);
    Set<FieldDescriptor> fieldDescriptors = TestUtils.getFieldDescriptors(schema);
    mvc.perform(post("/" + ENDPOINT).session(session).contentType(TestUtils.APPLICATION_JSON_UTF8).content(TestUtils.convertObjectToJsonBytes(cat))).andExpect(status().is(createdStatus)).andDo(documentPrettyPrint("categories/create", requestFields(fieldDescriptors.toArray(new FieldDescriptor[fieldDescriptors.size()]))));
    cat = manager.getByName(DataElementCategory.class, "DataElementCategoryA");
    assertNotNull(cat);
}
Also used : DataElementCategoryOption(org.hisp.dhis.dataelement.DataElementCategoryOption) 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 9 with FieldDescriptor

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

the class DataElementControllerDocumentation method testFilterLike.

@Test
public void testFilterLike() throws Exception {
    MockHttpSession session = getSession("F_DATAELEMENT_PUBLIC_ADD");
    DataElement de = createDataElement('A');
    manager.save(de);
    List<FieldDescriptor> fieldDescriptors = new ArrayList<>();
    fieldDescriptors.addAll(ResponseDocumentation.pager());
    fieldDescriptors.add(fieldWithPath("dataElements").description("Data elements"));
    mvc.perform(get("/dataElements?filter=name:like:DataElementA").session(session).contentType(TestUtils.APPLICATION_JSON_UTF8)).andExpect(jsonPath("$.pager.total", new GreaterThan<Integer>(0))).andDo(documentPrettyPrint("data-elements/filter", responseFields(fieldDescriptors.toArray(new FieldDescriptor[fieldDescriptors.size()]))));
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) ArrayList(java.util.ArrayList) 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 10 with FieldDescriptor

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

the class DataElementControllerDocumentation method testFieldsFilterOk.

@Test
public void testFieldsFilterOk() throws Exception {
    MockHttpSession session = getSession("F_DATAELEMENT_PUBLIC_ADD");
    DataElement de = createDataElement('A');
    manager.save(de);
    List<FieldDescriptor> fieldDescriptors = new ArrayList<>();
    fieldDescriptors.addAll(ResponseDocumentation.pager());
    fieldDescriptors.add(fieldWithPath("dataElements").description("Data elements"));
    mvc.perform(get("/dataElements?filter=name:eq:DataElementA&fields=id,name,valueType").session(session).contentType(TestUtils.APPLICATION_JSON_UTF8)).andExpect(jsonPath("$.dataElements[*].id").exists()).andExpect(jsonPath("$.dataElements[*].name").exists()).andExpect(jsonPath("$.dataElements[*].valueType").exists()).andExpect(jsonPath("$.dataElements[*].categoryCombo").doesNotExist()).andDo(documentPrettyPrint("data-elements/fields", responseFields(fieldDescriptors.toArray(new FieldDescriptor[fieldDescriptors.size()]))));
}
Also used : DataElement(org.hisp.dhis.dataelement.DataElement) ArrayList(java.util.ArrayList) MockHttpSession(org.springframework.mock.web.MockHttpSession) FieldDescriptor(org.springframework.restdocs.payload.FieldDescriptor) Test(org.junit.Test) AbstractWebApiTest(org.hisp.dhis.webapi.documentation.controller.AbstractWebApiTest)

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