diff options
author | 2018-11-28 22:42:51 +0000 | |
---|---|---|
committer | 2018-11-28 22:42:51 +0000 | |
commit | 704b6ab2b6b919993d6b048bea821e229cc12b67 (patch) | |
tree | 03d9f930261a043c052af8ae75a935a48809736a /pomba/network-discovery/src/test | |
parent | 33d1f5de2733b1b0c27082b5cf4fb491fbc9cf35 (diff) | |
parent | 9a0c9790a1dc31dcc2db199b6f0ff76e796c2597 (diff) |
Merge "ND get VM info from Openstack"
Diffstat (limited to 'pomba/network-discovery/src/test')
7 files changed, 427 insertions, 531 deletions
diff --git a/pomba/network-discovery/src/test/java/org/onap/sdnc/apps/pomba/networkdiscovery/unittest/service/NetworkDiscoveryTest.java b/pomba/network-discovery/src/test/java/org/onap/sdnc/apps/pomba/networkdiscovery/unittest/service/NetworkDiscoveryTest.java index 1829e6e..18a0a04 100644 --- a/pomba/network-discovery/src/test/java/org/onap/sdnc/apps/pomba/networkdiscovery/unittest/service/NetworkDiscoveryTest.java +++ b/pomba/network-discovery/src/test/java/org/onap/sdnc/apps/pomba/networkdiscovery/unittest/service/NetworkDiscoveryTest.java @@ -20,7 +20,7 @@ package org.onap.sdnc.apps.pomba.networkdiscovery.unittest.service; import static com.github.tomakehurst.wiremock.client.WireMock.get; import static com.github.tomakehurst.wiremock.client.WireMock.ok; -import static com.github.tomakehurst.wiremock.client.WireMock.okTextXml; +import static com.github.tomakehurst.wiremock.client.WireMock.okJson; import static com.github.tomakehurst.wiremock.client.WireMock.post; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; import static org.junit.Assert.assertEquals; @@ -28,13 +28,16 @@ import static org.junit.Assert.assertNull; import static org.junit.Assert.assertTrue; import static org.junit.Assert.fail; +import com.bazaarvoice.jolt.JsonUtils; import com.fasterxml.jackson.databind.AnnotationIntrospector; import com.fasterxml.jackson.databind.ObjectMapper; import com.fasterxml.jackson.databind.type.TypeFactory; import com.fasterxml.jackson.module.jaxb.JaxbAnnotationIntrospector; +import com.github.tomakehurst.wiremock.client.WireMock; import com.github.tomakehurst.wiremock.junit.WireMockRule; import com.github.tomakehurst.wiremock.stubbing.ServeEvent; import com.github.tomakehurst.wiremock.verification.LoggedRequest; + import java.net.URISyntaxException; import java.text.MessageFormat; import java.util.ArrayList; @@ -42,16 +45,19 @@ import java.util.Arrays; import java.util.Base64; import java.util.List; import java.util.UUID; + import javax.servlet.http.HttpServletRequest; import javax.ws.rs.core.HttpHeaders; import javax.ws.rs.core.Response; import javax.ws.rs.core.Response.Status; + import org.eclipse.jetty.util.security.Password; import org.junit.After; import org.junit.Before; import org.junit.Rule; import org.junit.Test; import org.junit.runner.RunWith; +import org.mockito.Mockito; import org.onap.logging.ref.slf4j.ONAPLogConstants; import org.onap.pomba.common.datatypes.DataQuality; import org.onap.sdnc.apps.pomba.networkdiscovery.datamodel.Attribute; @@ -64,7 +70,6 @@ import org.springframework.boot.autoconfigure.EnableAutoConfiguration; import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration; import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.core.env.Environment; import org.springframework.test.context.TestPropertySource; import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; import org.springframework.test.context.web.WebAppConfiguration; @@ -73,11 +78,13 @@ import org.springframework.test.context.web.WebAppConfiguration; @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class }) @WebAppConfiguration @SpringBootTest -@TestPropertySource(properties = { "enricher.url=http://localhost:9505", +@TestPropertySource(properties = { "openstack.type.vserver.url=http://localhost:8774/v2.1/servers/{0}", + "openstack.identity.url=http://localhost:5000/v3/auth/tokens", "enricher.keyStorePath=src/test/resources/client-cert-onap.p12", "enricher.keyStorePassword=OBF:1y0q1uvc1uum1uvg1pil1pjl1uuq1uvk1uuu1y10", "basicAuth.username=admin", "basicAuth.password=OBF:1u2a1toa1w8v1tok1u30" }) + public class NetworkDiscoveryTest { private static final String V1 = "v1"; private static final String APP = "junit"; @@ -87,11 +94,12 @@ public class NetworkDiscoveryTest { private static final String AUTH = "Basic " + Base64.getEncoder().encodeToString(( "admin:" + Password.deobfuscate("OBF:1u2a1toa1w8v1tok1u30")).getBytes()); - @Autowired - private Environment environment; @Rule - public WireMockRule enricherRule = new WireMockRule(wireMockConfig().port(9505)); + public WireMockRule identityRule = new WireMockRule(wireMockConfig().port(5000)); + + @Rule + public WireMockRule openstackRule = new WireMockRule(wireMockConfig().port(8774)); @Rule public WireMockRule callbackRule = new WireMockRule(wireMockConfig().dynamicPort()); @@ -101,7 +109,10 @@ public class NetworkDiscoveryTest { private String transactionId = UUID.randomUUID().toString(); private String requestId = UUID.randomUUID().toString(); - private HttpServletRequest httpRequest = new TestHttpServletRequest(); + private HttpServletRequest httpRequest = Mockito.mock(HttpServletRequest.class); + + private static final String TEST_RESOURCES = "src/test/resources/jolt/"; + public NetworkDiscoveryTest() throws URISyntaxException { @@ -139,6 +150,18 @@ public class NetworkDiscoveryTest { } @Test + public void testNoVersion() throws Exception { + // no Authorization header + String authorization = "Basic " + Base64.getEncoder().encodeToString("aaa:bbb".getBytes()); + List<String> resourceIds = Arrays.asList(UUID.randomUUID().toString()); + Response response = this.service.findbyResourceIdAndType(this.httpRequest, null, authorization, APP, + this.transactionId, this.requestId, RESOURCE_TYPE_VSERVER, resourceIds, getCallbackUrl()); + assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus()); + // should get WWW-Authenticate header in response + assertTrue(((String) response.getEntity()).contains("version")); + } + + @Test public void testVerifyAppId() throws Exception { // no X-FromAppId header List<String> resourceIds = Arrays.asList(UUID.randomUUID().toString()); @@ -150,7 +173,6 @@ public class NetworkDiscoveryTest { @Test public void testVerifyRequestId() throws Exception { - // no X-FromAppId header List<String> resourceIds = Arrays.asList(UUID.randomUUID().toString()); Response response = this.service.findbyResourceIdAndType(this.httpRequest, V1, AUTH, APP, this.transactionId, null, RESOURCE_TYPE_VSERVER, resourceIds, getCallbackUrl()); @@ -160,7 +182,6 @@ public class NetworkDiscoveryTest { @Test public void testVerifyNotificationUrl() throws Exception { - // no X-FromAppId header List<String> resourceIds = Arrays.asList(UUID.randomUUID().toString()); Response response = this.service.findbyResourceIdAndType(this.httpRequest, V1, AUTH, APP, this.transactionId, this.requestId, RESOURCE_TYPE_VSERVER, resourceIds, null); @@ -200,31 +221,31 @@ public class NetworkDiscoveryTest { } @Test + public void testVerifyInternalError() throws Exception { + // no request + List<String> resourceIds = Arrays.asList(UUID.randomUUID().toString()); + Response response = this.service.findbyResourceIdAndType(null, V1, AUTH, APP, this.transactionId, + this.requestId, null, resourceIds, getCallbackUrl()); + assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), response.getStatus()); + } + + @Test public void testDiscoverVserver() throws Exception { String vserverId = UUID.randomUUID().toString(); - String resourcePath = MessageFormat.format( - this.environment.getProperty("enricher.type.vserver.url"), - new Object[] { vserverId }); - - String enricherPayload = String.format("<vserver xmlns=\"http://org.onap.aai.inventory/v11\">\r\n" - + " <vserver-id>%s</vserver-id>\r\n" - + " <power-state>1</power-state>\r\n" - + " <locked>true</locked>\r\n" - + " <hostname>10.147.112.48</hostname>\r\n" - + " <vm-state>active</vm-state>\r\n" - + " <status>ACTIVE</status>\r\n" - + " <host-status>UNKNOWN</host-status>\r\n" - + " <updated>2017-11-20T04:26:13Z</updated>\r\n" - + " <disk-allocation-gb>.010</disk-allocation-gb>\r\n" - + " <memory-usage-mb>null</memory-usage-mb>\r\n" - + " <cpu-util-percent>.043</cpu-util-percent>\r\n" - + " <retrieval-timestamp>2018-06-27 19:41:49 +0000</retrieval-timestamp>\r\n" - + "</vserver>", - vserverId); - - this.enricherRule.stubFor(get(resourcePath).willReturn(okTextXml(enricherPayload))); + String resourcePath = MessageFormat.format("/v2.1/servers/{0}", + new Object[] { vserverId }); + String identityPath = "/v3/auth/tokens"; + + Object sourceObject = JsonUtils.filepathToObject(TEST_RESOURCES + "vserver-input.json"); + + String openstackApiResponse = JsonUtils.toJsonString(sourceObject); + + this.openstackRule.stubFor(get(resourcePath).willReturn(okJson(openstackApiResponse))); + + this.identityRule.stubFor(post(identityPath).willReturn(okJson("{}").withHeader("X-Subject-Token", "tokenId"))); + this.callbackRule.stubFor(post(CALLBACK_PATH).willReturn(ok("Acknowledged"))); Response response = this.service.findbyResourceIdAndType(this.httpRequest, V1, AUTH, APP, null, this.requestId, @@ -259,9 +280,57 @@ public class NetworkDiscoveryTest { verifyAttribute(vserver.getAttributeList(), "status", "ACTIVE"); verifyAttribute(vserver.getAttributeList(), "inMaintenance", "true"); - verifyAttribute(vserver.getAttributeList(), "hostName", "10.147.112.48"); + verifyAttribute(vserver.getAttributeList(), "hostname", "norm-bouygues"); verifyAttribute(vserver.getAttributeList(), "vmState", "active"); } + + @Test + public void testDiscoverVserverFailure() throws Exception { + String vserverId = UUID.randomUUID().toString(); + + String resourcePath = MessageFormat.format("/v2.1/servers/{0}", + new Object[] { vserverId }); + + String identityPath = "/v3/auth/tokens"; + + this.openstackRule.stubFor(get(resourcePath).willReturn(WireMock.notFound())); + + this.identityRule.stubFor(post(identityPath).willReturn(okJson("{}").withHeader("X-Subject-Token", "tokenId"))); + + this.callbackRule.stubFor(post(CALLBACK_PATH).willReturn(ok("Acknowledged"))); + + Response response = this.service.findbyResourceIdAndType(this.httpRequest, V1, AUTH, APP, null, this.requestId, + RESOURCE_TYPE_VSERVER, Arrays.asList(vserverId), getCallbackUrl()); + + assertEquals(Status.OK.getStatusCode(), response.getStatus()); + NetworkDiscoveryResponse entity = (NetworkDiscoveryResponse) response.getEntity(); + assertEquals(requestId, entity.getRequestId()); + assertEquals(Status.ACCEPTED.getStatusCode(), entity.getCode().intValue()); + assertEquals(Boolean.FALSE, entity.getAckFinalIndicator()); + + List<ServeEvent> events = waitForRequests(this.callbackRule, 1, 10); + LoggedRequest notificationRequest = events.get(0).getRequest(); + assertEquals(AUTH, notificationRequest.getHeader(HttpHeaders.AUTHORIZATION)); + String notificationJson = notificationRequest.getBodyAsString(); + + ObjectMapper mapper = new ObjectMapper(); + AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance()); + mapper.setAnnotationIntrospector(introspector); + NetworkDiscoveryNotification notification = mapper.readValue(notificationJson, + NetworkDiscoveryNotification.class); + + assertEquals(requestId, notification.getRequestId()); + assertEquals(Status.OK.getStatusCode(), notification.getCode().intValue()); + assertEquals(Boolean.TRUE, notification.getAckFinalIndicator()); + + assertEquals(1, notification.getResources().size()); + Resource vserver = notification.getResources().get(0); + assertEquals(vserverId, vserver.getId()); + assertEquals("vserver", vserver.getType()); + assertEquals(DataQuality.Status.error, vserver.getDataQuality().getStatus()); + assertNull(vserver.getAttributeList()); + } + /** * Verify API returns a final response indicating no discovery possible. @@ -276,9 +345,47 @@ public class NetworkDiscoveryTest { assertEquals(Status.OK.getStatusCode(), response.getStatus()); NetworkDiscoveryResponse entity = (NetworkDiscoveryResponse) response.getEntity(); + System.err.println("entity:" + entity); assertEquals(Boolean.TRUE, entity.getAckFinalIndicator()); assertEquals(Status.NO_CONTENT.getStatusCode(), entity.getCode().intValue()); } + + @Test + public void testLoginFailure() throws Exception { + String vserverId = UUID.randomUUID().toString(); + + String identityPath = "/v3/auth/tokens"; + + this.identityRule.stubFor(post(identityPath).willReturn(WireMock.unauthorized())); + + this.callbackRule.stubFor(post(CALLBACK_PATH).willReturn(ok("Acknowledged"))); + + Response response = this.service.findbyResourceIdAndType(this.httpRequest, V1, AUTH, APP, null, this.requestId, + RESOURCE_TYPE_VSERVER, Arrays.asList(vserverId), getCallbackUrl()); + + assertEquals(Status.OK.getStatusCode(), response.getStatus()); + NetworkDiscoveryResponse entity = (NetworkDiscoveryResponse) response.getEntity(); + assertEquals(requestId, entity.getRequestId()); + assertEquals(Status.ACCEPTED.getStatusCode(), entity.getCode().intValue()); + assertEquals(Boolean.FALSE, entity.getAckFinalIndicator()); + + List<ServeEvent> events = waitForRequests(this.callbackRule, 1, 10); + LoggedRequest notificationRequest = events.get(0).getRequest(); + assertEquals(AUTH, notificationRequest.getHeader(HttpHeaders.AUTHORIZATION)); + String notificationJson = notificationRequest.getBodyAsString(); + + ObjectMapper mapper = new ObjectMapper(); + AnnotationIntrospector introspector = new JaxbAnnotationIntrospector(TypeFactory.defaultInstance()); + mapper.setAnnotationIntrospector(introspector); + NetworkDiscoveryNotification notification = mapper.readValue(notificationJson, + NetworkDiscoveryNotification.class); + + assertEquals(requestId, notification.getRequestId()); + assertEquals(Status.INTERNAL_SERVER_ERROR.getStatusCode(), notification.getCode().intValue()); + assertEquals(Boolean.TRUE, notification.getAckFinalIndicator()); + assertNull(notification.getResources()); + } + private void verifyAttribute(List<Attribute> attributeList, String attrName, String attrValue) { for (Attribute attr : attributeList) { diff --git a/pomba/network-discovery/src/test/java/org/onap/sdnc/apps/pomba/networkdiscovery/unittest/service/TestHttpServletRequest.java b/pomba/network-discovery/src/test/java/org/onap/sdnc/apps/pomba/networkdiscovery/unittest/service/TestHttpServletRequest.java deleted file mode 100644 index 671033b..0000000 --- a/pomba/network-discovery/src/test/java/org/onap/sdnc/apps/pomba/networkdiscovery/unittest/service/TestHttpServletRequest.java +++ /dev/null @@ -1,500 +0,0 @@ -/* - * ============LICENSE_START=================================================== - * Copyright (c) 2018 Amdocs - * ============================================================================ - * 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. - * ============LICENSE_END===================================================== - */ - -package org.onap.sdnc.apps.pomba.networkdiscovery.unittest.service; - -import java.io.BufferedReader; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.security.Principal; -import java.util.Collection; -import java.util.Enumeration; -import java.util.Locale; -import java.util.Map; -import javax.servlet.AsyncContext; -import javax.servlet.DispatcherType; -import javax.servlet.RequestDispatcher; -import javax.servlet.ServletContext; -import javax.servlet.ServletException; -import javax.servlet.ServletInputStream; -import javax.servlet.ServletRequest; -import javax.servlet.ServletResponse; -import javax.servlet.http.Cookie; -import javax.servlet.http.HttpServletRequest; -import javax.servlet.http.HttpServletResponse; -import javax.servlet.http.HttpSession; -import javax.servlet.http.HttpUpgradeHandler; -import javax.servlet.http.Part; - -public class TestHttpServletRequest implements HttpServletRequest { - @Override - public String getHeader(String name) { - return null; - } - - @Override - public String getRemoteAddr() { - return "localhost"; - } - - @Override - public String getServerName() { - return "localhost"; - } - - @Override - public String getRequestURI() { - return "/test"; - } - - - @Override - public Object getAttribute(String name) { - // TODO Implement getAttribute - throw new UnsupportedOperationException("getAttribute"); - } - - @Override - public Enumeration<String> getAttributeNames() { - // TODO Implement getAttributeNames - throw new UnsupportedOperationException("getAttributeNames"); - } - - @Override - public String getCharacterEncoding() { - // TODO Implement getCharacterEncoding - throw new UnsupportedOperationException("getCharacterEncoding"); - } - - @Override - public void setCharacterEncoding(String env) throws UnsupportedEncodingException { - // TODO Implement setCharacterEncoding - throw new UnsupportedOperationException("setCharacterEncoding"); - } - - @Override - public int getContentLength() { - // TODO Implement getContentLength - throw new UnsupportedOperationException("getContentLength"); - } - - @Override - public long getContentLengthLong() { - // TODO Implement getContentLengthLong - throw new UnsupportedOperationException("getContentLengthLong"); - } - - @Override - public String getContentType() { - // TODO Implement getContentType - throw new UnsupportedOperationException("getContentType"); - } - - @Override - public ServletInputStream getInputStream() throws IOException { - // TODO Implement getInputStream - throw new UnsupportedOperationException("getInputStream"); - } - - @Override - public String getParameter(String name) { - // TODO Implement getParameter - throw new UnsupportedOperationException("getParameter"); - } - - @Override - public Enumeration<String> getParameterNames() { - // TODO Implement getParameterNames - throw new UnsupportedOperationException("getParameterNames"); - } - - @Override - public String[] getParameterValues(String name) { - // TODO Implement getParameterValues - throw new UnsupportedOperationException("getParameterValues"); - } - - @Override - public Map<String, String[]> getParameterMap() { - // TODO Implement getParameterMap - throw new UnsupportedOperationException("getParameterMap"); - } - - @Override - public String getProtocol() { - // TODO Implement getProtocol - throw new UnsupportedOperationException("getProtocol"); - } - - @Override - public String getScheme() { - // TODO Implement getScheme - throw new UnsupportedOperationException("getScheme"); - } - - @Override - public int getServerPort() { - // TODO Implement getServerPort - throw new UnsupportedOperationException("getServerPort"); - } - - @Override - public BufferedReader getReader() throws IOException { - // TODO Implement getReader - throw new UnsupportedOperationException("getReader"); - } - - @Override - public String getRemoteHost() { - // TODO Implement getRemoteHost - throw new UnsupportedOperationException("getRemoteHost"); - } - - @Override - public void setAttribute(String name, Object obj) { - // TODO Implement setAttribute - throw new UnsupportedOperationException("setAttribute"); - } - - @Override - public void removeAttribute(String name) { - // TODO Implement removeAttribute - throw new UnsupportedOperationException("removeAttribute"); - } - - @Override - public Locale getLocale() { - // TODO Implement getLocale - throw new UnsupportedOperationException("getLocale"); - } - - @Override - public Enumeration<Locale> getLocales() { - // TODO Implement getLocales - throw new UnsupportedOperationException("getLocales"); - } - - @Override - public boolean isSecure() { - // TODO Implement isSecure - throw new UnsupportedOperationException("isSecure"); - } - - @Override - public RequestDispatcher getRequestDispatcher(String path) { - // TODO Implement getRequestDispatcher - throw new UnsupportedOperationException("getRequestDispatcher"); - } - - @Override - public String getRealPath(String path) { - // TODO Implement getRealPath - throw new UnsupportedOperationException("getRealPath"); - } - - @Override - public int getRemotePort() { - // TODO Implement getRemotePort - throw new UnsupportedOperationException("getRemotePort"); - } - - @Override - public String getLocalName() { - // TODO Implement getLocalName - throw new UnsupportedOperationException("getLocalName"); - } - - @Override - public String getLocalAddr() { - // TODO Implement getLocalAddr - throw new UnsupportedOperationException("getLocalAddr"); - } - - @Override - public int getLocalPort() { - // TODO Implement getLocalPort - throw new UnsupportedOperationException("getLocalPort"); - } - - @Override - public ServletContext getServletContext() { - // TODO Implement getServletContext - throw new UnsupportedOperationException("getServletContext"); - } - - @Override - public AsyncContext startAsync() throws IllegalStateException { - // TODO Implement startAsync - throw new UnsupportedOperationException("startAsync"); - } - - @Override - public AsyncContext startAsync(ServletRequest servletRequest, ServletResponse servletResponse) - throws IllegalStateException { - // TODO Implement startAsync - throw new UnsupportedOperationException("startAsync"); - } - - @Override - public boolean isAsyncStarted() { - // TODO Implement isAsyncStarted - throw new UnsupportedOperationException("isAsyncStarted"); - } - - @Override - public boolean isAsyncSupported() { - // TODO Implement isAsyncSupported - throw new UnsupportedOperationException("isAsyncSupported"); - } - - @Override - public AsyncContext getAsyncContext() { - // TODO Implement getAsyncContext - throw new UnsupportedOperationException("getAsyncContext"); - } - - @Override - public DispatcherType getDispatcherType() { - // TODO Implement getDispatcherType - throw new UnsupportedOperationException("getDispatcherType"); - } - - @Override - public String getAuthType() { - // TODO Implement getAuthType - throw new UnsupportedOperationException("getAuthType"); - } - - @Override - public Cookie[] getCookies() { - // TODO Implement getCookies - throw new UnsupportedOperationException("getCookies"); - } - - @Override - public long getDateHeader(String name) { - // TODO Implement getDateHeader - throw new UnsupportedOperationException("getDateHeader"); - } - - @Override - public Enumeration<String> getHeaders(String name) { - // TODO Implement getHeaders - throw new UnsupportedOperationException("getHeaders"); - } - - @Override - public Enumeration<String> getHeaderNames() { - // TODO Implement getHeaderNames - throw new UnsupportedOperationException("getHeaderNames"); - } - - @Override - public int getIntHeader(String name) { - // TODO Implement getIntHeader - throw new UnsupportedOperationException("getIntHeader"); - } - - @Override - public String getMethod() { - // TODO Implement getMethod - throw new UnsupportedOperationException("getMethod"); - } - - @Override - public String getPathInfo() { - // TODO Implement getPathInfo - throw new UnsupportedOperationException("getPathInfo"); - } - - @Override - public String getPathTranslated() { - // TODO Implement getPathTranslated - throw new UnsupportedOperationException("getPathTranslated"); - } - - @Override - public String getContextPath() { - // TODO Implement getContextPath - throw new UnsupportedOperationException("getContextPath"); - } - - @Override - public String getQueryString() { - // TODO Implement getQueryString - throw new UnsupportedOperationException("getQueryString"); - } - - @Override - public String getRemoteUser() { - // TODO Implement getRemoteUser - throw new UnsupportedOperationException("getRemoteUser"); - } - - @Override - public boolean isUserInRole(String role) { - // TODO Implement isUserInRole - throw new UnsupportedOperationException("isUserInRole"); - } - - @Override - public Principal getUserPrincipal() { - // TODO Implement getUserPrincipal - throw new UnsupportedOperationException("getUserPrincipal"); - } - - @Override - public String getRequestedSessionId() { - // TODO Implement getRequestedSessionId - throw new UnsupportedOperationException("getRequestedSessionId"); - } - - @Override - public StringBuffer getRequestURL() { - // TODO Implement getRequestURL - throw new UnsupportedOperationException("getRequestURL"); - } - - @Override - public String getServletPath() { - // TODO Implement getServletPath - throw new UnsupportedOperationException("getServletPath"); - } - - @Override - public HttpSession getSession(boolean create) { - // TODO Implement getSession - throw new UnsupportedOperationException("getSession"); - } - - @Override - public HttpSession getSession() { - // TODO Implement getSession - throw new UnsupportedOperationException("getSession"); - } - - @Override - public String changeSessionId() { - // TODO Implement changeSessionId - throw new UnsupportedOperationException("changeSessionId"); - } - - @Override - public boolean isRequestedSessionIdValid() { - // TODO Implement isRequestedSessionIdValid - throw new UnsupportedOperationException("isRequestedSessionIdValid"); - } - - @Override - public boolean isRequestedSessionIdFromCookie() { - // TODO Implement isRequestedSessionIdFromCookie - throw new UnsupportedOperationException("isRequestedSessionIdFromCookie"); - } - - @Override - public boolean isRequestedSessionIdFromURL() { - // TODO Implement isRequestedSessionIdFromURL - throw new UnsupportedOperationException("isRequestedSessionIdFromURL"); - } - - @Override - public boolean isRequestedSessionIdFromUrl() { - // TODO Implement isRequestedSessionIdFromUrl - throw new UnsupportedOperationException("isRequestedSessionIdFromUrl"); - } - - @Override - public boolean authenticate(HttpServletResponse response) throws IOException, ServletException { - // TODO Implement authenticate - throw new UnsupportedOperationException("authenticate"); - } - - @Override - public void login(String username, String password) throws ServletException { - // TODO Implement login - throw new UnsupportedOperationException("login"); - } - - @Override - public void logout() throws ServletException { - // TODO Implement logout - throw new UnsupportedOperationException("logout"); - } - - @Override - public Collection<Part> getParts() throws IOException, ServletException { - // TODO Implement getParts - throw new UnsupportedOperationException("getParts"); - } - - @Override - public Part getPart(String name) throws IOException, ServletException { - // TODO Implement getPart - throw new UnsupportedOperationException("getPart"); - } - - @Override - public <T extends HttpUpgradeHandler> T upgrade(Class<T> httpUpgradeHandlerClass) - throws IOException, ServletException { - // TODO Implement upgrade - throw new UnsupportedOperationException("upgrade"); - } - ///////////////////////////////////////////////////////////////////////////// - // Constants - ///////////////////////////////////////////////////////////////////////////// - - ///////////////////////////////////////////////////////////////////////////// - // Class variables - ///////////////////////////////////////////////////////////////////////////// - - ///////////////////////////////////////////////////////////////////////////// - // Instance variables - ///////////////////////////////////////////////////////////////////////////// - - ///////////////////////////////////////////////////////////////////////////// - // Constructors - ///////////////////////////////////////////////////////////////////////////// - - ///////////////////////////////////////////////////////////////////////////// - // Public methods - ///////////////////////////////////////////////////////////////////////////// - - ///////////////////////////////////////////////////////////////////////////// - // [interface name] implementation - ///////////////////////////////////////////////////////////////////////////// - - ///////////////////////////////////////////////////////////////////////////// - // [super class] override methods - ///////////////////////////////////////////////////////////////////////////// - - ///////////////////////////////////////////////////////////////////////////// - // Package protected methods - ///////////////////////////////////////////////////////////////////////////// - - ///////////////////////////////////////////////////////////////////////////// - // Protected methods - ///////////////////////////////////////////////////////////////////////////// - - ///////////////////////////////////////////////////////////////////////////// - // Private methods - ///////////////////////////////////////////////////////////////////////////// - - ///////////////////////////////////////////////////////////////////////////// - // Inner classes - ///////////////////////////////////////////////////////////////////////////// -} diff --git a/pomba/network-discovery/src/test/java/org/onap/sdnc/apps/pomba/networkdiscovery/unittest/service/util/TransformationUtilTest.java b/pomba/network-discovery/src/test/java/org/onap/sdnc/apps/pomba/networkdiscovery/unittest/service/util/TransformationUtilTest.java new file mode 100644 index 0000000..968c1cf --- /dev/null +++ b/pomba/network-discovery/src/test/java/org/onap/sdnc/apps/pomba/networkdiscovery/unittest/service/util/TransformationUtilTest.java @@ -0,0 +1,136 @@ +/*
+ * ============LICENSE_START===================================================
+ * Copyright (c) 2018 Amdocs
+ * ============================================================================
+ * 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.
+ * ============LICENSE_END=====================================================
+ */
+
+package org.onap.sdnc.apps.pomba.networkdiscovery.unittest.service.util;
+
+import static org.junit.Assert.assertThat;
+
+import com.bazaarvoice.jolt.JsonUtils;
+import com.bazaarvoice.jolt.exception.JsonUnmarshalException;
+
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.hamcrest.CoreMatchers;
+import org.junit.Assert;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onap.sdnc.apps.pomba.networkdiscovery.datamodel.Attribute;
+import org.onap.sdnc.apps.pomba.networkdiscovery.service.util.TransformationUtil;
+
+public class TransformationUtilTest {
+
+ private static final String TEST_RESOURCES = "src/test/resources/jolt/";
+
+ @Rule
+ public ExpectedException expectedEx = ExpectedException.none();
+
+ @Test
+ public void testTransformVServer() {
+
+ Object sourceObject = JsonUtils.filepathToObject(TEST_RESOURCES + "vserver-input.json");
+
+ String resultJson = TransformationUtil.transform(JsonUtils.toJsonString(sourceObject), "vserver");
+
+ Object expectedObject = JsonUtils.filepathToObject(TEST_RESOURCES + "vserver-expected.json");
+
+ Assert.assertEquals("Json transformation result does not match expected content",
+ JsonUtils.toPrettyJsonString(expectedObject),
+ JsonUtils.toPrettyJsonString(JsonUtils.jsonToObject(resultJson)));
+
+ }
+
+ @Test
+ public void testTransformL3Network() {
+
+ Object sourceObject = JsonUtils.filepathToObject(TEST_RESOURCES + "l3network-input.json");
+ String resultJson = TransformationUtil.transform(JsonUtils.toJsonString(sourceObject), "l3-network");
+
+ Object expectedObject = JsonUtils.filepathToObject(TEST_RESOURCES + "l3network-expected.json");
+
+ Assert.assertEquals("Json transformation result does not match expected content",
+ JsonUtils.toPrettyJsonString(expectedObject),
+ JsonUtils.toPrettyJsonString(JsonUtils.jsonToObject(resultJson)));
+
+ }
+
+ @Test
+ public void testTransformFailureFileNotFound() {
+
+ expectedEx.expect(RuntimeException.class);
+ expectedEx.expectMessage("Unable to load JSON file");
+
+ TransformationUtil.transform("{}", "foobar");
+ }
+
+ @Test
+ public void testTransformFailureInvalidInputJson() {
+
+ expectedEx.expect(JsonUnmarshalException.class);
+ expectedEx.expectMessage("Unable to unmarshal JSON");
+
+ TransformationUtil.transform("xxx", "foobar");
+ }
+
+ @Test
+ public void testToAttributeList() {
+ Map<String, String> expectedAttributes = new HashMap<String, String>();
+ expectedAttributes.put("name", "norm_bouygues");
+ expectedAttributes.put("hostId", "ea1660efbbedda164379afacdc622305c4b88cebfb84119472d286a8");
+ expectedAttributes.put("hostStatus", "UP");
+ expectedAttributes.put("id", "2c311eae-f542-4173-8a01-582922abd495");
+ expectedAttributes.put("status", "ACTIVE");
+ expectedAttributes.put("vmState", "active");
+ expectedAttributes.put("hostname", "norm-bouygues");
+ expectedAttributes.put("inMaintenance", "true");
+ expectedAttributes.put("imageId", "c0022890-d91f-422c-91c5-3866edeae768");
+ expectedAttributes.put("tenantId", "15ad36d394e744838e947ca90609f805");
+ expectedAttributes.put("host", "Setup-NCSO-OTT-E-C2");
+
+ Object inputJson = JsonUtils.filepathToObject(TEST_RESOURCES + "vserver-expected.json");
+ List<Attribute> resultAttributeList = TransformationUtil.toAttributeList(JsonUtils.toJsonString(inputJson));
+
+ Map<String, String> resultAttributes = new HashMap<>();
+
+ for (Attribute attribute : resultAttributeList) {
+ resultAttributes.put(attribute.getName(), attribute.getValue());
+ }
+ assertThat(expectedAttributes, CoreMatchers.is(resultAttributes));
+ }
+
+ @Test
+ public void testToAttributeListNullJsonValue() {
+ Map<String, String> expectedAttributes = new HashMap<String, String>();
+ expectedAttributes.put("name", "");
+
+ String inputJson = "{\"server\": { \"name\": null }}";
+
+ List<Attribute> resultAttributeList = TransformationUtil.toAttributeList(inputJson);
+
+ Map<String, String> resultAttributes = new HashMap<>();
+
+ for (Attribute attribute : resultAttributeList) {
+ resultAttributes.put(attribute.getName(), attribute.getValue());
+ }
+ // assertThat(expectedAttributes, resultAttributes);
+ assertThat(expectedAttributes, CoreMatchers.is(resultAttributes));
+ }
+
+}
diff --git a/pomba/network-discovery/src/test/resources/jolt/l3network-expected.json b/pomba/network-discovery/src/test/resources/jolt/l3network-expected.json new file mode 100644 index 0000000..be4ad9d --- /dev/null +++ b/pomba/network-discovery/src/test/resources/jolt/l3network-expected.json @@ -0,0 +1,14 @@ +{
+ "l3-network": {
+ "id": "8df84f0a-737a-4628-be9c-c3c78454f9d9",
+ "name": "NET_503",
+ "AdminState": true,
+ "sharedNetwork": true,
+ "status": "ACTIVE",
+ "subnets": [
+ "d0e6ecad-7bc9-4138-b9e8-e742bdf9afd5"
+ ],
+ "tenantId": "c44b872f6830498b88c4989d67b2a6b7",
+ "host-status": "UNKNOWN"
+ }
+}
diff --git a/pomba/network-discovery/src/test/resources/jolt/l3network-input.json b/pomba/network-discovery/src/test/resources/jolt/l3network-input.json new file mode 100644 index 0000000..ebd9504 --- /dev/null +++ b/pomba/network-discovery/src/test/resources/jolt/l3network-input.json @@ -0,0 +1,32 @@ +{ + "network": { + "provider:physical_network": "public", + "ipv6_address_scope": null, + "revision_number": 9, + "port_security_enabled": true, + "mtu": 1500, + "id": "8df84f0a-737a-4628-be9c-c3c78454f9d9", + "router:external": true, + "availability_zone_hints": [], + "availability_zones": [ + "nova" + ], + "ipv4_address_scope": null, + "shared": true, + "project_id": "c44b872f6830498b88c4989d67b2a6b7", + "status": "ACTIVE", + "subnets": [ + "d0e6ecad-7bc9-4138-b9e8-e742bdf9afd5" + ], + "description": "", + "tags": [], + "updated_at": "2018-10-03T21:15:10Z", + "is_default": false, + "provider:segmentation_id": 503, + "name": "NET_503", + "admin_state_up": true, + "tenant_id": "c44b872f6830498b88c4989d67b2a6b7", + "created_at": "2018-03-20T16:49:01Z", + "provider:network_type": "vlan" + } +}
\ No newline at end of file diff --git a/pomba/network-discovery/src/test/resources/jolt/vserver-expected.json b/pomba/network-discovery/src/test/resources/jolt/vserver-expected.json new file mode 100644 index 0000000..53d4a79 --- /dev/null +++ b/pomba/network-discovery/src/test/resources/jolt/vserver-expected.json @@ -0,0 +1,15 @@ +{
+ "vserver" : {
+ "id" : "2c311eae-f542-4173-8a01-582922abd495",
+ "name" : "norm_bouygues",
+ "inMaintenance" : true,
+ "hostname" : "norm-bouygues",
+ "imageId" : "c0022890-d91f-422c-91c5-3866edeae768",
+ "status" : "ACTIVE",
+ "vmState" : "active",
+ "tenantId" : "15ad36d394e744838e947ca90609f805",
+ "hostId" : "ea1660efbbedda164379afacdc622305c4b88cebfb84119472d286a8",
+ "host" : "Setup-NCSO-OTT-E-C2",
+ "hostStatus" : "UP"
+ }
+}
\ No newline at end of file diff --git a/pomba/network-discovery/src/test/resources/jolt/vserver-input.json b/pomba/network-discovery/src/test/resources/jolt/vserver-input.json new file mode 100644 index 0000000..faa7ba5 --- /dev/null +++ b/pomba/network-discovery/src/test/resources/jolt/vserver-input.json @@ -0,0 +1,92 @@ +{ + "server": { + "OS-EXT-STS:task_state": null, + "addresses": { + "NET_502": [ + { + "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:a5:c5:19", + "version": 4, + "addr": "10.69.36.133", + "OS-EXT-IPS:type": "fixed" + }, + { + "OS-EXT-IPS-MAC:mac_addr": "fa:16:3e:50:05:f4", + "version": 4, + "addr": "10.69.36.137", + "OS-EXT-IPS:type": "fixed" + } + ] + }, + "links": [ + { + "href": "http://10.69.36.11:8774/v2.1/servers/2c311eae-f542-4173-8a01-582922abd495", + "rel": "self" + }, + { + "href": "http://10.69.36.11:8774/servers/2c311eae-f542-4173-8a01-582922abd495", + "rel": "bookmark" + } + ], + "image": { + "id": "c0022890-d91f-422c-91c5-3866edeae768", + "links": [ + { + "href": "http://10.69.36.11:8774/images/c0022890-d91f-422c-91c5-3866edeae768", + "rel": "bookmark" + } + ] + }, + "OS-EXT-SRV-ATTR:user_data": null, + "OS-EXT-STS:vm_state": "active", + "OS-EXT-SRV-ATTR:instance_name": "instance-000009ca", + "OS-EXT-SRV-ATTR:root_device_name": "/dev/vda", + "OS-SRV-USG:launched_at": "2018-08-20T15:10:09.000000", + "flavor": { + "id": "786f55b2-7c94-4089-b89d-96353567c9d8", + "links": [ + { + "href": "http://10.69.36.11:8774/flavors/786f55b2-7c94-4089-b89d-96353567c9d8", + "rel": "bookmark" + } + ] + }, + "id": "2c311eae-f542-4173-8a01-582922abd495", + "security_groups": [ + { + "name": "default" + }, + { + "name": "default" + } + ], + "OS-SRV-USG:terminated_at": null, + "os-extended-volumes:volumes_attached": [], + "user_id": "bdaf4fcde1574450bfa488b3221033d8", + "OS-EXT-SRV-ATTR:hostname": "norm-bouygues", + "OS-DCF:diskConfig": "AUTO", + "accessIPv4": "", + "accessIPv6": "", + "OS-EXT-SRV-ATTR:reservation_id": "r-wo1numj9", + "progress": 0, + "OS-EXT-STS:power_state": 1, + "OS-EXT-AZ:availability_zone": "nova", + "metadata": {}, + "status": "ACTIVE", + "OS-EXT-SRV-ATTR:ramdisk_id": "", + "updated": "2018-11-15T15:28:09Z", + "hostId": "ea1660efbbedda164379afacdc622305c4b88cebfb84119472d286a8", + "OS-EXT-SRV-ATTR:host": "Setup-NCSO-OTT-E-C2", + "description": "norm_bouygues", + "tags": [], + "key_name": "normant-test-key", + "OS-EXT-SRV-ATTR:kernel_id": "", + "locked": true, + "OS-EXT-SRV-ATTR:hypervisor_hostname": "Setup-NCSO-OTT-E-C2", + "name": "norm_bouygues", + "OS-EXT-SRV-ATTR:launch_index": 0, + "created": "2018-08-20T15:09:32Z", + "tenant_id": "15ad36d394e744838e947ca90609f805", + "host_status": "UP", + "config_drive": "" + } +}
\ No newline at end of file |