Search in sources :

Example 1 with PathDef

use of com.devonfw.cobigen.openapiplugin.model.PathDef in project cobigen by devonfw.

the class OpenAPIInputReader method extractPaths.

/**
 * Get a list of {@link PathDef} from a list of OpenApi paths definitions
 *
 * @param paths the list of OpenApi paths definitions
 * @param componentName the component where the paths belong to
 * @return list of {@link PathDef}'s
 */
private List<PathDef> extractPaths(Map<String, ? extends Path> paths, String componentName) {
    Matcher matcher;
    Pattern pattern;
    String match, rootComponent, version;
    boolean matchFound = false;
    List<PathDef> pathDefs = new LinkedList<>();
    for (String pathKey : paths.keySet()) {
        if (pathKey.toLowerCase().contains(componentName.toLowerCase())) {
            rootComponent = null;
            version = null;
            String[] mp = pathKey.split("/");
            String pathUri = "/";
            match = "^\\/[^\\/]+\\/+[^\\/]+\\/(.+)";
            pattern = Pattern.compile(match);
            matcher = pattern.matcher(pathKey);
            matchFound = matcher.find();
            if (matchFound) {
                pathUri += matcher.group(1);
                if (!pathUri.substring(pathUri.length() - 1).equals("/")) {
                    pathUri += "/";
                }
            }
            if (mp.length > 1) {
                rootComponent = mp[1];
                if (mp.length > 2) {
                    version = mp[2];
                }
            }
            PathDef path = new PathDef(rootComponent, pathUri, version);
            for (String opKey : paths.get(pathKey).getOperations().keySet()) {
                OperationDef operationDef = new OperationDef(opKey);
                Operation operation = paths.get(pathKey).getOperation(opKey);
                operationDef.setDescription(operation.getDescription());
                operationDef.setSummary(operation.getSummary());
                operationDef.setOperationId((operation.getOperationId()));
                operationDef.setResponses(extractResponses(operation.getResponses(), operation.getTags()));
                operationDef.setTags(operation.getTags());
                if (path.getOperations() == null) {
                    path.setOperations(new ArrayList<OperationDef>());
                }
                operationDef.getParameters().addAll(extractParameters(Overlay.of(operation)));
                path.getOperations().add(operationDef);
            }
            pathDefs.add(path);
        }
    }
    return pathDefs;
}
Also used : Pattern(java.util.regex.Pattern) Matcher(java.util.regex.Matcher) OperationDef(com.devonfw.cobigen.openapiplugin.model.OperationDef) PathDef(com.devonfw.cobigen.openapiplugin.model.PathDef) Operation(com.reprezen.kaizen.oasparser.model3.Operation) LinkedList(java.util.LinkedList)

Example 2 with PathDef

use of com.devonfw.cobigen.openapiplugin.model.PathDef in project cobigen by devonfw.

the class OpenAPIInputReaderTest method getParametersOfOperations.

private List<ParameterDef> getParametersOfOperations(String testInputFilename) throws Exception {
    List<Object> inputObjects = getInputs(testInputFilename);
    List<ComponentDef> cmps = new LinkedList<>();
    for (Object o : inputObjects) {
        if (isComponentDef(o)) {
            cmps.add(((ComponentDef) o));
        } else {
            cmps.add(((EntityDef) o).getComponent());
        }
    }
    List<ParameterDef> parameters = new LinkedList<>();
    for (ComponentDef cmp : cmps) {
        for (PathDef path : cmp.getPaths()) {
            for (OperationDef op : path.getOperations()) {
                for (ParameterDef param : op.getParameters()) {
                    parameters.add(param);
                }
            }
        }
    }
    return parameters;
}
Also used : ComponentDef(com.devonfw.cobigen.openapiplugin.model.ComponentDef) ParameterDef(com.devonfw.cobigen.openapiplugin.model.ParameterDef) OperationDef(com.devonfw.cobigen.openapiplugin.model.OperationDef) PathDef(com.devonfw.cobigen.openapiplugin.model.PathDef) LinkedList(java.util.LinkedList)

Example 3 with PathDef

use of com.devonfw.cobigen.openapiplugin.model.PathDef 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 4 with PathDef

use of com.devonfw.cobigen.openapiplugin.model.PathDef in project cobigen by devonfw.

the class OpenAPIInputReaderTest method testResponse.

@Test
public void testResponse() throws Exception {
    List<Object> inputObjects = getInputs("componentResponseType.yaml");
    boolean found = false;
    for (Object o : inputObjects) {
        if (isEntityDef(o)) {
            EntityDef e = (EntityDef) o;
            ComponentDef c = e.getComponent();
            for (PathDef p : c.getPaths()) {
                if (p.getPathURI().equals("/sampledata/customSearch/")) {
                    for (OperationDef op : p.getOperations()) {
                        for (ResponseDef r : op.getResponses()) {
                            assertThat(r.getType()).isEqualTo("SampleData");
                            found = true;
                        }
                    }
                }
            }
        }
    }
    assertThat(found).isTrue();
}
Also used : ComponentDef(com.devonfw.cobigen.openapiplugin.model.ComponentDef) 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 5 with PathDef

use of com.devonfw.cobigen.openapiplugin.model.PathDef in project cobigen by devonfw.

the class OpenAPIInputReaderTest method testRetrieveOperationsOfPath.

@Test
public void testRetrieveOperationsOfPath() throws Exception {
    List<Object> inputObjects = getInputs("two-components.yaml");
    List<ComponentDef> cmps = new LinkedList<>();
    for (Object o : inputObjects) {
        if (isComponentDef(o)) {
            cmps.add((ComponentDef) o);
        } else {
            cmps.add(((EntityDef) o).getComponent());
        }
    }
    List<OperationDef> operations = new LinkedList<>();
    for (ComponentDef cmp : cmps) {
        for (PathDef path : cmp.getPaths()) {
            for (OperationDef op : path.getOperations()) {
                operations.add(op);
            }
        }
    }
    assertThat(operations).extracting("type").hasSize(10);
    assertThat(operations).extracting("type").containsExactly("get", "post", "get", "get", "post", "get", "post", "get", "get", "post");
}
Also used : ComponentDef(com.devonfw.cobigen.openapiplugin.model.ComponentDef) OperationDef(com.devonfw.cobigen.openapiplugin.model.OperationDef) PathDef(com.devonfw.cobigen.openapiplugin.model.PathDef) LinkedList(java.util.LinkedList) Test(org.junit.Test)

Aggregations

PathDef (com.devonfw.cobigen.openapiplugin.model.PathDef)6 OperationDef (com.devonfw.cobigen.openapiplugin.model.OperationDef)5 ComponentDef (com.devonfw.cobigen.openapiplugin.model.ComponentDef)4 LinkedList (java.util.LinkedList)4 Test (org.junit.Test)4 EntityDef (com.devonfw.cobigen.openapiplugin.model.EntityDef)2 ResponseDef (com.devonfw.cobigen.openapiplugin.model.ResponseDef)2 ParameterDef (com.devonfw.cobigen.openapiplugin.model.ParameterDef)1 Operation (com.reprezen.kaizen.oasparser.model3.Operation)1 Matcher (java.util.regex.Matcher)1 Pattern (java.util.regex.Pattern)1