Improve coverage flow/controller/node #2 79/32079/2
authorkurczews <krzysztof.kurczewski@nokia.com>
Mon, 19 Feb 2018 08:05:47 +0000 (09:05 +0100)
committerPatrick Brady <pb071s@att.com>
Wed, 21 Feb 2018 21:10:09 +0000 (21:10 +0000)
* Extract TransactionHandler util class
* Minor cleanup

Issue-ID: APPC-440
Change-Id: I73d2ced81713daf36523e6b9d7de3bf19c98c9a1
Signed-off-by: kurczews <krzysztof.kurczewski@nokia.com>
appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/data/Transaction.java
appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/executorImpl/RestExecutor.java
appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/node/ResourceUriExtractor.java
appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/node/RestServiceNode.java
appc-config/appc-flow-controller/provider/src/main/java/org/onap/appc/flow/controller/node/TransactionHandler.java [new file with mode: 0644]
appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/ResourceUriExtractorTest.java
appc-config/appc-flow-controller/provider/src/test/java/org/onap/appc/flow/controller/node/TransactionHandlerTest.java [new file with mode: 0644]

index 011ac97..2f64a21 100644 (file)
@@ -65,7 +65,7 @@ public class RestExecutor implements FlowExecutorInterface {
                     HTTPSProperties.PROPERTY_HTTPS_PROPERTIES,
                     new HTTPSProperties(getHostnameVerifier(), sslContext));
             client = createClient(defaultClientConfig);
-            client.addFilter(new HTTPBasicAuthFilter(transaction.getuId(), transaction.getPswd()));
+            client.addFilter(new HTTPBasicAuthFilter(transaction.getId(), transaction.getPswd()));
             WebResource webResource = client.resource(new URI(transaction.getExecutionEndPoint()));
             webResource.setProperty("Content-Type", "application/json;charset=UTF-8");
 
