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.JsonObject;
22 import com.google.gson.JsonParser;
23 import junit.framework.TestCase;
24 import org.junit.Test;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
26 import org.springframework.web.bind.annotation.RequestMapping;
27 import org.springframework.web.bind.annotation.RequestMethod;
29 import java.lang.reflect.Method;
30 import java.util.HashSet;
31 import java.util.NoSuchElementException;
34 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.child;
35 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil.loadFile;
37 public class TestSwaggerDefinitionConsistency extends TestBase {
39 public static final HashSet<Class<?>> CLASSES = Sets.newHashSet(LcmApi.class, LcnApi.class, SwaggerApi.class, ConverterApi.class);
42 public void test() throws Exception {
43 JsonObject root = new JsonParser().parse(new String(loadFile("self.swagger.json"))).getAsJsonObject();
44 String basePath = root.get("basePath").getAsString();
45 HashMultimap<String, RequestMethod> expectedPaths = HashMultimap.create();
46 for (String pathName : child(root, "paths").keySet()) {
47 JsonObject path = child(child(root, "paths"), pathName);
48 for (String method : path.keySet()) {
49 locate(basePath + pathName);
50 expectedPaths.put(basePath + pathName, RequestMethod.valueOf(method.toUpperCase()));
54 for (Class<?> clazz : CLASSES) {
55 RequestMapping currentBasePath = clazz.getAnnotation(RequestMapping.class);
56 for (Method method : clazz.getMethods()) {
57 RequestMapping methodMapping = method.getAnnotation(RequestMapping.class);
58 if (methodMapping != null) {
59 String fPath = currentBasePath.value()[0] + methodMapping.value()[0];
60 RequestMethod restMethod = methodMapping.method()[0];
61 Set<RequestMethod> currentMethods = expectedPaths.get(fPath);
62 if (!currentMethods.contains(restMethod)) {
63 TestCase.fail("Not documented REST API" + fPath + " " + restMethod + " current " + currentMethods);
70 private void locate(String path) {
71 for (Class<?> clazz : CLASSES) {
72 RequestMapping basePath = clazz.getAnnotation(RequestMapping.class);
73 for (Method method : clazz.getMethods()) {
74 RequestMapping methodMapping = method.getAnnotation(RequestMapping.class);
75 if (methodMapping != null && path.equals(basePath.value()[0] + methodMapping.value()[0])) {
80 throw new NoSuchElementException(path);