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()]))));
}
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()]))));
}
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()]))));
}
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;
}
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()]))));
}
Aggregations