Refactored AaiJsonParserUtil
[holmes/common.git] / holmes-actions / src / test / java / org / onap / holmes / common / aai / AaiQuery4Ccvpn2Test.java
index 804df1f..346fe37 100644 (file)
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * org.onap.holmes.common.aai
  * ================================================================================
- * Copyright (C) 2018-2019 Huawei. All rights reserved.
+ * Copyright (C) 2018-2021 Huawei, ZTE. 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.
@@ -23,43 +23,40 @@ package org.onap.holmes.common.aai;
 import com.google.gson.JsonObject;
 import com.google.gson.JsonParser;
 import org.easymock.EasyMock;
-import org.junit.*;
-import org.junit.rules.ExpectedException;
+import org.junit.After;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
 import org.junit.runner.RunWith;
 import org.onap.holmes.common.aai.config.AaiConfig;
-import org.onap.holmes.common.exception.CorrelationException;
+import org.onap.holmes.common.utils.JerseyClient;
 import org.powermock.api.easymock.PowerMock;
 import org.powermock.core.classloader.annotations.PrepareForTest;
+import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
 import org.powermock.modules.junit4.PowerMockRunner;
 import org.powermock.reflect.Whitebox;
 
-import javax.ws.rs.client.Client;
-import javax.ws.rs.client.ClientBuilder;
-import javax.ws.rs.client.Invocation;
-import javax.ws.rs.client.WebTarget;
-import javax.ws.rs.core.MultivaluedHashMap;
-import javax.ws.rs.core.MultivaluedMap;
 import javax.ws.rs.core.Response;
 import java.io.*;
+import java.util.HashMap;
+import java.util.Map;
 
+import static org.hamcrest.MatcherAssert.assertThat;
+import static org.hamcrest.core.Is.is;
+import static org.hamcrest.core.IsNull.nullValue;
 import static org.onap.holmes.common.config.MicroServiceConfig.MSB_ADDR;
 
 
 @RunWith(PowerMockRunner.class)
-@PrepareForTest({ClientBuilder.class, Client.class, Invocation.Builder.class, WebTarget.class, Response.class})
+@PrepareForTest({JerseyClient.class})
+@SuppressStaticInitializationFor("org.onap.holmes.common.utils.JerseyClient")
 public class AaiQuery4Ccvpn2Test {
-
-    @Rule
-    public ExpectedException thrown = ExpectedException.none();
-
     private static JsonObject data;
 
     private static AaiQuery4Ccvpn2 aai = AaiQuery4Ccvpn2.newInstance();
 
-    private static MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>();
-    private static Client client;
-    private static WebTarget webTarget;
-    private static Invocation.Builder builder;
+    private static Map<String, Object> headers = new HashMap<>();
+    private static JerseyClient client;
     private static Response response;
 
     @BeforeClass
@@ -87,21 +84,19 @@ public class AaiQuery4Ccvpn2Test {
             }
         }
 
-        headers.add("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
-        headers.add("X-FromAppId", AaiConfig.X_FROMAPP_ID);
-        headers.add("Authorization", AaiConfig.getAuthenticationCredentials());
-        headers.add("Accept", "application/json");
-        headers.add("Content-Type", "application/json");
+        headers.put("X-TransactionId", AaiConfig.X_TRANSACTION_ID);
+        headers.put("X-FromAppId", AaiConfig.X_FROMAPP_ID);
+        headers.put("Authorization", AaiConfig.getAuthenticationCredentials());
+        headers.put("Accept", "application/json");
+        headers.put("Content-Type", "application/json");
         Whitebox.setInternalState(aai, "headers", headers);
     }
 
     @Before
-    public void before() {
-        PowerMock.mockStatic(ClientBuilder.class);
-        client = PowerMock.createMock(Client.class);
-        webTarget = PowerMock.createMock(WebTarget.class);
-        builder = PowerMock.createMock(Invocation.Builder.class);
+    public void before() throws Exception {
+        client = PowerMock.createMock(JerseyClient.class);
         response = PowerMock.createMock(Response.class);
+        PowerMock.expectNew(JerseyClient.class).andReturn(client).anyTimes();
     }
 
     @After
@@ -110,18 +105,15 @@ public class AaiQuery4Ccvpn2Test {
     }
 
     @Test
-    public void test_getServiceInstances_exception() throws CorrelationException {
+    public void test_getServiceInstances_exception() {
         mockGetMethod();
-        EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("site-resources").toString());
-        EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
+        EasyMock.expect(client.get(EasyMock.anyString())).andReturn(data.get("site-resources").toString());
 
         mockGetMethod();
-        EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("499hkg9933NNN").toString());
-        EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
+        EasyMock.expect(client.get(EasyMock.anyString())).andReturn(data.get("499hkg9933NNN").toString());
 
         mockGetMethod();
-        EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("499hkg9933NNN").toString());
-        EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
+        EasyMock.expect(client.get(EasyMock.anyString())).andReturn(data.get("499hkg9933NNN").toString());
 
         PowerMock.replayAll();
 
@@ -131,35 +123,19 @@ public class AaiQuery4Ccvpn2Test {
     }
 
     @Test
-    public void test_getServiceInstancesNull_exception() throws CorrelationException {
-        mockGetMethod();
-        EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("site-resources1").toString());
-        EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
-
-        mockGetMethod();
-        EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("499hkg9933NNN").toString());
-        EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
-
+    public void test_getServiceInstancesNull_exception() {
         mockGetMethod();
-        EasyMock.expect(response.readEntity(String.class)).andReturn(data.get("499hkg9933NNN").toString());
-        EasyMock.expect(response.getStatusInfo()).andReturn(Response.Status.OK);
+        EasyMock.expect(client.get(EasyMock.anyString())).andReturn(data.get("site-resources1").toString());
 
         PowerMock.replayAll();
 
-        aai.getSiteServiceInstance("HkHubONSDEMOSZHKCustomer");
-    }
-
+        assertThat(aai.getSiteServiceInstance("HkHubONSDEMOSZHKCustomer"), is(nullValue()));
 
-    private void mockGetMethod() {
-        initCommonMock();
-        EasyMock.expect(builder.get()).andReturn(response);
+        PowerMock.verifyAll();
     }
 
-    private void initCommonMock() {
-        EasyMock.expect(ClientBuilder.newClient()).andReturn(client);
-        EasyMock.expect(client.target(EasyMock.anyObject(String.class))).andReturn(webTarget);
-        EasyMock.expect(webTarget.path(EasyMock.anyObject(String.class))).andReturn(webTarget);
-        EasyMock.expect(webTarget.request()).andReturn(builder);
-        EasyMock.expect(builder.headers(headers)).andReturn(builder);
+    private void mockGetMethod() {
+        EasyMock.expect(client.headers(headers)).andReturn(client);
+        EasyMock.expect(client.path(EasyMock.anyString())).andReturn(client);
     }
 }