Updating Nokia driver
[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.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;
28
29 import java.lang.reflect.Method;
30 import java.util.HashSet;
31 import java.util.NoSuchElementException;
32 import java.util.Set;
33
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;
36
37 public class TestSwaggerDefinitionConsistency extends TestBase {
38
39     public static final HashSet<Class<?>> CLASSES = Sets.newHashSet(LcmApi.class, LcnApi.class, SwaggerApi.class, ConverterApi.class);
40
41     @Test
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()));
51             }
52         }
53
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);
64                     }
65                 }
66             }
67         }
68     }
69
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])) {
76                     return;
77                 }
78             }
79         }
80         throw new NoSuchElementException(path);
81     }
82 }