/* * Copyright 2016-2017, Nokia Corporation * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.restapi; import com.google.common.collect.HashMultimap; import com.google.common.collect.Sets; import com.google.gson.JsonObject; import com.google.gson.JsonParser; import junit.framework.TestCase; import org.junit.Test; import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import java.lang.reflect.Method; import java.util.HashSet; import java.util.NoSuchElementException; import java.util.Set; import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.CbamUtils.child; import static org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.TestUtil.loadFile; public class TestSwaggerDefinitionConsistency extends TestBase { public static final HashSet> CLASSES = Sets.newHashSet(LcmApi.class, LcnApi.class, SwaggerApi.class, ConverterApi.class); @Test public void test() throws Exception { JsonObject root = new JsonParser().parse(new String(loadFile("self.swagger.json"))).getAsJsonObject(); String basePath = root.get("basePath").getAsString(); HashMultimap expectedPaths = HashMultimap.create(); for (String pathName : child(root, "paths").keySet()) { JsonObject path = child(child(root, "paths"), pathName); for (String method : path.keySet()) { locate(basePath + pathName); expectedPaths.put(basePath + pathName, RequestMethod.valueOf(method.toUpperCase())); } } for (Class clazz : CLASSES) { RequestMapping currentBasePath = clazz.getAnnotation(RequestMapping.class); for (Method method : clazz.getMethods()) { RequestMapping methodMapping = method.getAnnotation(RequestMapping.class); if (methodMapping != null) { String fPath = currentBasePath.value()[0] + methodMapping.value()[0]; RequestMethod restMethod = methodMapping.method()[0]; Set currentMethods = expectedPaths.get(fPath); if (!currentMethods.contains(restMethod)) { TestCase.fail("Not documented REST API" + fPath + " " + restMethod + " current " + currentMethods); } } } } } private void locate(String path) { for (Class clazz : CLASSES) { RequestMapping basePath = clazz.getAnnotation(RequestMapping.class); for (Method method : clazz.getMethods()) { RequestMapping methodMapping = method.getAnnotation(RequestMapping.class); if (methodMapping != null && path.equals(basePath.value()[0] + methodMapping.value()[0])) { return; } } } throw new NoSuchElementException(path); } }