2 * Copyright 2016-2017, Nokia Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.restapi;
19 import com.google.common.collect.HashMultimap;
20 import com.google.common.collect.Sets;
21 import com.google.gson.JsonElement;
22 import com.google.gson.JsonObject;
23 import com.google.gson.JsonParser;
24 import java.lang.reflect.Method;
25 import java.util.HashSet;
27 import java.util.NoSuchElementException;
29 import junit.framework.TestCase;
30 import org.junit.Test;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
32 import org.springframework.web.bind.annotation.RequestMapping;
33 import org.springframework.web.bind.annotation.RequestMethod;
35 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.child;
36 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil.loadFile;
38 public class TestSwaggerDefinitionConsistency extends TestBase {
40 public static final HashSet<Class<?>> CLASSES = Sets.newHashSet(LcmApi.class, LcnApi.class, SwaggerApi.class, ConverterApi.class, SoV2Api.class);
43 public void test() throws Exception {
44 JsonObject root = new JsonParser().parse(new String(loadFile("self.swagger.json"))).getAsJsonObject();
45 String basePath = root.get("basePath").getAsString();
46 HashMultimap<String, RequestMethod> expectedPaths = HashMultimap.create();
47 for (Map.Entry<String, JsonElement> pathName : child(root, "paths").entrySet()) {
48 JsonObject path = child(child(root, "paths"), pathName.getKey());
49 for (Map.Entry<String, JsonElement> method : path.entrySet()) {
50 locate(basePath + pathName.getKey());
51 expectedPaths.put(basePath + pathName.getKey(), RequestMethod.valueOf(method.getKey().toUpperCase()));
55 for (Class<?> clazz : CLASSES) {
56 RequestMapping currentBasePath = clazz.getAnnotation(RequestMapping.class);
57 for (Method method : clazz.getMethods()) {
58 RequestMapping methodMapping = method.getAnnotation(RequestMapping.class);
59 if (methodMapping != null) {
60 String fPath = currentBasePath.value()[0] + methodMapping.value()[0];
61 RequestMethod restMethod = methodMapping.method()[0];
62 Set<RequestMethod> currentMethods = expectedPaths.get(fPath);
63 if (!currentMethods.contains(restMethod)) {
64 TestCase.fail("Not documented REST API " + fPath + " " + restMethod + " current " + currentMethods);
71 private void locate(String path) {
72 Set<String> paths = new HashSet<>();
73 for (Class<?> clazz : CLASSES) {
74 RequestMapping basePath = clazz.getAnnotation(RequestMapping.class);
75 for (Method method : clazz.getMethods()) {
76 RequestMapping methodMapping = method.getAnnotation(RequestMapping.class);
77 if (methodMapping != null) {
78 paths.add(basePath.value()[0] + methodMapping.value()[0]);
79 if (path.equals(basePath.value()[0] + methodMapping.value()[0])) {
85 for (Class<?> clazz : CLASSES) {
86 RequestMapping basePath = clazz.getAnnotation(RequestMapping.class);
87 for (Method method : clazz.getMethods()) {
88 RequestMapping methodMapping = method.getAnnotation(RequestMapping.class);
89 if (methodMapping != null) {
90 paths.add(basePath.value()[0] + methodMapping.value()[0]);
91 if (path.equals(basePath.value()[0] + methodMapping.value()[0])) {
97 throw new NoSuchElementException(path + " in " + paths);