diff options
Diffstat (limited to 'holmes-actions/src/test')
7 files changed, 585 insertions, 217 deletions
diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryMdonsTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryMdonsTest.java index 0fd577c..21dd44a 100644 --- a/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryMdonsTest.java +++ b/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryMdonsTest.java @@ -14,44 +14,35 @@ package org.onap.holmes.common.aai; -import static org.junit.Assert.assertEquals; -import static org.onap.holmes.common.config.MicroServiceConfig.MSB_ADDR; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileReader; -import java.io.IOException; -import java.util.HashMap; -import java.util.Map; - -import javax.ws.rs.core.MultivaluedHashMap; -import javax.ws.rs.core.MultivaluedMap; - -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.junit.After; -import org.junit.Before; -import org.junit.BeforeClass; -import org.junit.Rule; -import org.junit.Test; +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.runner.RunWith; import org.onap.holmes.common.aai.config.AaiConfig; import org.onap.holmes.common.config.MicroServiceConfig; -import org.onap.holmes.common.utils.HttpsUtils; +import org.onap.holmes.common.utils.JerseyClient; import org.powermock.api.easymock.PowerMock; import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.Whitebox; -import com.google.gson.JsonObject; -import com.google.gson.JsonParser; +import javax.ws.rs.client.Entity; +import javax.ws.rs.core.MultivaluedHashMap; +import javax.ws.rs.core.MultivaluedMap; +import java.io.*; +import java.util.HashMap; +import java.util.Map; + +import static org.easymock.EasyMock.anyObject; +import static org.junit.Assert.assertEquals; +import static org.onap.holmes.common.config.MicroServiceConfig.MSB_ADDR; @RunWith(PowerMockRunner.class) @PowerMockIgnore("javax.net.ssl.*") -@PrepareForTest({AaiQueryMdons.class, HttpsUtils.class, MicroServiceConfig.class, HttpGet.class}) +@PrepareForTest({AaiQueryMdons.class, MicroServiceConfig.class}) public class AaiQueryMdonsTest { @Rule @@ -62,7 +53,6 @@ public class AaiQueryMdonsTest { private static JsonObject data; - private HttpResponse httpResponse; private static final String AAI_ADDR = "https://aai.onap:8443/aai/v19/"; @BeforeClass @@ -101,7 +91,6 @@ public class AaiQueryMdonsTest { @Before public void before() { - httpResponse = PowerMock.createMock(HttpResponse.class); Whitebox.setInternalState(aaiMdonsQuery, "headers", headers); } @@ -158,12 +147,16 @@ public class AaiQueryMdonsTest { String response = "{\"link-name\":\"link1\",\"in-maint\":false,\"link-type\":\"inter-domain\",\"available-capacity\":\"ODU2\",\"resource-version\":\"1584338211407\",\"operational-status\":\"down\"}"; - aaiMdonsQuery = PowerMock.createPartialMock(AaiQueryMdons.class, "getResponse", "put"); + aaiMdonsQuery = PowerMock.createPartialMock(AaiQueryMdons.class, "getResponse"); + + JerseyClient mockedClient = PowerMock.createMock(JerseyClient.class); + PowerMock.expectNew(JerseyClient.class).andReturn(mockedClient); + EasyMock.expect(mockedClient.headers(anyObject(Map.class))).andReturn(mockedClient); + EasyMock.expect(mockedClient.put(anyObject(String.class), anyObject(Entity.class))).andReturn(""); PowerMock.expectPrivate(aaiMdonsQuery, "getResponse", accessService) .andReturn(data.get("get-access-service").toString()); PowerMock.expectPrivate(aaiMdonsQuery, "getResponse", linkUrl).andReturn(data.get("get-inter-link").toString()); - PowerMock.expectPrivate(aaiMdonsQuery, "put", linkUrl, response).andReturn(httpResponse); PowerMock.replayAll(); Whitebox.invokeMethod(aaiMdonsQuery, "updateLinksForAccessService", accessMap); diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryTest.java index 0340e04..0eec082 100644 --- a/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryTest.java +++ b/holmes-actions/src/test/java/org/onap/holmes/common/aai/AaiQueryTest.java @@ -16,37 +16,34 @@ package org.onap.holmes.common.aai; -import org.apache.http.HttpResponse; -import org.apache.http.client.methods.HttpGet; -import org.apache.http.impl.client.CloseableHttpClient; import org.easymock.EasyMock; import org.junit.BeforeClass; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; -import org.onap.holmes.common.aai.config.AaiConfig; import org.onap.holmes.common.aai.entity.VmEntity; import org.onap.holmes.common.aai.entity.VnfEntity; import org.onap.holmes.common.config.MicroServiceConfig; import org.onap.holmes.common.exception.CorrelationException; -import org.onap.holmes.common.utils.HttpsUtils; +import org.onap.holmes.common.exception.HttpException; +import org.onap.holmes.common.utils.JerseyClient; import org.powermock.api.easymock.PowerMock; import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.Whitebox; -import java.util.HashMap; import java.util.Map; import static org.easymock.EasyMock.anyObject; -import static org.easymock.EasyMock.expect; +import static org.hamcrest.core.Is.is; import static org.hamcrest.core.IsEqual.equalTo; +import static org.hamcrest.core.IsNull.nullValue; import static org.junit.Assert.assertThat; -@PrepareForTest({AaiQuery.class, HttpsUtils.class, MicroServiceConfig.class, HttpGet.class}) +@PrepareForTest({AaiQuery.class, MicroServiceConfig.class}) @PowerMockIgnore("javax.net.ssl.*") @RunWith(PowerMockRunner.class) public class AaiQueryTest { @@ -75,7 +72,7 @@ public class AaiQueryTest { VnfEntity vnfEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVnfData", "test1", "test2"); PowerMock.verifyAll(); - assertThat(vnfEntity == null, equalTo(true)); + assertThat(vnfEntity, is(nullValue())); } @Test @@ -100,64 +97,24 @@ public class AaiQueryTest { aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVmResourceLinks"); aaiResponseUtil = new AaiResponseUtil(); Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil); - PowerMock.mockStatic(HttpsUtils.class); - Map<String, String> headers = new HashMap<>(); - 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"); + String url = "https://aai.onap:8443/aai/v11/cloud-infrastructure"; - HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class); - CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class); - expect(HttpsUtils.getHttpsClient(30000)).andReturn(httpClient); - HttpGet httpGet = new HttpGet(url); - PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet); - expect(HttpsUtils.get(anyObject(HttpGet.class), anyObject(Map.class), - anyObject(CloseableHttpClient.class))).andReturn(httpResponse); - expect(HttpsUtils.extractResponseEntity(httpResponse)).andReturn("{}"); + + JerseyClient mockedClient = PowerMock.createMock(JerseyClient.class); + PowerMock.expectNew(JerseyClient.class).andReturn(mockedClient); + EasyMock.expect(mockedClient.headers(EasyMock.anyObject(Map.class))).andReturn(mockedClient); + EasyMock.expect(mockedClient.get(url)).andReturn("{}"); PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2") .andReturn("/aai/v11/cloud-infrastructure"); - PowerMock.expectPrivate(httpClient, "close"); - EasyMock.expectLastCall(); - PowerMock.replayAll(); - VmEntity vmEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2"); - PowerMock.verifyAll(); - - assertThat(vmEntity == null, equalTo(true)); - } - @Test - public void testAaiQuery_getAaiVmData_httpsutils_exception() throws Exception { - PowerMock.resetAll(); - thrown.expect(CorrelationException.class); - thrown.expectMessage("Failed to get data from aai"); - aaiQuery = PowerMock.createPartialMock(AaiQuery.class, "getVmResourceLinks"); + PowerMock.replayAll(); - aaiResponseUtil = new AaiResponseUtil(); - Whitebox.setInternalState(aaiQuery, "aaiResponseUtil", aaiResponseUtil); + VmEntity vmEntity = Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2"); - PowerMock.mockStatic(HttpsUtils.class); - Map<String, String> headers = new HashMap<>(); - 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"); - String url = "https://aai.onap:8443/aai/v11/cloud-infrastructure"; - CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class); - EasyMock.expect(HttpsUtils.getHttpsClient(30000)).andReturn(httpClient); - HttpGet httpGet = new HttpGet(url); - PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet); - EasyMock.expect(HttpsUtils.get(anyObject(HttpGet.class), anyObject(Map.class), - anyObject(CloseableHttpClient.class))).andThrow(new CorrelationException("")); - PowerMock.mockStatic(MicroServiceConfig.class); - PowerMock.expectPrivate(aaiQuery, "getVmResourceLinks", "test1", "test2") - .andReturn("/aai/v11/cloud-infrastructure"); - PowerMock.expectPrivate(httpClient, "close"); - EasyMock.expectLastCall(); - PowerMock.replayAll(); - Whitebox.invokeMethod(aaiQuery, "getAaiVmData", "test1", "test2"); PowerMock.verifyAll(); + + assertThat(vmEntity, is(nullValue())); } @Test @@ -178,7 +135,6 @@ public class AaiQueryTest { assertThat(resource, equalTo("le-vserver-id-val-51834")); } - @Test public void testAaiQuery_getResourceLinksResponse() throws Exception { PowerMock.resetAll(); @@ -215,24 +171,13 @@ public class AaiQueryTest { public void testAaiQuery_getResponse_ok() throws Exception { PowerMock.resetAll(); aaiQuery = new AaiQuery(); - PowerMock.mockStatic(HttpsUtils.class); - Map<String, String> headers = new HashMap<>(); - 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"); + String url = "host_url"; - HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class); - CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class); - expect(HttpsUtils.getHttpsClient(30000)).andReturn(httpClient); - HttpGet httpGet = new HttpGet(url); - PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet); - expect(HttpsUtils.get(anyObject(HttpGet.class), anyObject(Map.class), - anyObject(CloseableHttpClient.class))).andReturn(httpResponse); - expect(HttpsUtils.extractResponseEntity(httpResponse)).andReturn(""); - PowerMock.expectPrivate(httpClient, "close"); - EasyMock.expectLastCall(); + JerseyClient mockedClient = PowerMock.createMock(JerseyClient.class); + PowerMock.expectNew(JerseyClient.class).andReturn(mockedClient); + EasyMock.expect(mockedClient.headers(EasyMock.anyObject(Map.class))).andReturn(mockedClient); + EasyMock.expect(mockedClient.get(url)).andReturn(""); PowerMock.replayAll(); String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url"); @@ -242,28 +187,21 @@ public class AaiQueryTest { } @Test - public void testAaiQuery_getResponse_exceptioin() throws Exception { + public void testAaiQuery_getResponse_exception() throws Exception { PowerMock.resetAll(); thrown.expect(CorrelationException.class); thrown.expectMessage("Failed to get data from aai"); aaiQuery = new AaiQuery(); - PowerMock.mockStatic(HttpsUtils.class); - Map<String, String> headers = new HashMap<>(); - 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"); String url = "host_url"; - CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class); - expect(HttpsUtils.getHttpsClient(30000)).andReturn(httpClient); - HttpGet httpGet = new HttpGet(url); - PowerMock.expectNew(HttpGet.class, url).andReturn(httpGet); - expect(HttpsUtils.get(httpGet, headers, httpClient)).andThrow(new CorrelationException("")); - PowerMock.expectPrivate(httpClient, "close"); - EasyMock.expectLastCall(); + + JerseyClient mockedClient = PowerMock.createMock(JerseyClient.class); + PowerMock.expectNew(JerseyClient.class).andReturn(mockedClient); + EasyMock.expect(mockedClient.headers(EasyMock.anyObject(Map.class))).andReturn(mockedClient); + EasyMock.expect(mockedClient.get(url)).andThrow(new HttpException(404, "Not Found")); + PowerMock.replayAll(); - String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", "host_url"); + String resource = Whitebox.invokeMethod(aaiQuery, "getResponse", url); PowerMock.verifyAll(); assertThat(resource, equalTo("")); } diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/DmaapServiceTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/DmaapServiceTest.java index 76c4a45..c03bede 100644 --- a/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/DmaapServiceTest.java +++ b/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/DmaapServiceTest.java @@ -263,7 +263,8 @@ public class DmaapServiceTest { PolicyMsg policyMsg = new PolicyMsg(); policyMsg.setClosedLoopEventStatus(EVENT_STATUS.ONSET); PowerMock.expectNew(Publisher.class).andReturn(publisher); - EasyMock.expect(publisher.publish(policyMsg)).andReturn(true); + publisher.publish(policyMsg); + EasyMock.expectLastCall(); DcaeConfigurationsCache.setDcaeConfigurations( DcaeConfigurationParser.parse(readConfigurationsFromFile("dcae.config.json"))); @@ -283,7 +284,8 @@ public class DmaapServiceTest { policyMsg.setRequestID("testRequestid"); uniqueRequestIdCache.put("testAlarmId", "testRequestid"); PowerMock.expectNew(Publisher.class).andReturn(publisher); - EasyMock.expect(publisher.publish(policyMsg)).andReturn(true); + publisher.publish(policyMsg); + EasyMock.expectLastCall(); DcaeConfigurationsCache.setDcaeConfigurations( DcaeConfigurationParser.parse(readConfigurationsFromFile("dcae.config.json"))); diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/PublisherTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/PublisherTest.java index 0e7fe3d..8ad154d 100644 --- a/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/PublisherTest.java +++ b/holmes-actions/src/test/java/org/onap/holmes/common/dmaap/PublisherTest.java @@ -15,73 +15,39 @@ */ package org.onap.holmes.common.dmaap; -import org.apache.http.HttpResponse; -import org.apache.http.HttpStatus; -import org.apache.http.StatusLine; -import org.apache.http.client.methods.HttpPost; -import org.apache.http.entity.StringEntity; -import org.apache.http.impl.client.CloseableHttpClient; +import org.apache.commons.lang3.StringUtils; import org.easymock.EasyMock; -import org.junit.Rule; import org.junit.Test; -import org.junit.rules.ExpectedException; import org.junit.runner.RunWith; import org.onap.holmes.common.dmaap.entity.PolicyMsg; -import org.onap.holmes.common.exception.CorrelationException; -import org.onap.holmes.common.utils.HttpsUtils; +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.PowerMockIgnore; import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.reflect.internal.WhiteboxImpl; -import java.util.HashMap; +import javax.ws.rs.client.Entity; -import static org.hamcrest.CoreMatchers.is; -import static org.junit.Assert.assertThat; +import static org.easymock.EasyMock.anyObject; -@PrepareForTest({HttpsUtils.class, HttpResponse.class, Publisher.class}) @RunWith(PowerMockRunner.class) +@PowerMockIgnore({"javax.net.ssl.*", "javax.security.*"}) public class PublisherTest { - @Rule - public ExpectedException thrown = ExpectedException.none(); - - private static final String URL = "http://localhost/dmaapTopic"; - @Test - public void publish_exception() throws Exception { + public void publish_normal() { Publisher publisher = new Publisher(); - publisher.setUrl(URL); - - thrown.expect(CorrelationException.class); - thrown.expectMessage("Failed to connect to DCAE"); + publisher.setUrl("http://localhost/dmaapTopic"); - publisher.publish(new PolicyMsg()); - } - - @Test - public void publish_normal() throws Exception { - - Publisher publisher = new Publisher(); - publisher.setUrl(URL); - - PowerMock.mockStatic(HttpsUtils.class); - CloseableHttpClient httpClient = PowerMock.createMock(CloseableHttpClient.class); - EasyMock.expect(HttpsUtils.getConditionalHttpsClient(HttpsUtils.DEFUALT_TIMEOUT)).andReturn(httpClient); - HttpResponse httpResponse = PowerMock.createMock(HttpResponse.class); - EasyMock.expect(HttpsUtils - .post(EasyMock.anyObject(HttpPost.class), EasyMock.anyObject(HashMap.class), - EasyMock.anyObject(HashMap.class), EasyMock.anyObject(StringEntity.class), - EasyMock.anyObject(CloseableHttpClient.class))).andReturn(httpResponse); - StatusLine statusLine = PowerMock.createMock(StatusLine.class); - EasyMock.expect(httpResponse.getStatusLine()).andReturn(statusLine); - EasyMock.expect(statusLine.getStatusCode()).andReturn(HttpStatus.SC_OK); - httpClient.close(); - EasyMock.expectLastCall(); + JerseyClient mockedJerseyClient = PowerMock.createMock(JerseyClient.class); + WhiteboxImpl.setInternalState(publisher, "client", mockedJerseyClient); + EasyMock.expect(mockedJerseyClient.post(anyObject(String.class), anyObject(Entity.class))) + .andReturn(StringUtils.EMPTY); PowerMock.replayAll(); - assertThat(publisher.publish(new PolicyMsg()), is(true)); + publisher.publish(new PolicyMsg()); PowerMock.verifyAll(); } diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/exception/HttpExceptionTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/exception/HttpExceptionTest.java new file mode 100644 index 0000000..a786c9f --- /dev/null +++ b/holmes-actions/src/test/java/org/onap/holmes/common/exception/HttpExceptionTest.java @@ -0,0 +1,32 @@ +/** + * Copyright 2021 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 + * <p> + * http://www.apache.org/licenses/LICENSE-2.0 + * <p> + * 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. + */ + +package org.onap.holmes.common.exception; + +import org.junit.Test; + +public class HttpExceptionTest { + + @Test + public void exception_without_cause() { + HttpException e = new HttpException(200, "OK"); + } + + @Test + public void exception_with_cause() { + HttpException e = new HttpException(404, "Not Found", new Exception()); + } +}
\ No newline at end of file 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 6c95ccb..0183740 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 @@ -1,5 +1,5 @@ /** - * Copyright 2020 ZTE Corporation. + * Copyright 2020 - 2021 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. @@ -16,32 +16,446 @@ package org.onap.holmes.common.utils; +import org.easymock.EasyMock; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.junit.runner.RunWith; +import org.onap.holmes.common.exception.HttpException; +import org.powermock.api.easymock.PowerMock; +import org.powermock.core.classloader.annotations.PowerMockIgnore; +import org.powermock.modules.junit4.PowerMockRunner; import org.powermock.reflect.internal.WhiteboxImpl; +import javax.ws.rs.client.Client; +import javax.ws.rs.client.Entity; +import javax.ws.rs.client.Invocation; +import javax.ws.rs.client.WebTarget; +import javax.ws.rs.core.MediaType; +import javax.ws.rs.core.Response; +import java.util.HashMap; +import java.util.Map; + +import static org.hamcrest.core.IsEqual.equalTo; +import static org.junit.Assert.assertThat; + +@RunWith(PowerMockRunner.class) +@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 JerseyClient jerseyClient = new JerseyClient(); + private String url = "http://www.onap.org/holmes/test"; + @Rule + private ExpectedException exception = ExpectedException.none(); + + @Test + public void get_normal() { + Client mockedClient = PowerMock.createMock(Client.class); + WebTarget mockedTarget = PowerMock.createMock(WebTarget.class); + Invocation.Builder mockedBuilder = PowerMock.createPartialMock(Invocation.Builder.class, "get"); + Response mockedResponse = PowerMock.createMock(Response.class); + + EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget); + EasyMock.expect(mockedTarget.path("test")).andReturn(mockedTarget); + EasyMock.expect(mockedTarget.queryParam("ut", true)).andReturn(mockedTarget); + EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder); + EasyMock.expect(mockedBuilder.header("Accept", MediaType.APPLICATION_JSON)).andReturn(mockedBuilder); + EasyMock.expect(mockedBuilder.get()).andReturn(mockedResponse); + EasyMock.expect(mockedResponse.getStatus()).andReturn(200); + EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Normal"); + + WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient); + + PowerMock.replayAll(); + + assertThat(jerseyClient.path("test") + .header("Accept", MediaType.APPLICATION_JSON) + .queryParam("ut", true) + .get(url), equalTo("Normal")); + + PowerMock.verifyAll(); + } + + @Test + public void get_normal_with_class() { + Client mockedClient = PowerMock.createMock(Client.class); + WebTarget mockedTarget = PowerMock.createPartialMock(WebTarget.class, "request"); + Invocation.Builder mockedBuilder = PowerMock.createPartialMock(Invocation.Builder.class, "get"); + Response mockedResponse = PowerMock.createMock(Response.class); + Bean bean = new Bean("name", "holmes"); + + EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget); + EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder); + EasyMock.expect(mockedBuilder.get()).andReturn(mockedResponse); + EasyMock.expect(mockedResponse.getStatus()).andReturn(200); + EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn(GsonUtil.beanToJson(bean)); + + WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient); + + PowerMock.replayAll(); + + assertThat(jerseyClient.get(url, Bean.class), equalTo(bean)); + + PowerMock.verifyAll(); + } + + @Test + public void get_normal_with_null_class_info() { + Client mockedClient = PowerMock.createMock(Client.class); + WebTarget mockedTarget = PowerMock.createPartialMock(WebTarget.class, "request"); + Invocation.Builder mockedBuilder = PowerMock.createPartialMock(Invocation.Builder.class, "get"); + Response mockedResponse = PowerMock.createMock(Response.class); + + EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget); + EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder); + EasyMock.expect(mockedBuilder.get()).andReturn(mockedResponse); + EasyMock.expect(mockedResponse.getStatus()).andReturn(200); + EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Normal"); + + WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient); + + PowerMock.replayAll(); + + assertThat(jerseyClient.get(url, null), equalTo("Normal")); + + PowerMock.verifyAll(); + } + + @Test + public void get_fail() { + Client mockedClient = PowerMock.createMock(Client.class); + WebTarget mockedTarget = PowerMock.createPartialMock(WebTarget.class, "request"); + Invocation.Builder mockedBuilder = PowerMock.createPartialMock(Invocation.Builder.class, "get"); + Response mockedResponse = PowerMock.createMock(Response.class); + + EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget); + EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder); + EasyMock.expect(mockedBuilder.get()).andReturn(mockedResponse); + EasyMock.expect(mockedResponse.getStatus()).andReturn(404); + EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Error"); + + WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient); + + exception.expect(HttpException.class); + exception.expectMessage(ERROR_MSG); + + PowerMock.replayAll(); + + jerseyClient.get(url); + + PowerMock.verifyAll(); + } + + @Test + public void post_normal_no_body() { + Client mockedClient = PowerMock.createMock(Client.class); + WebTarget mockedTarget = PowerMock.createPartialMock(WebTarget.class, "request"); + Invocation.Builder mockedBuilder = PowerMock.createPartialMock(Invocation.Builder.class, "post", Entity.class); + Response mockedResponse = PowerMock.createMock(Response.class); + + EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget); + EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder); + EasyMock.expect(mockedBuilder.post(null)).andReturn(mockedResponse); + EasyMock.expect(mockedResponse.getStatus()).andReturn(200); + EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Normal"); + + WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient); + + PowerMock.replayAll(); + + assertThat(jerseyClient.post(url), equalTo("Normal")); + + PowerMock.verifyAll(); + } + + @Test + public void post_nobody_fail() { + Client mockedClient = PowerMock.createMock(Client.class); + WebTarget mockedTarget = PowerMock.createPartialMock(WebTarget.class, "request"); + Invocation.Builder mockedBuilder = PowerMock.createPartialMock(Invocation.Builder.class, "post", Entity.class); + Response mockedResponse = PowerMock.createMock(Response.class); + + EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget); + EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder); + EasyMock.expect(mockedBuilder.post(null)).andReturn(mockedResponse); + EasyMock.expect(mockedResponse.getStatus()).andReturn(404); + EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Error"); + + WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient); + + exception.expect(HttpException.class); + exception.expectMessage(ERROR_MSG); + + PowerMock.replayAll(); + + jerseyClient.post(url); + + PowerMock.verifyAll(); + } + + @Test + public void post_normal_no_body_with_class() { + Client mockedClient = PowerMock.createMock(Client.class); + WebTarget mockedTarget = PowerMock.createPartialMock(WebTarget.class, "request"); + Invocation.Builder mockedBuilder = PowerMock.createPartialMock(Invocation.Builder.class, "post", Entity.class); + Response mockedResponse = PowerMock.createMock(Response.class); + Bean bean = new Bean("name", "holmes"); + + EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget); + EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder); + EasyMock.expect(mockedBuilder.post(null)).andReturn(mockedResponse); + EasyMock.expect(mockedResponse.getStatus()).andReturn(200); + EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn(GsonUtil.beanToJson(bean)); + + WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient); + + PowerMock.replayAll(); + + assertThat(jerseyClient.post(url, null, Bean.class), equalTo(bean)); + + PowerMock.verifyAll(); + } + + + @Test + public void put_normal() { + Client mockedClient = PowerMock.createMock(Client.class); + WebTarget mockedTarget = PowerMock.createPartialMock(WebTarget.class, "request"); + Invocation.Builder mockedBuilder = PowerMock.createPartialMock(Invocation.Builder.class, "put", Entity.class); + Response mockedResponse = PowerMock.createMock(Response.class); + + EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget); + EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder); + EasyMock.expect(mockedBuilder.put(null)).andReturn(mockedResponse); + EasyMock.expect(mockedResponse.getStatus()).andReturn(200); + EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Normal"); + + WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient); + + PowerMock.replayAll(); + + assertThat(jerseyClient.put(url, null), equalTo("Normal")); + + PowerMock.verifyAll(); + } @Test - public void http() { - jerseyClient.httpClient(); + public void put_normal_with_class_info() { + Client mockedClient = PowerMock.createMock(Client.class); + WebTarget mockedTarget = PowerMock.createPartialMock(WebTarget.class, "request"); + Invocation.Builder mockedBuilder = PowerMock.createPartialMock(Invocation.Builder.class, "put", Entity.class); + Response mockedResponse = PowerMock.createMock(Response.class); + Bean bean = new Bean("name", "holmes"); + + EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget); + EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder); + EasyMock.expect(mockedBuilder.put(null)).andReturn(mockedResponse); + EasyMock.expect(mockedResponse.getStatus()).andReturn(200); + EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn(GsonUtil.beanToJson(bean)); + + WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient); + + PowerMock.replayAll(); + + assertThat(jerseyClient.put(url, null, Bean.class), equalTo(bean)); + + PowerMock.verifyAll(); } @Test - public void https() throws Exception { - WhiteboxImpl.invokeMethod(jerseyClient, "init"); - jerseyClient.httpsClient(); + public void put_normal_with_null_class_info() { + Client mockedClient = PowerMock.createMock(Client.class); + WebTarget mockedTarget = PowerMock.createPartialMock(WebTarget.class, "request"); + Invocation.Builder mockedBuilder = PowerMock.createPartialMock(Invocation.Builder.class, "put", Entity.class); + Response mockedResponse = PowerMock.createMock(Response.class); + Bean bean = new Bean("name", "holmes"); + + EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget); + EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder); + EasyMock.expect(mockedBuilder.put(null)).andReturn(mockedResponse); + EasyMock.expect(mockedResponse.getStatus()).andReturn(200); + EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn(GsonUtil.beanToJson(bean)); + + WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient); + + PowerMock.replayAll(); + + assertThat(jerseyClient.put(url, null, null), equalTo(GsonUtil.beanToJson(bean))); + + PowerMock.verifyAll(); } @Test - public void clientHttp() { - jerseyClient.client(false); + public void put_fail() { + Client mockedClient = PowerMock.createMock(Client.class); + WebTarget mockedTarget = PowerMock.createPartialMock(WebTarget.class, "request"); + Invocation.Builder mockedBuilder = PowerMock.createPartialMock(Invocation.Builder.class, "put", Entity.class); + Response mockedResponse = PowerMock.createMock(Response.class); + + EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget); + EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder); + EasyMock.expect(mockedBuilder.put(null)).andReturn(mockedResponse); + EasyMock.expect(mockedResponse.getStatus()).andReturn(404); + EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Error"); + + WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient); + + exception.expect(HttpException.class); + exception.expectMessage(ERROR_MSG); + + PowerMock.replayAll(); + + jerseyClient.put(url, null, null); + + PowerMock.verifyAll(); } + @Test - public void clientHttps() throws Exception { - WhiteboxImpl.invokeMethod(jerseyClient, "init"); - jerseyClient.client(true); + public void delete_normal() { + Client mockedClient = PowerMock.createMock(Client.class); + WebTarget mockedTarget = PowerMock.createPartialMock(WebTarget.class, "request"); + Invocation.Builder mockedBuilder = PowerMock.createPartialMock(Invocation.Builder.class, "delete"); + Response mockedResponse = PowerMock.createMock(Response.class); + + EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget); + EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder); + EasyMock.expect(mockedBuilder.delete()).andReturn(mockedResponse); + EasyMock.expect(mockedResponse.getStatus()).andReturn(200); + EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Normal"); + + WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient); + + PowerMock.replayAll(); + + assertThat(jerseyClient.delete(url), equalTo("Normal")); + + PowerMock.verifyAll(); + } + + @Test + public void delete_normal_with_class_info() { + Client mockedClient = PowerMock.createMock(Client.class); + WebTarget mockedTarget = PowerMock.createPartialMock(WebTarget.class, "request"); + Invocation.Builder mockedBuilder = PowerMock.createPartialMock(Invocation.Builder.class, "get"); + Response mockedResponse = PowerMock.createMock(Response.class); + Bean bean = new Bean("name", "holmes"); + + EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget); + EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder); + EasyMock.expect(mockedBuilder.delete()).andReturn(mockedResponse); + EasyMock.expect(mockedResponse.getStatus()).andReturn(200); + EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn(GsonUtil.beanToJson(bean)); + + WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient); + + PowerMock.replayAll(); + + assertThat(jerseyClient.delete(url, Bean.class), equalTo(bean)); + + PowerMock.verifyAll(); + } + + @Test + public void delete_with_null_class_info() { + Client mockedClient = PowerMock.createMock(Client.class); + WebTarget mockedTarget = PowerMock.createPartialMock(WebTarget.class, "request"); + Invocation.Builder mockedBuilder = PowerMock.createPartialMock(Invocation.Builder.class, "delete"); + Response mockedResponse = PowerMock.createMock(Response.class); + + EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget); + EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder); + EasyMock.expect(mockedBuilder.delete()).andReturn(mockedResponse); + EasyMock.expect(mockedResponse.getStatus()).andReturn(200); + EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Normal"); + + WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient); + + PowerMock.replayAll(); + + assertThat(jerseyClient.delete(url, null), equalTo("Normal")); + + PowerMock.verifyAll(); + } + + @Test + public void delete_fail() { + Client mockedClient = PowerMock.createMock(Client.class); + WebTarget mockedTarget = PowerMock.createPartialMock(WebTarget.class, "request"); + Invocation.Builder mockedBuilder = PowerMock.createPartialMock(Invocation.Builder.class, "delete"); + Response mockedResponse = PowerMock.createMock(Response.class); + + EasyMock.expect(mockedClient.target(url)).andReturn(mockedTarget); + EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder); + EasyMock.expect(mockedBuilder.delete()).andReturn(mockedResponse); + EasyMock.expect(mockedResponse.getStatus()).andReturn(404); + EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Error"); + + WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient); + + exception.expect(HttpException.class); + exception.expectMessage(ERROR_MSG); + + PowerMock.replayAll(); + + jerseyClient.delete(url); + + PowerMock.verifyAll(); + } + + @Test + public void headers() { + Map<String, Object> hds = new HashMap<>(); + jerseyClient.headers(hds); + } + + @Test + public void queryParams() { + Map<String, Object> params = new HashMap<>(); + jerseyClient.queryParams(params); + } +} + +class Bean { + private String name; + private String value; + + public Bean(String name, String value) { + this.name = name; + this.value = value; + } + + public String getName() { + return name; + } + + public void setName(String name) { + this.name = name; + } + + public String getValue() { + return value; + } + + public void setValue(String value) { + this.value = value; + } + + @Override + public int hashCode() { + int hName = name == null ? 0 : name.hashCode(); + int hValue = value == null ? 0 : value.hashCode(); + return hName << 1 + hValue >> 1; + } + + @Override + public boolean equals(Object o) { + if (o == null || !(o instanceof Bean)) { + return false; + } + + Bean b = (Bean) o; + return name.equals(b.name) && value.equals(b.value); } }
\ No newline at end of file diff --git a/holmes-actions/src/test/java/org/onap/holmes/common/utils/MsbRegisterTest.java b/holmes-actions/src/test/java/org/onap/holmes/common/utils/MsbRegisterTest.java index 0637586..1a12f85 100644 --- a/holmes-actions/src/test/java/org/onap/holmes/common/utils/MsbRegisterTest.java +++ b/holmes-actions/src/test/java/org/onap/holmes/common/utils/MsbRegisterTest.java @@ -16,69 +16,92 @@ package org.onap.holmes.common.utils; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.onap.holmes.common.config.MicroServiceConfig; import org.onap.holmes.common.exception.CorrelationException; +import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo; import org.onap.msb.sdk.discovery.entity.MicroServiceInfo; import org.powermock.api.easymock.PowerMock; +import org.powermock.core.classloader.annotations.PowerMockIgnore; import org.powermock.core.classloader.annotations.PrepareForTest; import org.powermock.modules.junit4.PowerMockRunner; +import org.powermock.reflect.internal.WhiteboxImpl; -import javax.ws.rs.client.Client; import javax.ws.rs.client.Entity; -import javax.ws.rs.client.Invocation; -import javax.ws.rs.client.WebTarget; -import javax.ws.rs.core.MediaType; -import javax.ws.rs.core.Response; +import static org.easymock.EasyMock.anyObject; import static org.easymock.EasyMock.expect; -import static org.powermock.api.easymock.PowerMock.createMock; +import static org.powermock.api.easymock.PowerMock.createPartialMock; @PrepareForTest({MicroServiceConfig.class}) @RunWith(PowerMockRunner.class) +@PowerMockIgnore({"javax.net.ssl.*", "javax.security.*"}) public class MsbRegisterTest { - @Test - public void test_register2Msb_normal() { - MicroServiceInfo msi = new MicroServiceInfo(); + + private MsbRegister msbRegister; + private JerseyClient mockedJerseyClient; + private MicroServiceInfo msi; + + @Before + public void before() { + msi = new MicroServiceInfo(); String[] msbAddrInfo = {"127.0.0.1", "80"}; PowerMock.mockStatic(MicroServiceConfig.class); expect(MicroServiceConfig.getMsbIpAndPort()).andReturn(msbAddrInfo); - JerseyClient mockedJerseyClient = createMock(JerseyClient.class); - - Client mockedClient = createMock(Client.class); - expect(mockedJerseyClient.client(false)).andReturn(mockedClient); + mockedJerseyClient = createPartialMock(JerseyClient.class, + "post", new Class[]{String.class, Entity.class, Class.class}); - WebTarget mockedWebTarget = createMock(WebTarget.class); - expect(mockedClient.target("http://127.0.0.1:80/api/microservices/v1/services")).andReturn(mockedWebTarget); - - - expect(mockedWebTarget.queryParam("createOrUpdate", true)).andReturn(mockedWebTarget).times(2); + msbRegister = new MsbRegister(); + WhiteboxImpl.setInternalState(msbRegister, "client", mockedJerseyClient); + } - Invocation.Builder mockedBuilder = createMock(Invocation.Builder.class); - expect(mockedWebTarget.request(MediaType.APPLICATION_JSON)).andReturn(mockedBuilder).times(2); + @Test + public void test_register2Msb_normal() { + expect(mockedJerseyClient.post(anyObject(String.class), + anyObject(Entity.class), + anyObject(Class.class))) + .andReturn(GsonUtil.jsonToBean("{\"serviceName\":\"holmes-engine-mgmt\"," + + "\"version\":\"v1\",\"url\":\"/api/holmes-engine-mgmt/v1\",\"protocol\":\"REST\"," + + "\"visualRange\":\"0|1\",\"lb_policy\":\"\",\"publish_port\":\"\",\"namespace\":\"\"," + + "\"network_plane_type\":\"\",\"host\":\"\",\"path\":\"/api/holmes-engine-mgmt/v1\"," + + "\"enable_ssl\":true,\"nodes\":[{\"ip\":\"127.0.0.1\",\"port\":\"9102\",\"checkType\":\"\"," + + "\"checkUrl\":\"\",\"tls_skip_verify\":true,\"ha_role\":\"\",\"nodeId\":\"_v1_holmes-engine-mgmt_127.0.0.1_9102\"," + + "\"status\":\"passing\"}],\"metadata\":[],\"labels\":[],\"status\":\"1\",\"is_manual\":false}", + MicroServiceFullInfo.class)); - Response mockedResponse = createMock(Response.class); - expect(mockedBuilder.post(Entity.entity(msi, MediaType.APPLICATION_JSON))) - .andReturn(mockedResponse); - expect(mockedResponse.getStatus()).andReturn(300); + PowerMock.replayAll(); - expect(mockedBuilder.post(Entity.entity(msi, MediaType.APPLICATION_JSON))) - .andReturn(mockedResponse); - expect(mockedResponse.getStatus()).andReturn(201); - expect(mockedResponse.readEntity(String.class)).andReturn("Error"); - expect(mockedResponse.readEntity(String.class)).andReturn("{\"serviceName\":\"holmes-engine-mgmt\"," + - "\"version\":\"v1\",\"url\":\"/api/holmes-engine-mgmt/v1\",\"protocol\":\"REST\"," + - "\"visualRange\":\"0|1\",\"lb_policy\":\"\",\"publish_port\":\"\",\"namespace\":\"\"," + - "\"network_plane_type\":\"\",\"host\":\"\",\"path\":\"/api/holmes-engine-mgmt/v1\"," + - "\"enable_ssl\":true,\"nodes\":[{\"ip\":\"127.0.0.1\",\"port\":\"9102\",\"checkType\":\"\"," + - "\"checkUrl\":\"\",\"tls_skip_verify\":true,\"ha_role\":\"\",\"nodeId\":\"_v1_holmes-engine-mgmt_127.0.0.1_9102\"," + - "\"status\":\"passing\"}],\"metadata\":[],\"labels\":[],\"status\":\"1\",\"is_manual\":false}"); + try { + msbRegister.register2Msb(msi); + } catch (CorrelationException e) { + // Do nothing + } + PowerMock.verifyAll(); + } - MsbRegister msbRegister = new MsbRegister(mockedJerseyClient); + @Test + public void test_register2Msb_fail_once() { + expect(mockedJerseyClient.post(anyObject(String.class), + anyObject(Entity.class), + anyObject(Class.class))) + .andReturn(null); + + expect(mockedJerseyClient.post(anyObject(String.class), + anyObject(Entity.class), + anyObject(Class.class))) + .andReturn(GsonUtil.jsonToBean("{\"serviceName\":\"holmes-engine-mgmt\"," + + "\"version\":\"v1\",\"url\":\"/api/holmes-engine-mgmt/v1\",\"protocol\":\"REST\"," + + "\"visualRange\":\"0|1\",\"lb_policy\":\"\",\"publish_port\":\"\",\"namespace\":\"\"," + + "\"network_plane_type\":\"\",\"host\":\"\",\"path\":\"/api/holmes-engine-mgmt/v1\"," + + "\"enable_ssl\":true,\"nodes\":[{\"ip\":\"127.0.0.1\",\"port\":\"9102\",\"checkType\":\"\"," + + "\"checkUrl\":\"\",\"tls_skip_verify\":true,\"ha_role\":\"\",\"nodeId\":\"_v1_holmes-engine-mgmt_127.0.0.1_9102\"," + + "\"status\":\"passing\"}],\"metadata\":[],\"labels\":[],\"status\":\"1\",\"is_manual\":false}", + MicroServiceFullInfo.class)); PowerMock.replayAll(); |