index 3cfea31..0745f20 100644 (file)
@@ -21,8 +21,7 @@ class ResourceUriExtractor {
 
   private static final EELFLogger log = EELFManager.getInstance().getLogger(RestServiceNode.class);
 
-  private ResourceUriExtractor() {
-  }
+  private ResourceUriExtractor() {}
 
   static String extractResourceUri(SvcLogicContext ctx, Properties prop) throws Exception {
     String resourceUri = ctx.getAttribute(INPUT_URL);
index 9d7f6e8..3597e0d 100644 (file)
@@ -24,8 +24,6 @@ package org.onap.appc.flow.controller.node;
 
 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.APPC_FLOW_CONTROLLER;
 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_PARAM_RESPONSE_PREFIX;
-import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_REQUEST_ACTION;
-import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_REQUEST_ACTION_TYPE;
 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.OUTPUT_PARAM_ERROR_MESSAGE;
 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.OUTPUT_PARAM_STATUS;
 import static org.onap.appc.flow.controller.utils.FlowControllerConstants.OUTPUT_STATUS_FAILURE;
@@ -85,31 +83,7 @@ public class RestServiceNode implements SvcLogicJavaPlugin {
       String resourceUri = ResourceUriExtractor.extractResourceUri(ctx, prop);
       log.info("Rest Constructed URL : " + resourceUri);
 
-      Transaction transaction = new Transaction();
-      transaction.setExecutionEndPoint(resourceUri);
-      transaction
-          .setExecutionRPC(ctx.getAttribute(INPUT_REQUEST_ACTION_TYPE));
-      transaction.setAction(INPUT_REQUEST_ACTION);
-      if (ctx.getAttribute(INPUT_REQUEST_ACTION_TYPE) == null
-          || ctx.getAttribute(INPUT_REQUEST_ACTION_TYPE).isEmpty()) {
-        throw new Exception("Dont know REST operation for Action " + transaction.getExecutionRPC());
-      }
-      if (ctx.getAttribute(INPUT_REQUEST_ACTION) == null
-          || ctx.getAttribute(INPUT_REQUEST_ACTION).isEmpty()) {
-        throw new Exception("Dont know request-action " + transaction.getAction());
-      }
-
-      //This code need to get changed to get the UserID and pass from a common place.
-      if (transaction.getuId() == null) {
-        transaction
-            .setuId(prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION)
-                .concat(".default-rest-user")));
-      }
-      if (transaction.getPswd() == null) {
-        transaction
-            .setPswd(prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION)
-                .concat(".default-rest-pass")));
-      }
+      Transaction transaction = TransactionHandler.buildTransaction(ctx, prop, resourceUri);
 
       RestExecutor restRequestExecutor = new RestExecutor();
       Map<String, String> output = restRequestExecutor.execute(transaction, ctx);
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
new file mode 100644 (file)
index 0000000..8d10b3c
--- /dev/null
@@ -0,0 +1,44 @@
+package org.onap.appc.flow.controller.node;
+
+import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_REQUEST_ACTION;
+import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_REQUEST_ACTION_TYPE;
+
+import java.util.Properties;
+import org.apache.commons.lang3.StringUtils;
+import org.onap.appc.flow.controller.data.Transaction;
+import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
+
+/**
+ * Helper class for RestServiceNode
+ */
+class TransactionHandler {
+
+  private TransactionHandler() {}
+
+  static 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);
+    }
+    if (StringUtils.isBlank(inputRequestAction)) {
+      throw new IllegalArgumentException("Don't know request-action " + INPUT_REQUEST_ACTION);
+    }
+
+    Transaction transaction = new Transaction();
+    transaction.setExecutionEndPoint(resourceUri);
+    transaction.setExecutionRPC(inputRequestActionType);
+    transaction.setAction(INPUT_REQUEST_ACTION);
+
+    //This code need to get changed to get the UserID and pass from a common place.
+    transaction.setuId(prop.getProperty(inputRequestAction.concat(".default-rest-user")));
+    transaction.setPswd(prop.getProperty(inputRequestAction.concat(".default-rest-pass")));
+
+    return transaction;
+  }
+
+}
\ No newline at end of file
index d7a853d..06f86c5 100644 (file)
@@ -20,18 +20,20 @@ import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
 public class ResourceUriExtractorTest {
 
   private Properties prop;
+  private SvcLogicContext ctx;
 
   @Rule
   public ExpectedException expectedException = ExpectedException.none();
 
   @Before
   public void setUp() {
-    prop = new Properties();
+    ctx = mock(SvcLogicContext.class);
+    prop = mock(Properties.class);
   }
 
   @Test
   public void should_return_input_url_if_exist() throws Exception {
-    SvcLogicContext ctx = mock(SvcLogicContext.class);
+    ctx = mock(SvcLogicContext.class);
     when(ctx.getAttribute(INPUT_URL)).thenReturn("test resource uri");
 
     String resourceUri = ResourceUriExtractor.extractResourceUri(ctx, prop);
@@ -41,8 +43,6 @@ public class ResourceUriExtractorTest {
 
   @Test
   public void should_extract_url_input_if_context_input_provided() throws Exception {
-    SvcLogicContext ctx = mock(SvcLogicContext.class);
-
     when(ctx.getAttribute(INPUT_URL)).thenReturn("");
     when(ctx.getAttribute(INPUT_HOST_IP_ADDRESS)).thenReturn("localhost");
     when(ctx.getAttribute(INPUT_PORT_NUMBER)).thenReturn("8080");
@@ -57,7 +57,6 @@ public class ResourceUriExtractorTest {
 
   @Test
   public void should_extract_url_input_if_request_action_provided() throws Exception {
-    SvcLogicContext ctx = mock(SvcLogicContext.class);
 
     when(ctx.getAttribute(INPUT_URL)).thenReturn("");
     when(ctx.getAttribute(INPUT_HOST_IP_ADDRESS)).thenReturn("localhost");
@@ -66,8 +65,8 @@ public class ResourceUriExtractorTest {
     when(ctx.getAttribute(INPUT_REQUEST_ACTION)).thenReturn("request-action");
     when(ctx.getAttribute(INPUT_REQUEST_ACTION)).thenReturn("request-action");
 
-    prop.put("request-action.context", "ra-context");
-    prop.put("request-action.sub-context", "ra-sub-context");
+    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);
 
@@ -76,14 +75,13 @@ public class ResourceUriExtractorTest {
 
   @Test
   public void should_throw_exception_if_missing_context() throws Exception {
-    SvcLogicContext ctx = mock(SvcLogicContext.class);
-
     when(ctx.getAttribute(INPUT_URL)).thenReturn("");
     when(ctx.getAttribute(INPUT_HOST_IP_ADDRESS)).thenReturn("localhost");
     when(ctx.getAttribute(INPUT_PORT_NUMBER)).thenReturn("8080");
 
     expectedException.expect(Exception.class);
     expectedException.expectMessage("Could Not found the context for operation null");
+
     ResourceUriExtractor.extractResourceUri(ctx, prop);
   }
 
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
new file mode 100644 (file)
index 0000000..5cdb062
--- /dev/null
@@ -0,0 +1,73 @@
+package org.onap.appc.flow.controller.node;
+
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
+import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_REQUEST_ACTION;
+import static org.onap.appc.flow.controller.utils.FlowControllerConstants.INPUT_REQUEST_ACTION_TYPE;
+
+import java.util.Properties;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.onap.appc.flow.controller.data.Transaction;
+import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
+
+public class TransactionHandlerTest {
+
+  private static final String RESOURCE_URI = "some uri";
+
+  private SvcLogicContext ctx;
+  private Properties prop;
+
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
+
+  @Before
+  public void setUp() {
+    ctx = mock(SvcLogicContext.class);
+    prop = mock(Properties.class);
+  }
+
+  @Test
+  public void should_throw_exception_when_input_request_action_type_missing() throws Exception {
+
+    when(ctx.getAttribute(INPUT_REQUEST_ACTION_TYPE)).thenReturn("");
+
+    expectedException.expect(IllegalArgumentException.class);
+    expectedException.expectMessage("Don't know REST operation for Action");
+    TransactionHandler.buildTransaction(ctx, prop, RESOURCE_URI);
+  }
+
+  @Test
+  public void should_throw_exception_when_input_request_action_missing() throws Exception {
+
+    when(ctx.getAttribute(INPUT_REQUEST_ACTION_TYPE)).thenReturn("foo");
+
+    expectedException.expect(IllegalArgumentException.class);
+    expectedException.expectMessage("Don't know request-action request-action");
+    TransactionHandler.buildTransaction(ctx, prop, "some uri");
+  }
+
+  @Test
+  public void should_return_proper_transaction() throws Exception {
+
+    when(ctx.getAttribute(INPUT_REQUEST_ACTION_TYPE)).thenReturn("input-ra-type");
+    when(ctx.getAttribute(INPUT_REQUEST_ACTION)).thenReturn("input-ra");
+
+    when(prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION).concat(".default-rest-user")))
+        .thenReturn("rest-user");
+    when(prop.getProperty(ctx.getAttribute(INPUT_REQUEST_ACTION).concat(".default-rest-pass")))
+        .thenReturn("rest-pass");
+
+    Transaction transaction = TransactionHandler.buildTransaction(ctx, prop, "some uri");
+
+    Assert.assertEquals(INPUT_REQUEST_ACTION, transaction.getAction());
+    Assert.assertEquals("input-ra-type", transaction.getExecutionRPC());
+    Assert.assertEquals("some uri", transaction.getExecutionEndPoint());
+    Assert.assertEquals("rest-user", transaction.getId());
+    Assert.assertEquals("rest-pass", transaction.getPswd());
+  }
+
+}
\ No newline at end of file