summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/node/EnvVariables.java26
-rw-r--r--appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/node/JsonValidator.java29
-rw-r--r--appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/node/ResourceUriExtractor.java17
-rw-r--r--appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/node/RestServiceNode.java112
-rw-r--r--appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/node/TransactionHandler.java11
-rw-r--r--appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/JsonValidatorTest.java36
-rw-r--r--appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/ResourceUriExtractorTest.java15
-rw-r--r--appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/TestRestServiceNode.java178
-rw-r--r--appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/TransactionHandlerTest.java13
-rw-r--r--appc-config/appc-flow-controller/provider/src/test/resources/appc-flow-controller.properties25
10 files changed, 338 insertions, 124 deletions
diff --git a/appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/node/EnvVariables.java b/appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/node/EnvVariables.java
new file mode 100644
index 000000000..6cc3b7550
--- /dev/null
+++ b/appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/node/EnvVariables.java
@@ -0,0 +1,26 @@
+package org.onap.appc.flow.controller.node;
+
+import java.util.function.Function;
+
+/**
+ * Wrapper for accessing environment variables
+ */
+class EnvVariables {
+
+ private Function<String, String> envSupplier;
+
+ EnvVariables() {
+ envSupplier = System::getenv;
+ }
+
+ /**
+ * Allows to override environment variables in tests, prefer to use default constructor
+ */
+ EnvVariables(Function<String, String> envSupplier) {
+ this.envSupplier = envSupplier;
+ }
+
+ String getenv(String variable) {
+ return envSupplier.apply(variable);
+ }
+}
diff --git a/appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/node/JsonValidator.java b/appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/node/JsonValidator.java
new file mode 100644
index 000000000..78d530533
--- /dev/null
+++ b/appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/node/JsonValidator.java
@@ -0,0 +1,29 @@
+package org.onap.appc.flow.controller.node;
+
+import com.att.eelf.configuration.EELFLogger;
+import com.att.eelf.configuration.EELFManager;
+import com.fasterxml.jackson.core.JsonProcessingException;
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import java.io.IOException;
+import org.apache.commons.lang3.StringUtils;
+
+class JsonValidator {
+
+ private static final EELFLogger log = EELFManager.getInstance().getLogger(JsonValidator.class);
+
+ static JsonNode validate(String json) throws IOException {
+ if (StringUtils.isBlank(json)) {
+ return null;
+ }
+ JsonNode output = null;
+ try {
+ ObjectMapper objectMapper = new ObjectMapper();
+ output = objectMapper.readTree(json);
+ } catch (JsonProcessingException e) {
+ log.warn("Response received from interface is not a valid JSON block" + json, e);
+ }
+ return output;
+ }
+
+}
diff --git a/appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/node/ResourceUriExtractor.java b/appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/node/ResourceUriExtractor.java
index 0745f20ea..cd07952cf 100644
--- a/appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/node/ResourceUriExtractor.java
+++ b/appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/node/ResourceUriExtractor.java
@@ -13,6 +13,7 @@ import com.att.eelf.configuration.EELFManager;
import java.util.Properties;
import org.apache.commons.lang.StringUtils;
import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
+import org.onap.ccsdk.sli.core.sli.SvcLogicException;
/**
* Helper class for RestServiceNode
@@ -21,9 +22,7 @@ class ResourceUriExtractor {
private static final EELFLogger log = EELFManager.getInstance().getLogger(RestServiceNode.class);
- private ResourceUriExtractor() {}
-
- static String extractResourceUri(SvcLogicContext ctx, Properties prop) throws Exception {
+ String extractResourceUri(SvcLogicContext ctx, Properties prop) throws Exception {
String resourceUri = ctx.getAttribute(INPUT_URL);
if (StringUtils.isBlank(resourceUri)) {
@@ -38,34 +37,34 @@ class ResourceUriExtractor {
return resourceUri;
}
- private static String getAddress(SvcLogicContext ctx) {
+ private String getAddress(SvcLogicContext ctx) {
String address = ctx.getAttribute(INPUT_HOST_IP_ADDRESS);
String port = ctx.getAttribute(INPUT_PORT_NUMBER);
return HTTP + address + ":" + port;
}
- private static String getContext(SvcLogicContext ctx, Properties prop) {
+ private String getContext(SvcLogicContext ctx, Properties prop) throws Exception {
String context;
if (StringUtils.isNotBlank(ctx.getAttribute(INPUT_CONTEXT))) {
context = "/" + ctx.getAttribute(INPUT_CONTEXT);
} else if (prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION) + ".context") != null) {
context = "/" + prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION) + ".context");
} else {
- throw new IllegalArgumentException("Could Not found the context for operation " + ctx.getAttribute(INPUT_REQUEST_ACTION));
+ throw new Exception("Could Not found the context for operation " + ctx.getAttribute(INPUT_REQUEST_ACTION));
}
return context;
}
- private static String getSubContext(SvcLogicContext ctx, Properties prop) {
+ private String getSubContext(SvcLogicContext ctx, Properties prop) throws Exception {
String subContext;
if (StringUtils.isNotBlank(ctx.getAttribute(INPUT_SUB_CONTEXT))) {
subContext = "/" + ctx.getAttribute(INPUT_SUB_CONTEXT);
} else if (prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION) + ".sub-context") != null) {
subContext = "/" + prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION) + ".sub-context");
} else {
- throw new IllegalArgumentException("Could Not found the sub context for operation " + ctx.getAttribute(INPUT_REQUEST_ACTION));
+ throw new Exception("Could Not found the sub context for operation " + ctx.getAttribute(INPUT_REQUEST_ACTION));
}
return subContext;
}
-}
+} \ No newline at end of file
diff --git a/appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/node/RestServiceNode.java b/appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/node/RestServiceNode.java
index 3597e0d18..184ba3b1b 100644
--- a/appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/node/RestServiceNode.java
+++ b/appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/node/RestServiceNode.java
@@ -32,10 +32,7 @@ import static org.onap.appc.flow.controller.utils.FlowControllerConstants.OUTPUT
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
-import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import java.io.IOException;
import java.util.Map;
import java.util.Properties;
import org.apache.commons.lang3.StringUtils;
@@ -50,6 +47,31 @@ public class RestServiceNode implements SvcLogicJavaPlugin {
private static final EELFLogger log = EELFManager.getInstance().getLogger(RestServiceNode.class);
private static final String SDNC_CONFIG_DIR_VAR = "SDNC_CONFIG_DIR";
+ static final String REST_RESPONSE = "restResponse";
+
+ private final TransactionHandler transactionHandler;
+ private final RestExecutor restExecutor;
+ private final ResourceUriExtractor resourceUriExtractor;
+ private final EnvVariables envVariables;
+
+ public RestServiceNode() {
+ this.transactionHandler = new TransactionHandler();
+ this.restExecutor = new RestExecutor();
+ this.resourceUriExtractor = new ResourceUriExtractor();
+ this.envVariables = new EnvVariables();
+ }
+
+ /**
+ * Constructor for tests, prefer to use no arg constructor
+ */
+ RestServiceNode(TransactionHandler transactionHandler, RestExecutor restExecutor,
+ ResourceUriExtractor uriExtractor, EnvVariables envVariables) {
+ this.transactionHandler = transactionHandler;
+ this.restExecutor = restExecutor;
+ this.resourceUriExtractor = uriExtractor;
+ this.envVariables = envVariables;
+ }
+
public void sendRequest(Map<String, String> inParams, SvcLogicContext ctx)
throws SvcLogicException {
String fn = "RestServiceNode.sendRequest";
@@ -58,84 +80,46 @@ public class RestServiceNode implements SvcLogicJavaPlugin {
try {
responsePrefix = StringUtils.isNotBlank(responsePrefix) ? (responsePrefix + ".") : "";
//Remove below for Block
- for (Object key : ctx.getAttributeKeySet()) {
- String parmName = (String) key;
- String parmValue = ctx.getAttribute(parmName);
- log.info(fn + "Getting Key = " + parmName + "and Value = " + parmValue);
+ for (String key : ctx.getAttributeKeySet()) {
+ log.info(fn + "Getting Key = " + key + "and Value = " + ctx.getAttribute(key));
}
- send(ctx, inParams);
- ctx.setAttribute(responsePrefix + OUTPUT_PARAM_STATUS, OUTPUT_STATUS_SUCCESS);
-
- } catch (Exception e) {
- ctx.setAttribute(responsePrefix + OUTPUT_PARAM_STATUS, OUTPUT_STATUS_FAILURE);
- ctx.setAttribute(responsePrefix + OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
- log.error("Error Message : " + e.getMessage(), e);
- throw new SvcLogicException(e.getMessage());
- }
- }
-
- private void send(SvcLogicContext ctx, Map<String, String> inParams) throws Exception {
- try {
Properties prop = loadProperties();
log.info("Loaded Properties " + prop.toString());
- String responsePrefix = inParams.get(INPUT_PARAM_RESPONSE_PREFIX);
- String resourceUri = ResourceUriExtractor.extractResourceUri(ctx, prop);
+
+ String resourceUri = resourceUriExtractor.extractResourceUri(ctx, prop);
+
log.info("Rest Constructed URL : " + resourceUri);
- Transaction transaction = TransactionHandler.buildTransaction(ctx, prop, resourceUri);
-
- RestExecutor restRequestExecutor = new RestExecutor();
- Map<String, String> output = restRequestExecutor.execute(transaction, ctx);
-
- if (isValidJson(output.get("restResponse")) != null) {
- ctx.setAttribute(responsePrefix + "." + OUTPUT_STATUS_MESSAGE,
- output.get("restResponse"));
-// JsonNode restResponse = isValidJson(output.get("restResponse"));
-// for (String key : inParams.keySet()) {
-// if(key !=null && key.startsWith("output-")){
-// log.info("Found Key = " + key);
-// log.info("Found Key in Params " + inParams.get(key) + ".");
-// JsonNode setValue = restResponse.findValue(inParams.get(key));
-// log.info("Found value = " + setValue);
-// if(setValue !=null && setValue.textValue() !=null && !setValue.textValue().isEmpty())
-// ctx.setAttribute(responsePrefix + "." + key, setValue.textValue());
-// else
-// ctx.setAttribute(responsePrefix + "." + key, null);
-// }
-// }
+ Transaction transaction = transactionHandler.buildTransaction(ctx, prop, resourceUri);
+ Map<String, String> output = restExecutor.execute(transaction, ctx);
+
+ String json = output.get(REST_RESPONSE);
+ log.info("Received response from Interface " + json);
+
+ JsonNode validatedJson = JsonValidator.validate(json);
+
+ if (validatedJson != null) {
+ log.info("state is " + validatedJson.findValue("state"));
+ ctx.setAttribute(responsePrefix + OUTPUT_STATUS_MESSAGE, output.get(REST_RESPONSE));
}
- log.info("Response from Rest :");
+
+ ctx.setAttribute(responsePrefix + OUTPUT_PARAM_STATUS, OUTPUT_STATUS_SUCCESS);
} catch (Exception e) {
- log.error("Error Message: " + e.getMessage(), e);
- throw e;
+ ctx.setAttribute(responsePrefix + OUTPUT_PARAM_STATUS, OUTPUT_STATUS_FAILURE);
+ ctx.setAttribute(responsePrefix + OUTPUT_PARAM_ERROR_MESSAGE, e.getMessage());
+ log.error("Error Message : " + e.getMessage(), e);
+ throw new SvcLogicException(e.getMessage());
}
}
private Properties loadProperties() throws Exception {
- String directory = System.getenv(SDNC_CONFIG_DIR_VAR);
+ String directory = envVariables.getenv(SDNC_CONFIG_DIR_VAR);
if (directory == null) {
throw new Exception("Cannot find Property file: " + SDNC_CONFIG_DIR_VAR);
}
String path = directory + APPC_FLOW_CONTROLLER;
return PropertiesLoader.load(path);
}
-
- private JsonNode isValidJson(String json) throws IOException {
- JsonNode output;
- log.info("Received response from Interface " + json);
- if (json == null || json.isEmpty()) {
- return null;
- }
- try {
- ObjectMapper objectMapper = new ObjectMapper();
- output = objectMapper.readTree(json);
- } catch (JsonProcessingException e) {
- log.warn("Response received from interface is not a valid JSON block" + json, e);
- return null;
- }
- log.info("state is " + output.findValue("state"));
- return output;
- }
}
diff --git a/appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/node/TransactionHandler.java b/appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/node/TransactionHandler.java
index 8d10b3c7f..af9e84589 100644
--- a/appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/node/TransactionHandler.java
+++ b/appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/node/TransactionHandler.java
@@ -13,20 +13,17 @@ import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
*/
class TransactionHandler {
- private TransactionHandler() {}
-
- static Transaction buildTransaction(SvcLogicContext ctx, Properties prop,
- String resourceUri) throws Exception {
+ Transaction buildTransaction(SvcLogicContext ctx, Properties prop, String resourceUri)
+ throws Exception {
String inputRequestAction = ctx.getAttribute(INPUT_REQUEST_ACTION);
String inputRequestActionType = ctx.getAttribute(INPUT_REQUEST_ACTION_TYPE);
if (StringUtils.isBlank(inputRequestActionType)) {
- throw new IllegalArgumentException(
- "Don't know REST operation for Action " + inputRequestActionType);
+ throw new Exception("Don't know REST operation for Action " + inputRequestActionType);
}
if (StringUtils.isBlank(inputRequestAction)) {
- throw new IllegalArgumentException("Don't know request-action " + INPUT_REQUEST_ACTION);
+ throw new Exception("Don't know request-action " + INPUT_REQUEST_ACTION);
}
Transaction transaction = new Transaction();
diff --git a/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/JsonValidatorTest.java b/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/JsonValidatorTest.java
new file mode 100644
index 000000000..fd6f92018
--- /dev/null
+++ b/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/JsonValidatorTest.java
@@ -0,0 +1,36 @@
+package org.onap.appc.flow.controller.node;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import java.io.IOException;
+import org.junit.Assert;
+import org.junit.Test;
+
+public class JsonValidatorTest {
+
+ @Test
+ public void should_return_json_node_on_valid_json() throws IOException {
+ String json = "{'test': 'OK'}".replaceAll("'", "\"");
+ JsonNode result = JsonValidator.validate(json);
+
+ Assert.assertNotNull(result);
+ Assert.assertTrue(result.has("test"));
+ Assert.assertEquals("OK", result.get("test").asText());
+ }
+
+ @Test
+ public void should_return_null_on_empty_input() throws IOException {
+ String json = "";
+ JsonNode result = JsonValidator.validate(json);
+
+ Assert.assertNull(result);
+ }
+
+ @Test
+ public void should_return_null_on_invalid_input() throws IOException {
+ String json = "{'test': 'OK'".replaceAll("'", "\"");
+ JsonNode result = JsonValidator.validate(json);
+
+ Assert.assertNull(result);
+ }
+
+} \ No newline at end of file
diff --git a/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/ResourceUriExtractorTest.java b/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/ResourceUriExtractorTest.java
index 06f86c56f..28a202c2d 100644
--- a/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/ResourceUriExtractorTest.java
+++ b/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/ResourceUriExtractorTest.java
@@ -24,21 +24,24 @@ public class ResourceUriExtractorTest {
@Rule
public ExpectedException expectedException = ExpectedException.none();
+ private ResourceUriExtractor resourceUriExtractor;
@Before
public void setUp() {
ctx = mock(SvcLogicContext.class);
prop = mock(Properties.class);
+ resourceUriExtractor = new ResourceUriExtractor();
}
@Test
public void should_return_input_url_if_exist() throws Exception {
ctx = mock(SvcLogicContext.class);
- when(ctx.getAttribute(INPUT_URL)).thenReturn("test resource uri");
+ when(ctx.getAttribute(INPUT_URL)).thenReturn("http://localhost:8080");
- String resourceUri = ResourceUriExtractor.extractResourceUri(ctx, prop);
+ resourceUriExtractor = new ResourceUriExtractor();
+ String resourceUri = resourceUriExtractor.extractResourceUri(ctx, prop);
- Assert.assertEquals("test resource uri", resourceUri);
+ Assert.assertEquals("http://localhost:8080", resourceUri);
}
@Test
@@ -50,7 +53,7 @@ public class ResourceUriExtractorTest {
when(ctx.getAttribute(INPUT_CONTEXT)).thenReturn("input-context");
when(ctx.getAttribute(INPUT_SUB_CONTEXT)).thenReturn("input-sub-context");
- String resourceUri = ResourceUriExtractor.extractResourceUri(ctx, prop);
+ String resourceUri = resourceUriExtractor.extractResourceUri(ctx, prop);
Assert.assertEquals("http://localhost:8080/input-context/input-sub-context", resourceUri);
}
@@ -68,7 +71,7 @@ public class ResourceUriExtractorTest {
when(prop.getProperty("request-action.context")).thenReturn("ra-context");
when(prop.getProperty("request-action.sub-context")).thenReturn("ra-sub-context");
- String resourceUri = ResourceUriExtractor.extractResourceUri(ctx, prop);
+ String resourceUri = resourceUriExtractor.extractResourceUri(ctx, prop);
Assert.assertEquals("http://localhost:8080/ra-context/ra-sub-context", resourceUri);
}
@@ -82,7 +85,7 @@ public class ResourceUriExtractorTest {
expectedException.expect(Exception.class);
expectedException.expectMessage("Could Not found the context for operation null");
- ResourceUriExtractor.extractResourceUri(ctx, prop);
+ resourceUriExtractor.extractResourceUri(ctx, prop);
}
} \ No newline at end of file
diff --git a/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/TestRestServiceNode.java b/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/TestRestServiceNode.java
index a6a207db7..20a1fd3f9 100644
--- a/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/TestRestServiceNode.java
+++ b/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/TestRestServiceNode.java
@@ -1,4 +1,3 @@
-
/*-
* ============LICENSE_START=======================================================
* ONAP : APPC
@@ -22,56 +21,169 @@
*/
package org.onap.appc.flow.controller.node;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.eq;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.verify;
+import static org.mockito.Mockito.verifyNoMoreInteractions;
+import static org.mockito.Mockito.when;
+import static org.onap.appc.flow.controller.node.RestServiceNode.REST_RESPONSE;
+import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_PARAM_RESPONSE_PREFIX;
+
import java.util.HashMap;
import java.util.Map;
+import java.util.Properties;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Rule;
import org.junit.Test;
+import org.junit.rules.ExpectedException;
import org.onap.appc.flow.controller.data.Transaction;
import org.onap.appc.flow.controller.executorImpl.RestExecutor;
import org.onap.appc.flow.controller.utils.FlowControllerConstants;
import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
+import org.onap.ccsdk.sli.core.sli.SvcLogicException;
public class TestRestServiceNode {
- @Test(expected=Exception.class)
- public void testRestServiceNode() throws Exception {
+ private static final String RESOURCE_URI = "resource-uri";
+ private static final String MOCK_ENV = "src/test/resources";
+ private static final String REST_BODY_RESPONSE = "{ 'state' : 'TEST' }";
- SvcLogicContext ctx = new SvcLogicContext();
- ctx.setAttribute(FlowControllerConstants.VNF_TYPE, "vUSP - vDBE-IPX HUB");
- ctx.setAttribute(FlowControllerConstants.REQUEST_ACTION, "healthcheck");
- ctx.setAttribute(FlowControllerConstants.VNFC_TYPE, "TESTVNFC-CF");
- ctx.setAttribute(FlowControllerConstants.REQUEST_ID,"TESTCOMMONFRMWK");
- ctx.setAttribute("host-ip-address","127.0.0.1");
- ctx.setAttribute("port-number","8888");
- ctx.setAttribute("request-action-type","GET");
- ctx.setAttribute("context", "loader/restconf/operations/appc-provider-lcm:health-check");
+ private RestServiceNode restServiceNode;
- HashMap<String, String> inParams = new HashMap<String, String>();
- RestServiceNode rsn = new RestServiceNode();
- inParams.put("output-state", "state");
- inParams.put("responsePrefix", "healthcheck");
- rsn.sendRequest(inParams, ctx);
+ private ResourceUriExtractor uriExtractorMock;
+ private TransactionHandler transactionHandlerMock;
+ private RestExecutor restExecutorMock;
- for (Object key : ctx.getAttributeKeySet()) {
- String parmName = (String) key;
- String parmValue = ctx.getAttribute(parmName);
- }
+ private SvcLogicContext ctxMock;
+ private Transaction transaction;
+ private Map<String, String> params;
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
- }
+ @Before
+ public void setUp() throws Exception {
+
+ uriExtractorMock = mock(ResourceUriExtractor.class);
+ transactionHandlerMock = mock(TransactionHandler.class);
+ restExecutorMock = mock(RestExecutor.class);
+ ctxMock = mock(SvcLogicContext.class);
+ transaction = mock(Transaction.class);
+
+ // given
+ params = new HashMap<>();
+
+ HashMap<String, String> restResponseMap = new HashMap<>();
+ restResponseMap.put(REST_RESPONSE, REST_BODY_RESPONSE.replaceAll("'", "\""));
+
+ when(uriExtractorMock
+ .extractResourceUri(any(SvcLogicContext.class), any(Properties.class)))
+ .thenReturn(RESOURCE_URI);
+ when(transactionHandlerMock
+ .buildTransaction(any(SvcLogicContext.class), any(Properties.class), eq(RESOURCE_URI)))
+ .thenReturn(transaction);
+ when(restExecutorMock.execute(eq(transaction), any(SvcLogicContext.class)))
+ .thenReturn(restResponseMap);
+
+ EnvVariables envVariables = new EnvVariables(envKey -> MOCK_ENV);
+ restServiceNode = new RestServiceNode(transactionHandlerMock, restExecutorMock, uriExtractorMock, envVariables);
+ }
+
+ @Test
+ public void should_send_request() throws Exception {
+ // given
+ params.put(INPUT_PARAM_RESPONSE_PREFIX, "some-prefix");
+
+ // when
+ restServiceNode.sendRequest(params, ctxMock);
+
+ // then
+ verify(uriExtractorMock)
+ .extractResourceUri(eq(ctxMock), any(Properties.class));
+ verify(transactionHandlerMock)
+ .buildTransaction(eq(ctxMock), any(Properties.class), eq(RESOURCE_URI));
+ verify(restExecutorMock)
+ .execute(transaction, ctxMock);
+ verifyNoMoreInteractions(uriExtractorMock, transactionHandlerMock, restExecutorMock);
+ }
+
+ @Test
+ public void should_rethrow_exception_from_uri_extractor() throws Exception {
+ when(uriExtractorMock
+ .extractResourceUri(eq(ctxMock), any(Properties.class)))
+ .thenThrow(new Exception("resource uri exception"));
+ expectedException.expect(SvcLogicException.class);
+ expectedException.expectMessage("resource uri exception");
- @Test(expected=Exception.class)
- public void testInputParamsRestServiceNode() throws Exception {
- SvcLogicContext ctx = new SvcLogicContext();
- ctx.setAttribute("vnf-id", "test");
- ctx.setAttribute("tmp.vnfInfo.vm-count", "1");
- ctx.setAttribute("tmp.vnfInfo.vm[0].vnfc-count", "1");
- RestExecutor restExe = new RestExecutor();
- Transaction transaction = new Transaction();
+ restServiceNode.sendRequest(params, ctxMock);
+ }
- FlowControlNode node = new FlowControlNode();
- Map<String,String> flowSeq= restExe.execute(transaction, ctx);
- String flowSequnce=flowSeq.get("restResponse");
+ @Test
+ public void should_rethrow_exception_from_transaction_handler() throws Exception {
+ when(transactionHandlerMock
+ .buildTransaction(eq(ctxMock), any(Properties.class), eq(RESOURCE_URI)))
+ .thenThrow(new Exception("transaction exception"));
+ expectedException.expect(SvcLogicException.class);
+ expectedException.expectMessage("transaction exception");
+
+ restServiceNode.sendRequest(params, ctxMock);
+ }
+
+ @Test
+ public void should_rethrow_exception_from_rest_executor() throws Exception {
+ when(restExecutorMock
+ .execute(transaction, ctxMock))
+ .thenThrow(new Exception("rest executor exception"));
+
+ expectedException.expect(SvcLogicException.class);
+ expectedException.expectMessage("rest executor exception");
+
+ restServiceNode.sendRequest(params, ctxMock);
+ }
+
+ @Ignore("missing asserts")
+ @Test(expected = Exception.class)
+ public void testRestServiceNode() throws Exception {
+
+ SvcLogicContext ctx = new SvcLogicContext();
+ ctx.setAttribute(FlowControllerConstants.VNF_TYPE, "vUSP - vDBE-IPX HUB");
+ ctx.setAttribute(FlowControllerConstants.REQUEST_ACTION, "healthcheck");
+ ctx.setAttribute(FlowControllerConstants.VNFC_TYPE, "TESTVNFC-CF");
+ ctx.setAttribute(FlowControllerConstants.REQUEST_ID, "TESTCOMMONFRMWK");
+ ctx.setAttribute("host-ip-address", "127.0.0.1");
+ ctx.setAttribute("port-number", "8888");
+ ctx.setAttribute("request-action-type", "GET");
+ ctx.setAttribute("context", "loader/restconf/operations/appc-provider-lcm:health-check");
+
+ HashMap<String, String> inParams = new HashMap<String, String>();
+ RestServiceNode rsn = new RestServiceNode();
+ inParams.put("output-state", "state");
+ inParams.put("responsePrefix", "healthcheck");
+ rsn.sendRequest(inParams, ctx);
+
+ for (Object key : ctx.getAttributeKeySet()) {
+ String parmName = (String) key;
+ String parmValue = ctx.getAttribute(parmName);
}
+ }
+
+ @Ignore("missing asserts")
+ @Test(expected = Exception.class)
+ public void testInputParamsRestServiceNode() throws Exception {
+ SvcLogicContext ctx = new SvcLogicContext();
+ ctx.setAttribute("vnf-id", "test");
+ ctx.setAttribute("tmp.vnfInfo.vm-count", "1");
+ ctx.setAttribute("tmp.vnfInfo.vm[0].vnfc-count", "1");
+ RestExecutor restExe = new RestExecutor();
+ Transaction transaction = new Transaction();
+
+ FlowControlNode node = new FlowControlNode();
+ Map<String, String> flowSeq = restExe.execute(transaction, ctx);
+ String flowSequnce = flowSeq.get("restResponse");
+
+ }
}
diff --git a/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/TransactionHandlerTest.java b/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/TransactionHandlerTest.java
index 5cdb062f4..f16dd65cc 100644
--- a/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/TransactionHandlerTest.java
+++ b/appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/TransactionHandlerTest.java
@@ -18,6 +18,7 @@ public class TransactionHandlerTest {
private static final String RESOURCE_URI = "some uri";
+ private TransactionHandler transactionHandler;
private SvcLogicContext ctx;
private Properties prop;
@@ -26,6 +27,7 @@ public class TransactionHandlerTest {
@Before
public void setUp() {
+ transactionHandler = new TransactionHandler();
ctx = mock(SvcLogicContext.class);
prop = mock(Properties.class);
}
@@ -35,9 +37,10 @@ public class TransactionHandlerTest {
when(ctx.getAttribute(INPUT_REQUEST_ACTION_TYPE)).thenReturn("");
- expectedException.expect(IllegalArgumentException.class);
+ expectedException.expect(Exception.class);
expectedException.expectMessage("Don't know REST operation for Action");
- TransactionHandler.buildTransaction(ctx, prop, RESOURCE_URI);
+ transactionHandler = new TransactionHandler();
+ transactionHandler.buildTransaction(ctx, prop, RESOURCE_URI);
}
@Test
@@ -45,9 +48,9 @@ public class TransactionHandlerTest {
when(ctx.getAttribute(INPUT_REQUEST_ACTION_TYPE)).thenReturn("foo");
- expectedException.expect(IllegalArgumentException.class);
+ expectedException.expect(Exception.class);
expectedException.expectMessage("Don't know request-action request-action");
- TransactionHandler.buildTransaction(ctx, prop, "some uri");
+ transactionHandler.buildTransaction(ctx, prop, "some uri");
}
@Test
@@ -61,7 +64,7 @@ public class TransactionHandlerTest {
when(prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION).concat(".default-rest-pass")))
.thenReturn("rest-pass");
- Transaction transaction = TransactionHandler.buildTransaction(ctx, prop, "some uri");
+ Transaction transaction = transactionHandler.buildTransaction(ctx, prop, "some uri");
Assert.assertEquals(INPUT_REQUEST_ACTION, transaction.getAction());
Assert.assertEquals("input-ra-type", transaction.getExecutionRPC());
diff --git a/appc-config/appc-flow-controller/provider/src/test/resources/appc-flow-controller.properties b/appc-config/appc-flow-controller/provider/src/test/resources/appc-flow-controller.properties
new file mode 100644
index 000000000..0bbe7f207
--- /dev/null
+++ b/appc-config/appc-flow-controller/provider/src/test/resources/appc-flow-controller.properties
@@ -0,0 +1,25 @@
+###
+# ============LICENSE_START=======================================================
+# ONAP : APP-C
+# ================================================================================
+# Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
+# ================================================================================
+# 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=========================================================
+###
+
+# test properties used by RestServiceNode
+
+healthcheck.mock=false
+healthcheck.default-rest-user=User
+healthcheck.default-rest-pass=@#asd723%^