diff options
5 files changed, 79 insertions, 141 deletions
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiJsonParserUtil.java b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiJsonParserUtil.java index 8bc006a..ba66628 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiJsonParserUtil.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiJsonParserUtil.java @@ -20,7 +20,6 @@ package org.onap.holmes.common.aai; -import com.google.gson.Gson; import com.google.gson.JsonArray; import com.google.gson.JsonObject; import com.google.gson.JsonParser; @@ -28,15 +27,9 @@ import lombok.extern.slf4j.Slf4j; import org.jvnet.hk2.annotations.Service; import org.onap.holmes.common.aai.config.AaiConfig; import org.onap.holmes.common.config.MicroServiceConfig; -import org.onap.holmes.common.exception.CorrelationException; -import javax.ws.rs.client.Client; -import javax.ws.rs.client.ClientBuilder; -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.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -60,25 +53,6 @@ public class AaiJsonParserUtil { return ret; } - public static Response get(String host, String path) throws CorrelationException { - Client client = ClientBuilder.newClient(); - WebTarget target = client.target(host).path(path); - try { - Response response = target.request().headers(getAaiHeaders()).get(); - if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { - throw new CorrelationException("Failed to connect to AAI. \nCause: " - + response.getStatusInfo().getReasonPhrase() + "\nDetails: \n" - + getErrorMsg(String.format("%s%s", host, path), null, response)); - } - return response; - } catch (CorrelationException e) { - throw e; - } catch (Exception e) { - throw new CorrelationException(e.getMessage() + "More info: " - + getErrorMsg(String.format("%s%s", host, path), null, null), e); - } - } - public static JsonObject getInfo(String response, String field) { JsonObject jObject = JsonParser.parseString(response).getAsJsonObject(); JsonObject relationshipList = extractJsonObject(jObject, "relationship-list"); @@ -111,28 +85,4 @@ public class AaiJsonParserUtil { public static String getHostAddr() { return MicroServiceConfig.getMsbServerAddrWithHttpPrefix(); } - - public static String getErrorMsg(String url, Map<String, Object> body, Response response) { - Gson gson = new Gson(); - StringBuilder sb = new StringBuilder(); - sb.append("Rerquest URL: ").append(url).append("\n"); - sb.append("Request Header: ").append(gson.toJson(getAaiHeaders())).append("\n"); - if (body != null) { - sb.append("Request Body: ").append(gson.toJson(body)).append("\n"); - } - if (response != null) { - sb.append("Request Body: ").append(response.readEntity(String.class)); - } - return sb.toString(); - } - - public static MultivaluedMap getAaiHeaders() { - MultivaluedMap<String, Object> headers = new MultivaluedHashMap<>(); - 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"); - return headers; - } } diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery4Ccvpn2.java b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery4Ccvpn2.java index cf39910..41eb2c0 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery4Ccvpn2.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery4Ccvpn2.java @@ -27,10 +27,10 @@ import lombok.extern.slf4j.Slf4j; import org.jvnet.hk2.annotations.Service; import org.onap.holmes.common.aai.config.AaiConfig; import org.onap.holmes.common.exception.CorrelationException; +import org.onap.holmes.common.utils.JerseyClient; -import javax.ws.rs.core.MultivaluedHashMap; -import javax.ws.rs.core.MultivaluedMap; -import javax.ws.rs.core.Response; +import java.util.HashMap; +import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -40,25 +40,27 @@ import static org.onap.holmes.common.aai.AaiJsonParserUtil.*; @Slf4j public class AaiQuery4Ccvpn2 { - private MultivaluedMap<String, Object> headers; + private Map<String, Object> headers; static public AaiQuery4Ccvpn2 newInstance() { return new AaiQuery4Ccvpn2(); } private AaiQuery4Ccvpn2() { - headers = new MultivaluedHashMap<>(); - 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 = 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"); + headers.put("Content-Type", "application/json"); } - private String getSiteVNFId(String siteService) throws CorrelationException { - Response response = get(getHostAddr(), AaiConfig.MsbConsts.AAI_SITE_RESOURCES_QUERY); - String resStr = response.readEntity(String.class); - JsonObject resObj = JsonParser.parseString(resStr).getAsJsonObject(); + private String getSiteVNFId(String siteService) { + String response = JerseyClient.newInstance() + .headers(headers) + .path(AaiConfig.MsbConsts.AAI_SITE_RESOURCES_QUERY) + .get(getHostAddr()); + JsonObject resObj = JsonParser.parseString(response).getAsJsonObject(); JsonArray siteResources = extractJsonArray(resObj, "site-resource"); if (siteResources != null) { for (int i = 0; i < siteResources.size(); i++) { @@ -81,27 +83,31 @@ public class AaiQuery4Ccvpn2 { return null; } - private JsonObject getServiceInstanceByVnfId(String vnfId) throws CorrelationException { - Response response = get(getHostAddr(), getPath(AaiConfig.MsbConsts.AAI_SITE_VNF_QUERY, - "vnfId", vnfId)); - String resStr = response.readEntity(String.class); + private JsonObject getServiceInstanceByVnfId(String vnfId) { + String resStr = JerseyClient.newInstance() + .headers(headers) + .path(getPath(AaiConfig.MsbConsts.AAI_SITE_VNF_QUERY, + "vnfId", vnfId)) + .get(getHostAddr()); return getInfo(resStr, "service-instance"); } - public JsonObject getSiteServiceInstance(String siteService) throws CorrelationException { + public JsonObject getSiteServiceInstance(String siteService) { String vnfId = getSiteVNFId(siteService); if (vnfId == null) { return null; } JsonObject serviceInstanceInfo = getServiceInstanceByVnfId(vnfId); String serviceInstancePath = serviceInstanceInfo.get("related-link").getAsString(); - Response response = get(getHostAddr(), getPath(serviceInstancePath)); - String res = response.readEntity(String.class); + String res = JerseyClient.newInstance() + .headers(headers) + .path(getPath(serviceInstancePath)) + .get(getHostAddr()); JsonObject instance = JsonParser.parseString(res).getAsJsonObject(); String[] params = new String[2]; Pattern pattern = Pattern.compile("/aai/v\\d+/business/customers/customer/(.+)" + - "/service-subscriptions/service-subscription/(.+)" + - "/service-instances/service-instance/(.+)"); + "/service-subscriptions/service-subscription/(.+)" + + "/service-instances/service-instance/(.+)"); Matcher matcher = pattern.matcher(serviceInstancePath); if (matcher.find()) { params[0] = matcher.group(1); diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/utils/JerseyClient.java b/holmes-actions/src/main/java/org/onap/holmes/common/utils/JerseyClient.java index cd87ebe..871ffad 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/utils/JerseyClient.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/utils/JerseyClient.java @@ -122,7 +122,7 @@ public class JerseyClient { Response response = builder.get(); - if (isSuccessful(response)) { + if (isSuccessful(response, url)) { return response2Target(response, clazz); } @@ -147,7 +147,7 @@ public class JerseyClient { Response response = builder.post(entity); - if (isSuccessful(response)) { + if (isSuccessful(response, url)) { return response2Target(response, clazz); } @@ -167,7 +167,7 @@ public class JerseyClient { Response response = builder.put(entity); - if (isSuccessful(response)) { + if (isSuccessful(response, url)) { return response2Target(response, clazz); } @@ -187,17 +187,22 @@ public class JerseyClient { Response response = builder.delete(); - if (isSuccessful(response)) { + if (isSuccessful(response, url)) { return response2Target(response, clazz); } return null; } - private boolean isSuccessful(Response response) { - if (response.getStatusInfo().getFamily() != Response.Status.Family.SUCCESSFUL) { - throw new HttpException(response.getStatus(), String.format("Failed to get response from the server. Info: %s", - response.readEntity(String.class))); + private boolean isSuccessful(Response response, String url) { + Response.StatusType statusInfo = response.getStatusInfo(); + if (statusInfo.getFamily() != Response.Status.Family.SUCCESSFUL) { + throw new HttpException(statusInfo.getStatusCode(), + String.format("Failed to get response from the server. " + + "\nURL: %s\nCause: %s\nResponse body: %s", + url, + statusInfo.getReasonPhrase(), + response.readEntity(String.class))); } return true; } 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"; |