Fix Blocker/Critical issues

Fix Blocker/Critical issues in ccsdk/sli/adaptors/aai-service module

Change-Id: Ia6fd34892a8ec6db83126f31b27bd7b69b77b256
Issue-Id: CCSDK-102
Signed-off-by: Rich Tabedzki <richard.tabedzki@att.com>
diff --git a/aai-service/provider/src/test/java/org/onap/ccsdk/sli/adaptors/aai/AAIServiceTest.java b/aai-service/provider/src/test/java/org/onap/ccsdk/sli/adaptors/aai/AAIServiceTest.java
new file mode 100755
index 0000000..011b926
--- /dev/null
+++ b/aai-service/provider/src/test/java/org/onap/ccsdk/sli/adaptors/aai/AAIServiceTest.java
@@ -0,0 +1,211 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * openECOMP : SDN-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=========================================================
+ */
+
+package org.onap.ccsdk.sli.adaptors.aai;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+import static org.mockito.Mockito.when;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.ProtocolException;
+import java.net.URISyntaxException;
+import java.net.URL;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.util.HashMap;
+import java.util.Map;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+import javax.net.ssl.HttpsURLConnection;
+import javax.ws.rs.HttpMethod;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.mockito.Mock;
+import org.mockito.Spy;
+import org.mockito.runners.MockitoJUnitRunner;
+
+import com.fasterxml.jackson.core.JsonParseException;
+import com.fasterxml.jackson.databind.JsonMappingException;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import org.openecomp.aai.inventory.v11.*;
+
+import org.onap.ccsdk.sli.core.sli.SvcLogicException;
+import org.onap.ccsdk.sli.core.sli.SvcLogicResource.QueryStatus;
+import org.onap.ccsdk.sli.adaptors.aai.AAIService.TransactionIdTracker;
+import org.onap.ccsdk.sli.core.sli.SvcLogicContext;
+
+@RunWith(MockitoJUnitRunner.class)
+public class AAIServiceTest {
+    private static AAIService aaiService = new AAIService(
+            AAIService.class.getResource(AAIService.AAICLIENT_PROPERTIES));
+
+    @Spy private AAIService aaiServiceSpy = new AAIService(
+            AAIService.class.getResource(AAIService.AAICLIENT_PROPERTIES));
+
+    @Mock private HttpsURLConnection connMock;
+
+    // @Test
+    public void existsInvalidResource_shouldReturnFailure() throws MalformedURLException, Exception {
+        QueryStatus queryStatus = aaiServiceSpy.exists("InvalidResource", null, null, null);
+        assertEquals(QueryStatus.FAILURE, queryStatus);
+    }
+
+//    @Test
+    public void existsGetPserverByCallBackUrl_shouldReturnSuccess() throws MalformedURLException, Exception {
+        String key = "https://aai.api.simpledemo.openecomp.org:8443/aai/v11/cloud-infrastructure/pservers/pserver/chcil129snd";
+        String fileLocation = "json/pserverJson.txt";
+        SvcLogicContext ctx = new SvcLogicContext();
+        setConnMock();
+
+        when(aaiServiceSpy.getConfiguredConnection(new URL(key), HttpMethod.GET)).thenReturn(connMock);
+        when(connMock.getResponseCode()).thenReturn(200);
+        when(connMock.getInputStream()).thenReturn(getClass().getClassLoader().getResourceAsStream(fileLocation));
+
+        QueryStatus queryStatus = aaiServiceSpy.exists("pserver", key, "prefix.", ctx);
+
+        assertEquals(QueryStatus.SUCCESS, queryStatus);
+    }
+
+//    @Test
+    public void existsGetPserverByCallBackUrl_throwsExceptionAndReturnsFailure()
+            throws MalformedURLException, Exception {
+        String key = "https://aai.api.simpledemo.openecomp.org:8443/aai/v11/cloud-infrastructure/pservers/pserver/chcil129snd";
+        String fileLocation = "json/pserverJson.txt";
+        SvcLogicContext ctx = new SvcLogicContext();
+        setConnMock();
+
+        when(aaiServiceSpy.getConfiguredConnection(new URL(key), HttpMethod.GET)).thenReturn(connMock);
+        when(connMock.getResponseCode()).thenReturn(200);
+        when(connMock.getInputStream()).thenReturn(getClass().getClassLoader().getResourceAsStream(fileLocation));
+
+        when(aaiServiceSpy.dataChangeRequestAaiData(key, Pserver.class)).thenThrow(
+                new AAIServiceException("testException"));
+
+        QueryStatus queryStatus = aaiServiceSpy.exists("pserver", key, "prefix.", ctx);
+
+        assertEquals(QueryStatus.FAILURE, queryStatus);
+    }
+
+//    @Test
+    public void pserverDataChangeRequestData_shouldSucceed() throws Exception {
+        String fileLocation = "json/pserverJson.txt";
+        String url = "https://aai.api.simpledemo.openecomp.org:8443/aai/v11/cloud-infrastructure/pservers/pserver/chcil129snd";
+        setConnMock();
+
+        when(aaiServiceSpy.getConfiguredConnection(new URL(url), HttpMethod.GET)).thenReturn(connMock);
+        when(connMock.getResponseCode()).thenReturn(200);
+        when(connMock.getInputStream()).thenReturn(getClass().getClassLoader().getResourceAsStream(fileLocation));
+
+        Pserver pserver = aaiServiceSpy.dataChangeRequestAaiData(url, Pserver.class);
+
+        assertEquals("chcil129snd", pserver.getHostname());
+    }
+
+//    @Test
+    public void pserverDataChangeRequestData_shouldReturnNullFor404() throws Exception {
+        String fileLocation = "json/pserverJson.txt";
+        String url = "https://aai.api.simpledemo.openecomp.org:8443/aai/v11/cloud-infrastructure/pservers/pserver/chcil129snd";
+        setConnMock();
+
+        when(aaiServiceSpy.getConfiguredConnection(new URL(url), HttpMethod.GET)).thenReturn(connMock);
+        when(connMock.getResponseCode()).thenReturn(404);
+        when(connMock.getErrorStream()).thenReturn(getClass().getClassLoader().getResourceAsStream(fileLocation));
+
+        Pserver pserver = aaiServiceSpy.dataChangeRequestAaiData(url, Pserver.class);
+
+        assertEquals(null, pserver);
+    }
+
+    @Test(expected = AAIServiceException.class)
+    public void dataChangeRequestData_throwsAAIServiceException() throws Exception {
+        String fileLocation = "json/pserverJson.txt";
+        String url = "https://aai.api.simpledemo.openecomp.org:8443/aai/v11/cloud-infrastructure/pservers/pserver/chcil129snd";
+        setConnMock();
+
+        when(aaiServiceSpy.getConfiguredConnection(new URL(url), HttpMethod.GET)).thenReturn(connMock);
+        when(connMock.getResponseCode()).thenReturn(500);
+        when(connMock.getInputStream()).thenReturn(getClass().getClassLoader().getResourceAsStream(fileLocation));
+
+        aaiServiceSpy.dataChangeRequestAaiData(url, Class.class);
+    }
+
+    public String readFileToString(String fileName) throws IOException, URISyntaxException {
+        URL url = AAIServiceTest.class.getResource(fileName);
+        Path resPath = Paths.get(url.toURI());
+
+        return new String(Files.readAllBytes(resPath), "UTF8");
+    }
+
+    public <T> T getObjectFromJson(String text, Class<T> type)
+            throws JsonParseException, JsonMappingException, IOException {
+        ObjectMapper mapper = AAIService.getObjectMapper();
+
+        return type.cast(mapper.readValue(text, type));
+    }
+
+    private void setConnMock() throws ProtocolException {
+        // Set up the connection properties
+        connMock.setRequestProperty("Connection", "close");
+        connMock.setDoInput(true);
+        connMock.setDoOutput(true);
+        connMock.setUseCaches(false);
+        connMock.setConnectTimeout(1000);
+        connMock.setReadTimeout(1000);
+        connMock.setRequestMethod(HttpMethod.GET);
+        connMock.setRequestProperty("Accept", "application/json");
+        connMock.setRequestProperty("Content-Type", "application/json");
+        connMock.setRequestProperty("X-FromAppId", "testId");
+        connMock.setRequestProperty("X-TransactionId", TransactionIdTracker.getNextTransactionId());
+    }
+
+    @Test
+    public void testSetStatusMessage_shouldSucceed() throws SvcLogicException, MalformedURLException {
+        SvcLogicContext ctx = new SvcLogicContext();
+        Map<String, String> parameters = new HashMap<String, String>();
+
+        parameters.put("key1", "ActivateSubnet failure, need to manually activate in EIPAM.");
+        aaiService.setStatusMethod(parameters, ctx);
+
+        Pattern r8601 = Pattern.compile(
+                "(\\d{4})-(\\d{2})-(\\d{2})T((\\d{2}):(\\d{2}):(\\d{2}))Z");
+        Matcher isoDate = r8601.matcher(ctx.getAttribute("aai-summary-status-message"));
+
+        assertTrue(isoDate.lookingAt());
+
+        assertTrue(ctx.getAttribute("aai-summary-status-message")
+                .contains("ActivateSubnet failure, need to manually activate in EIPAM."));
+    }
+
+     @Test(expected = SvcLogicException.class)
+    public void testSetStatusMessage_nullContext() throws SvcLogicException, MalformedURLException {
+        SvcLogicContext ctx = null;
+        Map<String, String> parameters = new HashMap<String, String>();
+
+        parameters.put("key1", "ActivateSubnet failure, need to manually activate in EIPAM.");
+        aaiService.setStatusMethod(parameters, ctx);
+    }
+}
diff --git a/aai-service/provider/src/test/resources/aaiclient.properties b/aai-service/provider/src/test/resources/aaiclient.properties
index c09be3a..35dec54 100755
--- a/aai-service/provider/src/test/resources/aaiclient.properties
+++ b/aai-service/provider/src/test/resources/aaiclient.properties
@@ -40,10 +40,8 @@
 #
 # Configuration file for A&AI Client
 #
