6096802a3461b8f26c0ca5358058129a94e1f0a8
[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 junit.framework.TestCase;
25 import org.junit.Test;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
27 import org.springframework.web.bind.annotation.RequestMapping;
28 import org.springframework.web.bind.annotation.RequestMethod;
29
30 import java.lang.reflect.Method;
31 import java.util.HashSet;
32 import java.util.Map;
33 import java.util.NoSuchElementException;
34 import java.util.Set;
35
36 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.child;
37 import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil.loadFile;
38
39 public class TestSwaggerDefinitionConsistency extends TestBase {
40
41     public static final HashSet<Class<?>> CLASSES = Sets.newHashSet(LcmApi.class, LcnApi.class, SwaggerApi.class, ConverterApi.class);
42
43     @Test
44     public void test() throws Exception {
45         JsonObject root = new JsonParser().parse(new String(loadFile("self.swagger.json"))).getAsJsonObject();
46         String basePath = root.get("basePath").getAsString();
47         HashMultimap<String, RequestMethod> expectedPaths = HashMultimap.create();
48         for (Map.Entry<String, JsonElement> pathName : child(root, "paths").entrySet()) {
49             JsonObject path = child(child(root, "paths"), pathName.getKey());
50             for (Map.Entry<String, JsonElement> method : path.entrySet()) {
51                 locate(basePath + pathName.getKey());
52                 expectedPaths.put(basePath + pathName.getKey(), RequestMethod.valueOf(method.getKey().toUpperCase()));
53             }
54         }
55
56         for (Class<?> clazz : CLASSES) {
57             RequestMapping currentBasePath = clazz.getAnnotation(RequestMapping.class);
58             for (Method method : clazz.getMethods()) {
59                 RequestMapping methodMapping = method.getAnnotation(RequestMapping.class);
60                 if (methodMapping != null) {
61                     String fPath = currentBasePath.value()[0] + methodMapping.value()[0];
62                     RequestMethod restMethod = methodMapping.method()[0];
63                     Set<RequestMethod> currentMethods = expectedPaths.get(fPath);
64                     if (!currentMethods.contains(restMethod)) {
65                         TestCase.fail("Not documented REST API " + fPath + " " + restMethod + " current " + currentMethods);
66                     }
67                 }
68             }
69         }
70     }
71
72     private void locate(String path) {
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 && path.equals(basePath.value()[0] + methodMapping.value()[0])) {
78                     return;
79                 }
80             }
81         }
82         throw new NoSuchElementException(path);
83     }
84 }