Fixed the CLM Issues
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / aai / AaiQuery4CcvpnTest.java
index c7c32eb..f1855f9 100644 (file)
@@ -1,5 +1,5 @@
 /**
- * Copyright 2018 ZTE Corporation.
+ * Copyright 2018-2020 ZTE Corporation.
  * <p>
  * 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
 
 package org.onap.holmes.common.aai;
 
-import com.alibaba.fastjson.JSONArray;
-import com.alibaba.fastjson.JSONObject;
+import com.google.gson.JsonObject;
+import com.google.gson.JsonParser;
 import org.easymock.EasyMock;
+import org.glassfish.jersey.client.HttpUrlConnectorProvider;
 import org.junit.*;
 import org.junit.rules.ExpectedException;
 import org.junit.runner.RunWith;
@@ -49,9 +50,9 @@ public class AaiQuery4CcvpnTest {
     @Rule
     public ExpectedException thrown = ExpectedException.none();
 
-    private static JSONObject data;
+    private static JsonObject data;
 
-    private static AaiQuery4Ccvpn aai = new AaiQuery4Ccvpn();
+    private static AaiQuery4Ccvpn aai = AaiQuery4Ccvpn.newInstance();
 
     private static MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
     private static Client client;
@@ -69,7 +70,7 @@ public class AaiQuery4CcvpnTest {
             reader = new BufferedReader(new FileReader(file));
             StringBuilder sb = new StringBuilder();
             reader.lines().forEach(l -> sb.append(l));
-            data = JSONObject.parseObject(sb.toString());
+            data = JsonParser.parseString(sb.toString()).getAsJsonObject();
         } catch (FileNotFoundException e) {
             // Do nothing
         } catch (IOException e) {
@@ -88,6 +89,7 @@ public class AaiQuery4CcvpnTest {
         headers.add("X-FromAppId", AaiConfig.X_FROMAPP_ID);
         headers.add("Authorization", AaiConfig.getAuthenticationCredentials());
         headers.add("Accept", "application/json");
+        headers.add("Content-Type", "application/json");
         Whitebox.setInternalState(aai, "headers", headers);
     }
 
@@ -119,11 +121,12 @@ public class AaiQuery4CcvpnTest {
     }
 
     @Test
-    public void test_getLogicLink_exception() {
+    public void test_getLogicLink_exception() throws CorrelationException {
         mockGetMethod();
         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
+        EasyMock.expect(response.readEntity(String.class)).andReturn("Error!");
 
-        thrown.expect(RuntimeException.class);
+        thrown.expect(CorrelationException.class);
 
         PowerMock.replayAll();
 
@@ -136,9 +139,9 @@ public class AaiQuery4CcvpnTest {
     }
 
     @Test
-    public void test_getLogicLink() {
+    public void test_getLogicLink() throws CorrelationException {
         mockGetMethod();
-        EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("logic-link"));
+        EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("logic-link").toString());
         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
 
         PowerMock.replayAll();
@@ -154,102 +157,113 @@ public class AaiQuery4CcvpnTest {
     @Test
     public void test_getServiceInstances_exception() {
         mockGetMethod();
-        EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("vpn-binding"));
+        EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("vpn-binding").toString());
         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
 
         mockGetMethod();
-        EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("connectivity"));
+        EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("connectivity").toString());
         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
 
         mockGetMethod();
-        EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instance-by-connectivity"));
+        EasyMock.expect(response.readEntity(String.class))
+                .andReturn(data.get("service-instance-by-connectivity").toString());
         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
 
         mockGetMethod();
-        EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instances-by-service-type"));
+        EasyMock.expect(response.readEntity(String.class))
+                .andReturn(data.get("service-instances-by-service-type").toString());
+        EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
+
+        mockGetMethod();
+        EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("service-instance").toString());
         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
 
         thrown.expect(RuntimeException.class);
 
         PowerMock.replayAll();
 
-        JSONArray instances = aai.getServiceInstances("network-1", "pnf-1", "interface-1", "DOWN");
+        JsonObject instance = aai.getServiceInstance("network-1", "pnf-1", "interface-1", "DOWN");
 
         PowerMock.verifyAll();
 
-        assertThat(instances, equalTo("logic-link-1"));
+        assertThat(instance, equalTo("logic-link-1"));
 
     }
 
     @Test
-    public void test_getServiceInstances() {
+    public void test_getServiceInstance() {
         mockGetMethod();
-        EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("vpn-binding"));
+        EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("vpn-binding").toString());
         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
 
         mockGetMethod();
-        EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("connectivity"));
+        EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("connectivity").toString());
         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
 
         mockGetMethod();
-        EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instance-by-connectivity"));
+        EasyMock.expect(response.readEntity(String.class))
+                .andReturn(data.get("service-instance-by-connectivity").toString());
         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
 
         mockGetMethod();
-        EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instances-by-service-type"));
+        EasyMock.expect(response.readEntity(String.class))
+                .andReturn(data.get("service-instances-by-service-type").toString());
         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
 
         PowerMock.replayAll();
 
-        JSONArray instances = aai.getServiceInstances("network-1", "pnf-1", "interface-1", "DOWN");
+        JsonObject instance = aai.getServiceInstance("network-1", "pnf-1", "interface-1", "DOWN");
 
         PowerMock.verifyAll();
 
-        assertThat(instances.getJSONObject(0).getString("service-instance-id"), equalTo("some id 1"));
-        assertThat(instances.getJSONObject(1).getString("service-instance-id"), equalTo("some id 2"));
-        assertThat(instances.getJSONObject(2).getString("service-instance-id"), equalTo("some id 3"));
+        assertThat(instance.get("service-instance-id").getAsString(), equalTo("some id 1"));
+        assertThat(instance.get("globalSubscriberId").getAsString(), equalTo("e151059a-d924-4629-845f-264db19e50b4"));
+        assertThat(instance.get("serviceType").getAsString(), equalTo("volte"));
     }
 
     @Test
-    public void test_getServiceInstances_1() throws Exception {
+    public void test_getServiceInstance_1() throws Exception {
         mockGetMethod();
-        EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instances-by-service-type"));
+        EasyMock.expect(response.readEntity(String.class))
+                .andReturn(data.get("service-instances-by-service-type").toString());
         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
 
         PowerMock.replayAll();
 
-        JSONArray instances = (JSONArray)Whitebox.invokeMethod(aai, "getServiceInstances",
-                "custom-1", "service-type-1");
+        JsonObject instance = Whitebox.invokeMethod(aai, "getServiceInstance",
+                                                    "custom-1", "service-type-1");
 
         PowerMock.verifyAll();
 
-        assertThat(instances.getJSONObject(0).getString("service-instance-id"), equalTo("some id 1"));
-        assertThat(instances.getJSONObject(1).getString("service-instance-id"), equalTo("some id 2"));
-        assertThat(instances.getJSONObject(2).getString("service-instance-id"), equalTo("some id 3"));
+        assertThat(instance.get("service-instance-id").getAsString(), equalTo("some id 1"));
     }
 
     @Test
-    public void test_getServiceInstances_1_exception() throws Exception {
+    public void test_getServiceInstance_1_exception() throws Exception {
         mockGetMethod();
-        EasyMock.expect(response.getEntity()).andReturn(data.getJSONObject("service-instances-by-service-type"));
+        EasyMock.expect(response.readEntity(String.class)).andReturn("Failed to get the service instance by type.");
         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
 
         thrown.expect(CorrelationException.class);
 
         PowerMock.replayAll();
 
-        JSONArray instances = (JSONArray)Whitebox.invokeMethod(aai, "getServiceInstances",
-                "custom-1", "service-type-1");
+        JsonObject instance = Whitebox.invokeMethod(aai, "getServiceInstance",
+                                                    "custom-1", "service-type-1");
 
         PowerMock.verifyAll();
 
-        assertThat(instances.getJSONObject(0).getString("service-instance-id"), equalTo("some id 1"));
-        assertThat(instances.getJSONObject(1).getString("service-instance-id"), equalTo("some id 2"));
-        assertThat(instances.getJSONObject(2).getString("service-instance-id"), equalTo("some id 3"));
+        assertThat(instance.get("service-instance-id").getAsString(), equalTo("some id 1"));
+        assertThat(instance.get("service-instance-id").getAsString(), equalTo("some id 2"));
+        assertThat(instance.get("service-instance-id").getAsString(), equalTo("some id 3"));
     }
 
     @Test
     public void test_updateTerminalPointStatus() throws CorrelationException {
+        mockGetMethod();
+        EasyMock.expect(response.readEntity(String.class)).andReturn(data.toString());
+        EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
+
         mockPatchMethod();
         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
 
@@ -262,8 +276,13 @@ public class AaiQuery4CcvpnTest {
 
     @Test
     public void test_updateTerminalPointStatus_exception() throws CorrelationException {
+        mockGetMethod();
+        EasyMock.expect(response.readEntity(String.class)).andReturn(data.toString());
+        EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
+
         mockPatchMethod();
         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
+        EasyMock.expect(response.readEntity(String.class)).andReturn("Failed to update the TP information.");
 
         thrown.expect(CorrelationException.class);
 
@@ -276,6 +295,10 @@ public class AaiQuery4CcvpnTest {
 
     @Test
     public void test_updateLogicLinkStatus() throws CorrelationException {
+        mockGetMethod();
+        EasyMock.expect(response.readEntity(String.class)).andReturn(data.toString());
+        EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
+
         mockPatchMethod();
         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
 
@@ -288,8 +311,13 @@ public class AaiQuery4CcvpnTest {
 
     @Test
     public void test_updateLogicLinkStatus_exception() throws CorrelationException {
+        mockGetMethod();
+        EasyMock.expect(response.readEntity(String.class)).andReturn(data.toString());
+        EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
+
         mockPatchMethod();
         EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.NOT_FOUND).times(2);
+        EasyMock.expect(response.readEntity(String.class)).andReturn("Failed to update the logic link information.");
 
         thrown.expect(CorrelationException.class);
 
@@ -308,7 +336,10 @@ public class AaiQuery4CcvpnTest {
 
     private void mockPatchMethod() {
         initCommonMock();
-        EasyMock.expect(builder.method(EasyMock.anyObject(String.class), EasyMock.anyObject(Entity.class))).andReturn(response);
+        Invocation invocation = PowerMock.createMock(Invocation.class);
+        EasyMock.expect(builder.build(EasyMock.anyObject(String.class), EasyMock.anyObject(Entity.class))).andReturn(invocation);
+        EasyMock.expect(invocation.property(HttpUrlConnectorProvider.SET_METHOD_WORKAROUND, true)).andReturn(invocation);
+        EasyMock.expect(invocation.invoke()).andReturn(response);
     }
 
     private void initCommonMock() {