d39a14452d19e94d69a2c75d37af7dac2b865e5c
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / test / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / restapi / TestSwaggerDefinitionConsistency.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.restapi;
18
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;
26 import java.util.Map;
27 import java.util.NoSuchElementException;
28 import java.util.Set;
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;
34
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;
37
38 public class TestSwaggerDefinitionConsistency extends TestBase {
39
40     public static final HashSet<Class<?>> CLASSES = Sets.newHashSet(LcmApi.class, LcnApi.class, SwaggerApi.class, ConverterApi.class, SoApi.class);
41
42     @Test
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()));
52             }
53         }
54
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);
65                     }
66                 }
67             }
68         }
69     }
70
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])) {
80                         return;
81                     }
82                 }
83             }
84         }
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])) {
92                         return;
93                     }
94                 }
95             }
96         }
97         throw new NoSuchElementException(path + " in " + paths);
98     }
99 }