diff options
author | 2021-06-08 18:48:01 +0800 | |
---|---|---|
committer | 2021-06-08 18:48:01 +0800 | |
commit | bb55dbd5674c5a5165592387de13a2a211f90ec9 (patch) | |
tree | 581d3fc0f8fd00521661552e25364c7362a3cee3 /holmes-actions/src/test | |
parent | be5ca2b68e80688677d75e5f688199511745570b (diff) |
Refactored AaiJsonParserUtil
Change-Id: I7c5458e945d860ef75e5defd803600f56ec718e9
Issue-ID: HOLMES-453
Signed-off-by: GuangrongFu <fu.guangrong@zte.com.cn>
Diffstat (limited to 'holmes-actions/src/test')
-rw-r--r-- | holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQuery4Ccvpn2Test.java | 94 | ||||
-rw-r--r-- | holmes-actions/src/test/java/org/onap/holmes/common/utils/JerseyClientTest.java | 3 |
2 files changed, 37 insertions, 60 deletions
diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQuery4Ccvpn2Test.java b/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQuery4Ccvpn2Test.java index 804df1f..346fe37 100644 --- a/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQuery4Ccvpn2Test.java +++ b/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQuery4Ccvpn2Test.java @@ -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); } } diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/utils/JerseyClientTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/utils/JerseyClientTest.java index df1e2a2..b0b5714 100644 --- a/holmes-actions/src/test/java/org/onap/holmes/common/utils/JerseyClientTest.java +++ b/holmes-actions/src/test/java/org/onap/holmes/common/utils/JerseyClientTest.java @@ -43,7 +43,8 @@ import static org.junit.Assert.assertThat; @PowerMockIgnore({"javax.net.ssl.*", "javax.security.*"}) public class JerseyClientTest { - private final String ERROR_MSG = "Status code: <404>. Message: Failed to get response from the server. Info: Error"; + private final String ERROR_MSG = "Failed to get response from the server. \nURL: http://www.onap.org/holmes/test" + + "\nCause: Not Found\nResponse body: Error"; private JerseyClient jerseyClient = JerseyClient.newInstance(); private String url = "http://www.onap.org/holmes/test"; |