Search in sources :

Example 6 with EntityDef

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();
}
Also used : OpenAPIMatcher(com.devonfw.cobigen.openapiplugin.matcher.OpenAPIMatcher) MatcherTo(com.devonfw.cobigen.api.to.MatcherTo) EntityDef(com.devonfw.cobigen.openapiplugin.model.EntityDef) Test(org.junit.Test)

Example 7 with EntityDef

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;
}
Also used : PropertyDef(com.devonfw.cobigen.openapiplugin.model.PropertyDef) Schema(com.reprezen.kaizen.oasparser.model3.Schema) MediaType(com.reprezen.kaizen.oasparser.model3.MediaType) ResponseDef(com.devonfw.cobigen.openapiplugin.model.ResponseDef) LinkedList(java.util.LinkedList) EntityDef(com.devonfw.cobigen.openapiplugin.model.EntityDef)

Example 8 with EntityDef

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;
}
Also used : Path(com.reprezen.kaizen.oasparser.model3.Path) OpenAPIFile(com.devonfw.cobigen.openapiplugin.model.OpenAPIFile) ArrayList(java.util.ArrayList) LinkedList(java.util.LinkedList) OpenApi3(com.reprezen.kaizen.oasparser.model3.OpenApi3) EntityDef(com.devonfw.cobigen.openapiplugin.model.EntityDef)

Example 9 with EntityDef

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();
}
Also used : OperationDef(com.devonfw.cobigen.openapiplugin.model.OperationDef) PathDef(com.devonfw.cobigen.openapiplugin.model.PathDef) ResponseDef(com.devonfw.cobigen.openapiplugin.model.ResponseDef) EntityDef(com.devonfw.cobigen.openapiplugin.model.EntityDef) Test(org.junit.Test)

Example 10 with EntityDef

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");
            }
        }
    }
}
Also used : PropertyDef(com.devonfw.cobigen.openapiplugin.model.PropertyDef) EntityDef(com.devonfw.cobigen.openapiplugin.model.EntityDef) Test(org.junit.Test)

Aggregations

EntityDef (com.devonfw.cobigen.openapiplugin.model.EntityDef)12 Test (org.junit.Test)9 PropertyDef (com.devonfw.cobigen.openapiplugin.model.PropertyDef)4 ResponseDef (com.devonfw.cobigen.openapiplugin.model.ResponseDef)3 LinkedList (java.util.LinkedList)3 MatcherTo (com.devonfw.cobigen.api.to.MatcherTo)2 OpenAPIMatcher (com.devonfw.cobigen.openapiplugin.matcher.OpenAPIMatcher)2 ComponentDef (com.devonfw.cobigen.openapiplugin.model.ComponentDef)2 HeaderDef (com.devonfw.cobigen.openapiplugin.model.HeaderDef)2 OperationDef (com.devonfw.cobigen.openapiplugin.model.OperationDef)2 PathDef (com.devonfw.cobigen.openapiplugin.model.PathDef)2 CobiGen (com.devonfw.cobigen.api.CobiGen)1 CobiGenAsserts.assertThat (com.devonfw.cobigen.api.assertj.CobiGenAsserts.assertThat)1 InvalidConfigurationException (com.devonfw.cobigen.api.exception.InvalidConfigurationException)1 GenerationReportTo (com.devonfw.cobigen.api.to.GenerationReportTo)1 TemplateTo (com.devonfw.cobigen.api.to.TemplateTo)1 CobiGenFactory (com.devonfw.cobigen.impl.CobiGenFactory)1 InfoDef (com.devonfw.cobigen.openapiplugin.model.InfoDef)1 OpenAPIFile (com.devonfw.cobigen.openapiplugin.model.OpenAPIFile)1 ServerDef (com.devonfw.cobigen.openapiplugin.model.ServerDef)1