use of com.devonfw.cobigen.openapiplugin.model.OperationDef 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;
}
use of com.devonfw.cobigen.openapiplugin.model.OperationDef 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;
}
use of com.devonfw.cobigen.openapiplugin.model.OperationDef 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.OperationDef 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();
}
use of com.devonfw.cobigen.openapiplugin.model.OperationDef 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");
}
Aggregations