use of com.devonfw.cobigen.openapiplugin.model.EntityDef in project cobigen by devonfw.
the class OpenAPIMatcherTest method testInvalidEntityDefMatching.
/**
* Test non valid {@link EntityDef} matching
*/
@Test
public void testInvalidEntityDefMatching() {
EntityDef entityDef = new EntityDef();
entityDef.setComponentName("Tablemanagement");
OpenAPIMatcher matcher = new OpenAPIMatcher();
boolean matches = matcher.matches(new MatcherTo("element", "EntityDefs", entityDef));
assertThat(matches).isFalse();
}
use of com.devonfw.cobigen.openapiplugin.model.EntityDef in project cobigen by devonfw.
the class OpenAPIInputReader method extractResponses.
/**
* Returns a {@link ResponseDef} from a operation '200' response definition depending on the tags
*
* @param overlay overlay of the operator
* @param responses list of OpenApu responses definition
* @param tags list of oasp4j relative tags
* @return List of {@link ResponseDef}'s
*/
private List<ResponseDef> extractResponses(Map<String, ? extends Response> responses, Collection<String> tags) {
ResponseDef response;
List<String> mediaTypes = new LinkedList<>();
List<ResponseDef> resps = new LinkedList<>();
String schemaType;
for (String resp : responses.keySet()) {
response = new ResponseDef();
response.setCode(resp);
Map<String, MediaType> contentMediaTypes = responses.get(resp).getContentMediaTypes();
response.setDescription(responses.get(resp).getDescription());
if (contentMediaTypes != null) {
if (contentMediaTypes.isEmpty()) {
response.setIsVoid(true);
}
for (String media : contentMediaTypes.keySet()) {
mediaTypes.add(media);
Schema schema = contentMediaTypes.get(media).getSchema();
if (schema != null) {
schemaType = schema.getType();
if (Constants.OBJECT.equals(schemaType)) {
response.setType(schema.getName());
response.setIsEntity(true);
EntityDef eDef = new EntityDef();
List<PropertyDef> propDefs = new LinkedList<>();
for (Schema propertySchema : schema.getProperties().values()) {
PropertyDef prop = new PropertyDef();
prop.setDescription(propertySchema.getDescription());
prop.setFormat(propertySchema.getFormat());
prop.setName(propertySchema.getName());
prop.setType(propertySchema.getType());
propDefs.add(prop);
}
eDef.setName(schema.getName());
eDef.setProperties(propDefs);
response.setEntityRef(eDef);
} else if (Constants.ARRAY.equals(schemaType)) {
if (schema.getItemsSchema() != null) {
response.setType(schema.getItemsSchema().getName());
response.setIsEntity(true);
} else {
response.setType(schema.getItemsSchema().getType());
}
if (tags.contains(Constants.PAGINATED)) {
response.setIsPaginated(true);
} else {
response.setIsArray(true);
}
} else if (schemaType != null) {
response.setType(schemaType);
} else {
response.setIsVoid(true);
}
}
}
} else {
response.setIsVoid(true);
}
response.setMediaTypes(mediaTypes);
resps.add(response);
}
return resps;
}
use of com.devonfw.cobigen.openapiplugin.model.EntityDef in project cobigen by devonfw.
the class OpenAPIInputReader method getInputObjects.
@Override
public List<Object> getInputObjects(Object input, Charset inputCharset) {
List<Object> inputs = new LinkedList<>();
List<Path> paths = new LinkedList<>();
if (input instanceof OpenAPIFile) {
OpenApi3 astOpenApi = ((OpenAPIFile) input).getAST();
inputs.addAll(extractComponents(astOpenApi));
for (String key : astOpenApi.getPaths().keySet()) {
Path path = astOpenApi.getPaths().get(key);
paths.add(path);
}
inputs.addAll(extractComponentsFromPaths(paths, astOpenApi));
List<EntityDef> entityDefs = new ArrayList<>();
for (Object obj : inputs) {
if (obj instanceof EntityDef) {
entityDefs.add((EntityDef) obj);
}
}
for (EntityDef entityDef : entityDefs) {
entityDef.setAllEntityDefs(entityDefs);
}
}
return inputs;
}
use of com.devonfw.cobigen.openapiplugin.model.EntityDef in project cobigen by devonfw.
the class OpenAPIInputReaderTest method testRetrieveResponsesOfPath.
@Test
public void testRetrieveResponsesOfPath() throws Exception {
List<Object> inputObjects = getInputs("two-components.yaml");
boolean found = false;
for (Object o : inputObjects) {
if (isEntityDef(o)) {
EntityDef eDef = (EntityDef) o;
if (eDef.getName().equals("Table")) {
assertThat(eDef.getComponent().getPaths()).hasSize(2);
for (PathDef pathDef : eDef.getComponent().getPaths()) {
for (OperationDef opDef : pathDef.getOperations()) {
if (opDef.getOperationId() != null && opDef.getOperationId().equals("findTable")) {
found = true;
assertThat(opDef.getResponses()).hasSize(2);
for (ResponseDef respDef : opDef.getResponses()) {
if (respDef.getCode().equals("200")) {
assertThat(respDef.getMediaTypes()).hasSize(2);
assertThat(respDef.getMediaTypes()).containsExactly("application/json", "text/plain");
} else if (respDef.getCode().equals("404")) {
assertThat(respDef.getDescription()).isEqualTo("Not found");
} else {
found = false;
}
}
}
}
}
}
}
}
assertThat(found).as("findTable path operation not found!").isTrue();
}
use of com.devonfw.cobigen.openapiplugin.model.EntityDef in project cobigen by devonfw.
the class OpenAPIInputReaderTest method testNullableEnum.
/**
* Tests if the input reader can handle nullable enums. See: https://github.com/devonfw/cobigen/issues/1244
*
* @throws Exception
*/
@Test
public void testNullableEnum() throws Exception {
List<Object> inputObjects = getInputs("nullableEnum.yaml");
for (Object o : inputObjects) {
if (isEntityDef(o)) {
EntityDef entity = (EntityDef) o;
List<PropertyDef> properties = entity.getProperties();
for (PropertyDef p : properties) {
List<String> enums = p.getEnumElements();
assertThat(enums.get(0)).isEqualTo("enum1");
assertThat(enums.get(1)).isEqualTo("null");
}
}
}
}
Aggregations