Search in sources :

Example 6 with ComponentDef

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

the class OpenAPIMatcherTest method testXRootPackageVariableAssigned.

/**
 * Test if the generation is successful and without warnings, if a requested variable is assigned
 */
@Test
public void testXRootPackageVariableAssigned() {
    ComponentDef componentDef = new ComponentDef();
    componentDef.setName("Tablemanagement");
    componentDef.setUserProperty("x-rootpackage", "com.devonfw");
    OpenAPIMatcher matcher = new OpenAPIMatcher();
    GenerationReportTo report = new GenerationReportTo();
    List<VariableAssignmentTo> va = new ArrayList<>();
    va.add(new VariableAssignmentTo("extension", "rootPackage", "x-rootpackage", false));
    matcher.resolveVariables(new MatcherTo("element", "ComponentDef", componentDef), va, report);
    assertThat(report.getWarnings().size()).isEqualTo(0);
}
Also used : ComponentDef(com.devonfw.cobigen.openapiplugin.model.ComponentDef) VariableAssignmentTo(com.devonfw.cobigen.api.to.VariableAssignmentTo) GenerationReportTo(com.devonfw.cobigen.api.to.GenerationReportTo) OpenAPIMatcher(com.devonfw.cobigen.openapiplugin.matcher.OpenAPIMatcher) ArrayList(java.util.ArrayList) MatcherTo(com.devonfw.cobigen.api.to.MatcherTo) Test(org.junit.Test)

Example 7 with ComponentDef

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

the class OpenAPIMatcherTest method testMissingXRootPackageVariableMandatory.

/**
 * Test if the generation isn't successful and the report contains errors, if a mandatory variable isn't given
 */
@Test
public void testMissingXRootPackageVariableMandatory() {
    ComponentDef componentDef = new ComponentDef();
    componentDef.setName("Tablemanagement");
    OpenAPIMatcher matcher = new OpenAPIMatcher();
    GenerationReportTo report = new GenerationReportTo();
    List<VariableAssignmentTo> vaMandatoryXRootPackage = new ArrayList<>();
    vaMandatoryXRootPackage.add(new VariableAssignmentTo("extension", "rootPackage", "x-rootpackage", true));
    matcher.resolveVariables(new MatcherTo("element", "ComponentDef", componentDef), vaMandatoryXRootPackage, report);
    assertThat(report.getErrors().get(0).getMessage()).containsSequence(Constants.getMandatoryMessage(true, "x-rootpackage"));
}
Also used : ComponentDef(com.devonfw.cobigen.openapiplugin.model.ComponentDef) VariableAssignmentTo(com.devonfw.cobigen.api.to.VariableAssignmentTo) GenerationReportTo(com.devonfw.cobigen.api.to.GenerationReportTo) OpenAPIMatcher(com.devonfw.cobigen.openapiplugin.matcher.OpenAPIMatcher) ArrayList(java.util.ArrayList) MatcherTo(com.devonfw.cobigen.api.to.MatcherTo) Test(org.junit.Test)

Example 8 with ComponentDef

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

the class OpenAPIInputReader method extractComponentsFromPaths.

/**
 * Get a list of components defined at the paths part with the x-component tag. Returns a list of
 * {@link ComponentDef}'s
 *
 * @param paths the paths of the OpenApi3 file
 * @param astOpenApi OpenApi3 object which is the AST of the file
 * @return a list of {@link ComponentDef}'s for each path that contains x-component tag
 */
private List<ComponentDef> extractComponentsFromPaths(List<Path> paths, OpenApi3 astOpenApi) {
    for (Path path : paths) {
        if (path.getExtensions().get(Constants.COMPONENT_EXT) != null) {
            String componentName = path.getExtensions().get(Constants.COMPONENT_EXT).toString();
            if (componentName != null && !componentName.isEmpty()) {
                // items on a list are passed by reference, we can change it
                ComponentDef componentDef = getComponent(componentName);
                // If the component has no name, it means no component was found
                if (componentDef.getName() == null) {
                    componentDef.setName(componentName);
                    componentDef.setPaths(extractPaths(astOpenApi.getPaths(), componentName));
                    setExtensionsToComponent(astOpenApi, componentDef);
                    this.components.add(componentDef);
                } else {
                    setExtensionsToComponent(astOpenApi, componentDef);
                }
            }
        }
    }
    return this.components;
}
Also used : Path(com.reprezen.kaizen.oasparser.model3.Path) ComponentDef(com.devonfw.cobigen.openapiplugin.model.ComponentDef)

Example 9 with ComponentDef

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

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

ComponentDef (com.devonfw.cobigen.openapiplugin.model.ComponentDef)11 Test (org.junit.Test)8 MatcherTo (com.devonfw.cobigen.api.to.MatcherTo)5 OpenAPIMatcher (com.devonfw.cobigen.openapiplugin.matcher.OpenAPIMatcher)5 PathDef (com.devonfw.cobigen.openapiplugin.model.PathDef)4 LinkedList (java.util.LinkedList)4 GenerationReportTo (com.devonfw.cobigen.api.to.GenerationReportTo)3 VariableAssignmentTo (com.devonfw.cobigen.api.to.VariableAssignmentTo)3 OperationDef (com.devonfw.cobigen.openapiplugin.model.OperationDef)3 ArrayList (java.util.ArrayList)3 EntityDef (com.devonfw.cobigen.openapiplugin.model.EntityDef)2 InvalidConfigurationException (com.devonfw.cobigen.api.exception.InvalidConfigurationException)1 HeaderDef (com.devonfw.cobigen.openapiplugin.model.HeaderDef)1 ParameterDef (com.devonfw.cobigen.openapiplugin.model.ParameterDef)1 ResponseDef (com.devonfw.cobigen.openapiplugin.model.ResponseDef)1 Path (com.reprezen.kaizen.oasparser.model3.Path)1