Test coverage in XInterfaceService 90/78490/4
authorJoss Armstrong <joss.armstrong@ericsson.com>
Thu, 14 Feb 2019 13:38:23 +0000 (13:38 +0000)
committerTakamune Cho <takamune.cho@att.com>
Thu, 14 Feb 2019 16:29:22 +0000 (16:29 +0000)
Increased coverage from 68% to 100%

Issue-ID: APPC-1436
Change-Id: If9f1670ba9955325bf938d7edf3d7d67bb48088c
Signed-off-by: Joss Armstrong <joss.armstrong@ericsson.com>
appc-inbound/appc-design-services/provider/src/main/java/org/onap/appc/design/xinterface/XInterfaceService.java
appc-inbound/appc-design-services/provider/src/test/java/org/onap/appc/design/xinterface/XInterfaceServiceTest.java [new file with mode: 0644]

index 42f1324..4e93af1 100644 (file)
@@ -31,31 +31,34 @@ import com.att.eelf.configuration.EELFManager;
 import com.google.common.base.Strings;
 
 public class XInterfaceService {
-  
-  private final EELFLogger log = EELFManager.getInstance().getLogger(XInterfaceService.class);
-
-  public static XInterfaceService getInstance() {
-    return new XInterfaceService();
-  }
-
-  public String execute(String action, String payload) throws Exception {
-    // File targetFile = new File("/tmp/" + action + "-response.txt" );
-    String interfaceResponse = null;
-    try {
-      if (Strings.isNullOrEmpty(payload))
-        throw new Exception("Payload is null or empty..");
-      if (DesignServiceConstants.GETINSTARDATA.equalsIgnoreCase(action)) {
-        XResponseProcessor xResponseProcessor = new XResponseProcessor();
-        interfaceResponse =
-            xResponseProcessor.parseResponse(payload, DesignServiceConstants.GETINSTARDATA);
-      } else {
-        throw new Exception("No Such Action, Please enter valid Action");
-      }
-    } catch (Exception e) {
-      e.printStackTrace();
-      throw e;
+
+    private final EELFLogger log = EELFManager.getInstance().getLogger(XInterfaceService.class);
+
+    public static XInterfaceService getInstance() {
+        return new XInterfaceService();
+    }
+
+    public String execute(String action, String payload) throws Exception {
+        String interfaceResponse = null;
+        try {
+            if (Strings.isNullOrEmpty(payload))
+                throw new Exception("Payload is null or empty..");
+            if (DesignServiceConstants.GETINSTARDATA.equalsIgnoreCase(action)) {
+                XResponseProcessor xResponseProcessor = getXResponseProcessor();
+                interfaceResponse =
+                        xResponseProcessor.parseResponse(payload, DesignServiceConstants.GETINSTARDATA);
+            } else {
+                throw new Exception("No Such Action, Please enter valid Action");
+            }
+        } catch (Exception e) {
+            log.error(e.getMessage());
+            throw e;
+        }
+        return interfaceResponse;
+    }
+
+    protected XResponseProcessor getXResponseProcessor() {
+        return new XResponseProcessor();
     }
-    return interfaceResponse;
-  }
 
 }
diff --git a/appc-inbound/appc-design-services/provider/src/test/java/org/onap/appc/design/xinterface/XInterfaceServiceTest.java b/appc-inbound/appc-design-services/provider/src/test/java/org/onap/appc/design/xinterface/XInterfaceServiceTest.java
new file mode 100644 (file)
index 0000000..4e3f83f
--- /dev/null
@@ -0,0 +1,76 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP : APPC
+ * ================================================================================
+ * Copyright (C) 2019 Ericsson
+ * ================================================================================
+ * 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.appc.design.xinterface;
+
+import static org.junit.Assert.assertNull;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.mockito.Mockito;
+import org.onap.appc.design.services.util.DesignServiceConstants;
+
+public class XInterfaceServiceTest {
+
+    @Rule
+    public ExpectedException expectedEx = ExpectedException.none();
+
+    @Test
+    public void testExecute() throws Exception {
+        XInterfaceService service = Mockito.spy(new XInterfaceService());
+        XResponseProcessor processor = Mockito.mock(XResponseProcessor.class);
+        Mockito.when(processor.parseResponse(Mockito.anyString(), Mockito.anyString())).thenReturn(null);
+        Mockito.when(service.getXResponseProcessor()).thenReturn(processor);
+        assertNull(service.execute(DesignServiceConstants.GETINSTARDATA, "{}"));
+    }
+
+    @Test
+    public void testExecuteException() throws Exception {
+        XInterfaceService service = Mockito.spy(XInterfaceService.getInstance());
+        XResponseProcessor processor = Mockito.mock(XResponseProcessor.class);
+        Mockito.when(processor.parseResponse(Mockito.anyString(), Mockito.anyString())).thenThrow(new RuntimeException());
+        Mockito.when(service.getXResponseProcessor()).thenReturn(processor);
+        expectedEx.expect(RuntimeException.class);
+        service.execute(DesignServiceConstants.GETINSTARDATA, "{}");
+    }
+
+    @Test
+    public void testExecuteNullPayload() throws Exception {
+        XInterfaceService service = Mockito.spy(XInterfaceService.getInstance());
+        XResponseProcessor processor = Mockito.mock(XResponseProcessor.class);
+        Mockito.when(processor.parseResponse(Mockito.anyString(), Mockito.anyString())).thenThrow(new RuntimeException());
+        Mockito.when(service.getXResponseProcessor()).thenReturn(processor);
+        expectedEx.expect(Exception.class);
+        expectedEx.expectMessage("Payload is null or empty..");
+        service.execute(DesignServiceConstants.GETINSTARDATA, null);
+    }
+
+    @Test
+    public void testExecuteInvalidAction() throws Exception {
+        XInterfaceService service = Mockito.spy(XInterfaceService.getInstance());
+        XResponseProcessor processor = Mockito.mock(XResponseProcessor.class);
+        Mockito.when(processor.parseResponse(Mockito.anyString(), Mockito.anyString())).thenThrow(new RuntimeException());
+        Mockito.when(service.getXResponseProcessor()).thenReturn(processor);
+        expectedEx.expect(Exception.class);
+        expectedEx.expectMessage("No Such Action, Please enter valid Action");
+        service.execute("TEST", "{}");
+    }
+}