From 99caec109e4ca48cfc7f2e8d9c3f9c948d173801 Mon Sep 17 00:00:00 2001 From: Jakub Dudycz Date: Mon, 19 Mar 2018 09:12:01 +0100 Subject: ProviderOperations unit tests Improved code coverage. Change-Id: I04d0afe9799cc9bce5111173031ba8f9baa3e409 Issue-ID: APPC-745 Signed-off-by: Jakub Dudycz --- .../onap/appc/listener/LCM/impl/ListenerImpl.java | 43 ++++---- .../listener/LCM/operation/ProviderOperations.java | 98 ++++++++++------- .../LCM/operation/ProviderOperationsTest.java | 120 +++++++++++++++++++++ 3 files changed, 195 insertions(+), 66 deletions(-) create mode 100644 appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/LCM/operation/ProviderOperationsTest.java (limited to 'appc-event-listener/appc-event-listener-bundle/src') diff --git a/appc-event-listener/appc-event-listener-bundle/src/main/java/org/onap/appc/listener/LCM/impl/ListenerImpl.java b/appc-event-listener/appc-event-listener-bundle/src/main/java/org/onap/appc/listener/LCM/impl/ListenerImpl.java index 41a6f92ee..a3c5aba91 100644 --- a/appc-event-listener/appc-event-listener-bundle/src/main/java/org/onap/appc/listener/LCM/impl/ListenerImpl.java +++ b/appc-event-listener/appc-event-listener-bundle/src/main/java/org/onap/appc/listener/LCM/impl/ListenerImpl.java @@ -24,24 +24,22 @@ package org.onap.appc.listener.LCM.impl; -import com.fasterxml.jackson.databind.JsonNode; -import org.apache.commons.lang3.StringUtils; -import org.onap.appc.listener.AbstractListener; -import org.onap.appc.listener.ListenerProperties; -import org.onap.appc.listener.LCM.conv.Converter; -import org.onap.appc.listener.LCM.model.DmaapIncomingMessage; -import org.onap.appc.listener.LCM.operation.ProviderOperations; - import com.att.eelf.configuration.EELFLogger; import com.att.eelf.configuration.EELFManager; import com.att.eelf.i18n.EELFResourceManager; - +import com.fasterxml.jackson.databind.JsonNode; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.List; import java.util.TimeZone; import java.util.concurrent.RejectedExecutionException; +import org.apache.commons.lang3.StringUtils; +import org.onap.appc.listener.AbstractListener; +import org.onap.appc.listener.LCM.conv.Converter; +import org.onap.appc.listener.LCM.model.DmaapIncomingMessage; +import org.onap.appc.listener.LCM.operation.ProviderOperations; +import org.onap.appc.listener.ListenerProperties; public class ListenerImpl extends AbstractListener { @@ -55,14 +53,10 @@ public class ListenerImpl extends AbstractListener { super(props); String url = props.getProperty("provider.url"); - LOG.info("DMaaP Provider Endpoint: " + url); - providerOperations = new ProviderOperations(); - providerOperations.setUrl(url); - - // Set Basic Auth String user = props.getProperty("provider.user"); String pass = props.getProperty("provider.pass"); - providerOperations.setAuthentication(user, pass); + providerOperations = new ProviderOperations(url, user, pass); + LOG.info("DMaaP Provider Endpoint: " + url); } @Override @@ -79,8 +73,8 @@ public class ListenerImpl extends AbstractListener { if (executor.getQueue().size() <= QUEUED_MIN) { LOG.debug("DMaaP queue running low. Querying for more jobs"); - - List messages = dmaap.getIncomingEvents(DmaapIncomingMessage.class, QUEUED_MAX); + List messages = dmaap + .getIncomingEvents(DmaapIncomingMessage.class, QUEUED_MAX); LOG.debug(String.format("Read %d messages from dmaap", messages.size())); for (DmaapIncomingMessage incoming : messages) { // Acknowledge that we read the event @@ -102,15 +96,14 @@ public class ListenerImpl extends AbstractListener { } } else { // Badly formed message - LOG.error("Message was not valid. Rejecting message: "+incoming); + LOG.error("Message was not valid. Rejecting message: " + incoming); } } else { if (isValid(incoming)) { LOG.info("Run stopped. Orphaning Message: " + requestIdWithSubId); - } - else { + } else { // Badly formed message - LOG.error("Message was not valid. Rejecting message: "+incoming); + LOG.error("Message was not valid. Rejecting message: " + incoming); } } } @@ -135,8 +128,8 @@ public class ListenerImpl extends AbstractListener { private boolean isValid(DmaapIncomingMessage incoming) { return ((incoming != null) && - incoming.getBody() != null - && !StringUtils.isEmpty(incoming.getRpcName())); + incoming.getBody() != null + && !StringUtils.isEmpty(incoming.getRpcName())); } @Override @@ -147,12 +140,12 @@ public class ListenerImpl extends AbstractListener { String runningTime = df.format(new Date(time - startTime)); String out = String.format("Running for %s and completed %d jobs using %d threads.", runningTime, - executor.getCompletedTaskCount(), executor.getPoolSize()); + executor.getCompletedTaskCount(), executor.getPoolSize()); LOG.info("***BENCHMARK*** " + out); return out; } - private String getRequestIdWithSubId(JsonNode event){ + private String getRequestIdWithSubId(JsonNode event) { String requestId = ""; try { requestId = Converter.extractRequestIdWithSubId(event); diff --git a/appc-event-listener/appc-event-listener-bundle/src/main/java/org/onap/appc/listener/LCM/operation/ProviderOperations.java b/appc-event-listener/appc-event-listener-bundle/src/main/java/org/onap/appc/listener/LCM/operation/ProviderOperations.java index 0e7044930..83e7af82c 100644 --- a/appc-event-listener/appc-event-listener-bundle/src/main/java/org/onap/appc/listener/LCM/operation/ProviderOperations.java +++ b/appc-event-listener/appc-event-listener-bundle/src/main/java/org/onap/appc/listener/LCM/operation/ProviderOperations.java @@ -24,7 +24,24 @@ package org.onap.appc.listener.LCM.operation; +import com.att.eelf.configuration.EELFLogger; +import com.att.eelf.configuration.EELFManager; import com.fasterxml.jackson.databind.JsonNode; +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.MalformedURLException; +import java.net.Socket; +import java.net.URL; +import java.security.KeyManagementException; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.UnrecoverableKeyException; +import java.security.cert.CertificateException; +import java.security.cert.X509Certificate; +import javax.net.ssl.SSLContext; +import javax.net.ssl.TrustManager; +import javax.net.ssl.X509TrustManager; import org.apache.commons.codec.binary.Base64; import org.apache.commons.io.IOUtils; import org.apache.http.HttpHeaders; @@ -48,46 +65,45 @@ import org.onap.appc.exceptions.APPCException; import org.onap.appc.listener.LCM.model.ResponseStatus; import org.onap.appc.listener.util.Mapper; -import com.att.eelf.configuration.EELFLogger; -import com.att.eelf.configuration.EELFManager; - -import javax.net.ssl.SSLContext; -import javax.net.ssl.TrustManager; -import javax.net.ssl.X509TrustManager; -import java.io.IOException; -import java.io.UnsupportedEncodingException; -import java.net.MalformedURLException; -import java.net.Socket; -import java.net.URL; -import java.net.UnknownHostException; -import java.security.*; -import java.security.cert.CertificateException; -import java.security.cert.X509Certificate; - public class ProviderOperations { private final EELFLogger LOG = EELFManager.getInstance().getLogger(ProviderOperations.class); private URL url; - private String basic_auth; + private String basicAuth; private static ProviderOperationRequestFormatter requestFormatter = new GenericProviderOperationRequestFormatter(); + + public ProviderOperations(String url, String username, String password){ + setAuthentication(username, password); + try { + this.url = new URL(url); + } catch (MalformedURLException e) { + LOG.error("An error occurred while building url", e); + } + } + /** * Calls the AppcProvider to run a topology directed graph * - * @param msg The incoming message to be run + * @param message The incoming message to be run * @return True if the result is success. Never returns false and throws an exception instead. * @throws UnsupportedEncodingException * @throws Exception if there was a failure processing the request. The exception message is the failure reason. */ @SuppressWarnings("nls") - public JsonNode topologyDG(String rpcName, JsonNode msg) throws APPCException { - if (msg == null) { + public JsonNode topologyDG(String rpcName, JsonNode message) throws APPCException { + if (message == null) { throw new APPCException("Provided message was null"); } - HttpPost post = null; + HttpPost postRequest = buildPostRequest(rpcName, message); + return getJsonNode(message, postRequest); + } + + private HttpPost buildPostRequest(String rpcName, JsonNode message) throws APPCException { + HttpPost post; try { // Concatenate the "action" on the end of the URL @@ -99,22 +115,25 @@ public class ProviderOperations { post.setHeader(HttpHeaders.ACCEPT, "application/json"); // Set Auth - if (basic_auth != null) { - post.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + basic_auth); + if (basicAuth != null) { + post.setHeader(HttpHeaders.AUTHORIZATION, "Basic " + basicAuth); } - String body = Mapper.toJsonString(msg); + String body = Mapper.toJsonString(message); StringEntity entity = new StringEntity(body); entity.setContentType("application/json"); - post.setEntity(new StringEntity(body)); + post.setEntity(entity); } catch (UnsupportedEncodingException | MalformedURLException e) { throw new APPCException(e); } + return post; + } + private JsonNode getJsonNode(JsonNode message, HttpPost post) throws APPCException { HttpClient client = getHttpClient(); - int httpCode = 0; - String respBody = null; + int httpCode; + String respBody; try { HttpResponse response = client.execute(post); httpCode = response.getStatusLine().getStatusCode(); @@ -131,16 +150,12 @@ public class ProviderOperations { LOG.error("Error processing response from provider. Could not map response to json", e); throw new APPCException("APPC has an unknown RPC error"); } - ResponseStatus responseStatus = requestFormatter.getResponseStatus(json); - if (!isSucceeded(responseStatus.getCode())) { - LOG.warn(String.format("Operation failed [%s]", msg.toString())); + LOG.warn(String.format("Operation failed [%s]", message.toString())); } - return json; } - throw new APPCException(String.format("Unexpected response from endpoint: [%d] - %s ", httpCode, respBody)); } @@ -157,7 +172,7 @@ public class ProviderOperations { try { url = new URL(newUrl); } catch (MalformedURLException e) { - e.printStackTrace(); + LOG.error("An error occurred while building url", e); } } @@ -172,11 +187,11 @@ public class ProviderOperations { public String setAuthentication(String user, String password) { if (user != null && password != null) { String authStr = user + ":" + password; - basic_auth = new String(Base64.encodeBase64(authStr.getBytes())); + basicAuth = new String(Base64.encodeBase64(authStr.getBytes())); } else { - basic_auth = null; + basicAuth = null; } - return basic_auth; + return basicAuth; } @SuppressWarnings("deprecation") @@ -221,7 +236,7 @@ public class ProviderOperations { private SSLContext sslContext = SSLContext.getInstance("TLS"); public MySSLSocketFactory(KeyStore truststore) throws NoSuchAlgorithmException, KeyManagementException, - KeyStoreException, UnrecoverableKeyException { + KeyStoreException, UnrecoverableKeyException { super(truststore); TrustManager tm = new X509TrustManager() { @@ -240,13 +255,13 @@ public class ProviderOperations { }; sslContext.init(null, new TrustManager[]{ - tm + tm }, null); } @Override public Socket createSocket(Socket socket, String host, int port, boolean autoClose) - throws IOException { + throws IOException { return sslContext.getSocketFactory().createSocket(socket, host, port, autoClose); } @@ -257,7 +272,8 @@ public class ProviderOperations { } public static boolean isSucceeded(Integer code) { + + //FIXME is it working as intended? return code != null && ((code == 100) || (code == 400)); } - -} +} \ No newline at end of file diff --git a/appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/LCM/operation/ProviderOperationsTest.java b/appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/LCM/operation/ProviderOperationsTest.java new file mode 100644 index 000000000..42881f12f --- /dev/null +++ b/appc-event-listener/appc-event-listener-bundle/src/test/java/org/onap/appc/listener/LCM/operation/ProviderOperationsTest.java @@ -0,0 +1,120 @@ +/*- + * ============LICENSE_START======================================================= + * ONAP : APPC + * ================================================================================ + * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. + * ================================================================================ + * Copyright (C) 2018 Nokia Solutions and Networks + * ============================================================================= + * 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. + * + * ECOMP is a trademark and service mark of AT&T Intellectual Property. + * ============LICENSE_END========================================================= + */ +package org.onap.appc.listener.LCM.operation; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; +import static org.mockito.Matchers.anyString; + +import com.att.aft.dme2.internal.jersey.core.util.Base64; +import com.fasterxml.jackson.databind.JsonNode; +import java.io.IOException; +import java.net.Socket; +import java.net.SocketException; +import java.security.KeyManagementException; +import java.security.KeyStore; +import java.security.KeyStoreException; +import java.security.NoSuchAlgorithmException; +import java.security.UnrecoverableKeyException; +import org.junit.Before; +import org.junit.Test; +import org.mockito.Mock; +import org.onap.appc.exceptions.APPCException; +import org.onap.appc.listener.LCM.operation.ProviderOperations.MySSLSocketFactory; +import org.onap.appc.listener.util.Mapper; + +public class ProviderOperationsTest { + + private static final String jsonInputBodyStr = + "{\"input\":{ \"common-header\": { \"timestamp\": \"2016-08-03T08:50:18.97Z\", " + + "\"api-ver\": \"1\", \"originator-id\": \"1\", \"request-id\": \"123\", \"sub-request-id\": \"1\", " + + "\"flags\": { \"force\":\"TRUE\", \"ttl\":\"9900\" } }, \"action\": \"Stop\", " + + "\"action-identifiers\": { \"vnf-id\": \"TEST\" } }}"; + private final JsonNode jsonNode = Mapper.toJsonNodeFromJsonString(jsonInputBodyStr); + + private ProviderOperations providerOperations; + private MySSLSocketFactory socketFactory; + + @Mock + private KeyStore mockKeyStore; + + + @Before + public void setup() + throws UnrecoverableKeyException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException { + + providerOperations = + new ProviderOperations("http://127.0.0.1", "test_user", "test_password"); + socketFactory = new MySSLSocketFactory(mockKeyStore); + } + + @Test + public void setAuthentication_should_return_null_given_null_arguments() { + String newAuthentication = providerOperations.setAuthentication(null, null); + assertNull(newAuthentication); + } + + @Test + public void should_set_properties() { + providerOperations.setUrl("hp://123.1.2.3"); + assertEquals("http://127.0.0.1", providerOperations.getUrl()); + providerOperations.setUrl("http://123.1.2.3"); + assertEquals("http://123.1.2.3", providerOperations.getUrl()); + + String newAuthentication = providerOperations.setAuthentication("new_user", "new_password"); + assertEquals("new_user:new_password", Base64.base64Decode(newAuthentication)); + } + + @Test + public void isSucceeded_should_resolve_status_codes() { + + assertFalse(ProviderOperations.isSucceeded(null)); + assertFalse(ProviderOperations.isSucceeded(200)); + assertTrue(ProviderOperations.isSucceeded(100)); + assertTrue(ProviderOperations.isSucceeded(400)); + } + + @Test(expected = APPCException.class) + public void topologyDG_should_throw_given_null_message() throws APPCException { + + providerOperations.topologyDG("test-rpc-name", null); + } + + @Test + public void todo() throws APPCException { + //TODO write some test cases for topologyDG method +// JsonNode result = providerOperations.topologyDG("test", jsonNode); + } + + @Test(expected = SocketException.class) + public void sslSocketFactory_should_throw_when_socket_not_connected() throws IOException { + Socket socket = socketFactory.createSocket(); + assertNotNull(socket); + + socketFactory.createSocket(socket, "127.0.0.1", 123, true); + } +} -- cgit 1.2.3-korg