-#org.onap.ccsdk.sli.adaptors.aai.uri=https://aai-ext1.test.att.com:8443
-#org.onap.ccsdk.sli.adaptors.aai.uri=https://aai-int1.test.att.com:8443
-#org.onap.ccsdk.sli.adaptors.aai.uri=https://mtanjv9aaas40.aic.cip.att.com:8443
-org.onap.ccsdk.sli.adaptors.aai.uri=https://aai-int2.test.att.com:8443
+org.onap.ccsdk.sli.adaptors.aai.uri=https://aai.api.simpledemo.openecomp.org:8443
+
 #
 connection.timeout=60000
 read.timeout=60000
diff --git a/aai-service/provider/src/test/resources/json/pserverJson.txt b/aai-service/provider/src/test/resources/json/pserverJson.txt
new file mode 100755
index 0000000..4b7f97d
--- /dev/null
+++ b/aai-service/provider/src/test/resources/json/pserverJson.txt
@@ -0,0 +1,97 @@
+{
+  "selflink": "https://mtinjvmsdn30.cip.att.com:8443/aai/v4/network/pservers/pserver/chcil129snd",
+  "hostname": "chcil129snd",
+  "ptnii-equip-name": "chcil129snd",
+  "equip-type": "server1",
+  "equip-vendor": "HP",
+  "equip-model": "DL380p-nd",
+  "fqdn": "chcilrsv129.chcil.sbcglobal.net",
+  "ipv4-oam-address": "12.80.1.83",
+  "serial-number": "n02kvcBczBI",
+  "in-maint":false,
+  "resource-version": "1455988020",
+  "purpose": "Gamma-VCE",
+  "relationship-list": {
+    "relationship": [
+      {
+        "related-to": "complex",
+        "related-link": "https://aai.infra.aic.att.net:8443/aai/v5/cloud-infrastructure/complexes/complex/CHCGILCL73W/",
+        "relationship-data": [
+          {
+            "relationship-key": "complex.physical-location-id",
+            "relationship-value": "CHCGILCL73W"
+          }
+        ]
+      }
+    ]
+  },
+  "p-interfaces": {
+    "p-interface": [
+      {
+        "interface-name": "p6p2-6/2",
+        "speed-value": "10000000",
+        "speed-units": "kbps",
+        "resource-version": "1455988020",
+        "relationship-list": {
+          "relationship": [
+            {
+              "related-to": "physical-link",
+              "related-link": "https://aai.infra.aic.att.net:8443/aai/v5/network/physical-links/physical-link/chcil129snd%3Ap6p2-6%2F2%7Cchciltax102%3Axe-1%2F0%2F29/",
+              "relationship-data": [
+                {
+                  "relationship-key": "physical-link.link-name",
+                  "relationship-value": "chcil129snd:p6p2-6/2|chciltax102:xe-1/0/29"
+                }
+              ]
+            }
+          ]
+        },
+        "l-interfaces": {
+          
+        }
+      },
+      {
+        "interface-name": "p6p1-6/1",
+        "speed-value": "10000000",
+        "speed-units": "kbps",
+        "resource-version": "1455988020",
+        "relationship-list": {
+          "relationship": [
+            {
+              "related-to": "physical-link",
+              "related-link": "https://aai.infra.aic.att.net:8443/aai/v5/network/physical-links/physical-link/chcil129snd%3Ap6p1-6%2F1%7Cchciltax101%3Axe-0%2F0%2F29/",
+              "relationship-data": [
+                {
+                  "relationship-key": "physical-link.link-name",
+                  "relationship-value": "chcil129snd:p6p1-6/1|chciltax101:xe-0/0/29"
+                }
+              ]
+            }
+          ]
+        },
+        "l-interfaces": {
+          
+        }
+      },
+      {
+        "interface-name": "em0-0/1",
+        "speed-value": "10000000",
+        "speed-units": "kbps",
+        "resource-version": "1455988020",
+        "relationship-list": {
+          "relationship": [
+            {
+              "related-to": "physical-link",
+              "related-link": "https://aai.infra.aic.att.net:8443/aai/v5/network/physical-links/physical-link/chcil129snd%3Aem0-0%2F1%7Cchciltax101%3Axe-0%2F0%2F59/",
+              "relationship-data": [
+                {
+                  
+                }
+              ]
+            }
+          ]
+        }
+      }
+    ]
+  }
+}
\ No newline at end of file