diff options
author | GuangrongFu <fu.guangrong@zte.com.cn> | 2021-06-08 10:17:05 +0800 |
---|---|---|
committer | GuangrongFu <fu.guangrong@zte.com.cn> | 2021-06-08 10:17:05 +0800 |
commit | 4242b93bdf5079fee5339a50d8126e4ea56ad9b6 (patch) | |
tree | ba6b5e16e773c057b3371d595494163540910422 | |
parent | 7218f0b8e91bc61b6f5a9db44f706da1bf669f4b (diff) |
Refactored JerseyClient
Change-Id: I0a6c83b50380affdc723c07df0b1ca41947a3d8e
Issue-ID: HOLMES-442
Signed-off-by: GuangrongFu <fu.guangrong@zte.com.cn>
11 files changed, 40 insertions, 30 deletions
diff --git a/holmes-actions/pom.xml b/holmes-actions/pom.xml index 3a5b042..ea935c4 100644 --- a/holmes-actions/pom.xml +++ b/holmes-actions/pom.xml @@ -12,7 +12,7 @@ <parent>
<groupId>org.onap.holmes.common</groupId>
<artifactId>holmes-common-parent</artifactId>
- <version>1.3.4-SNAPSHOT</version>
+ <version>1.3.5-SNAPSHOT</version>
</parent>
<name>holmes-common-service</name>
diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java index 4a4ffcb..a73eb22 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQuery.java @@ -1,5 +1,5 @@ /** - * Copyright 2017 ZTE Corporation. + * Copyright 2017 - 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 @@ -92,7 +92,7 @@ public class AaiQuery { private String getResponse(String url) throws CorrelationException { try { - return new JerseyClient().headers(getHeaders()).get(url); + return JerseyClient.newInstance().headers(getHeaders()).get(url); } catch (Exception e) { throw new CorrelationException("Failed to get data from aai", e); } diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQueryMdons.java b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQueryMdons.java index 892638b..c557003 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQueryMdons.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/aai/AaiQueryMdons.java @@ -66,7 +66,7 @@ public class AaiQueryMdons { private String getResponse(String url) throws CorrelationException { try { - return new JerseyClient().headers(getHeaders()).get(url); + return JerseyClient.newInstance().headers(getHeaders()).get(url); } catch (Exception e) { throw new CorrelationException("Failed to get data from aai", e); } @@ -174,7 +174,7 @@ public class AaiQueryMdons { private void put(String url, String content) throws CorrelationException { try { - new JerseyClient().headers(getHeaders()).put(url, Entity.json(content)); + JerseyClient.newInstance().headers(getHeaders()).put(url, Entity.json(content)); } catch (Exception e) { throw new CorrelationException("Failed to put data in AAI", e); } diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/Publisher.java b/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/Publisher.java index a4024ea..23a04d6 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/Publisher.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/dmaap/Publisher.java @@ -29,7 +29,7 @@ import java.util.concurrent.TimeUnit; @Service
public class Publisher {
private String url;
- private JerseyClient client = new JerseyClient(TimeUnit.SECONDS.toMillis(30));
+ private JerseyClient client = JerseyClient.newInstance(TimeUnit.SECONDS.toMillis(30));
public void publish(PolicyMsg msg) {
client.post(url, Entity.json(msg));
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 09d9f64..cd87ebe 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 @@ -16,7 +16,6 @@ package org.onap.holmes.common.utils; -import org.eclipse.jetty.http.HttpStatus; import org.onap.holmes.common.exception.HttpException; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -63,12 +62,19 @@ public class JerseyClient { private Map<String, Object> parameters = new HashMap(); private List<String> paths = new ArrayList(); + public static JerseyClient newInstance() { + return new JerseyClient(); + } + + public static JerseyClient newInstance(long timeout) { + return new JerseyClient(timeout); + } - public JerseyClient() { + private JerseyClient() { this(DEFAULT_TIMEOUT); } - public JerseyClient(long timeout) { + private JerseyClient(long timeout) { this.client = ClientBuilder.newBuilder() .connectTimeout(timeout, TimeUnit.MILLISECONDS) .readTimeout(timeout, TimeUnit.MILLISECONDS) @@ -189,9 +195,8 @@ public class JerseyClient { } private boolean isSuccessful(Response response) { - int status = response.getStatus(); - if (!HttpStatus.isSuccess(status)) { - throw new HttpException(status, String.format("Failed to get response from the server. Info: %s", + 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))); } return true; diff --git a/holmes-actions/src/main/java/org/onap/holmes/common/utils/MsbRegister.java b/holmes-actions/src/main/java/org/onap/holmes/common/utils/MsbRegister.java index ff5b48a..79ccbdc 100644 --- a/holmes-actions/src/main/java/org/onap/holmes/common/utils/MsbRegister.java +++ b/holmes-actions/src/main/java/org/onap/holmes/common/utils/MsbRegister.java @@ -36,7 +36,7 @@ import static org.onap.holmes.common.utils.JerseyClient.PROTOCOL_HTTPS; public class MsbRegister { private static final Logger log = LoggerFactory.getLogger(MsbRegister.class); - private JerseyClient client = new JerseyClient(); + private JerseyClient client = JerseyClient.newInstance(); public MsbRegister() { } 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 21dd44a..09dce8e 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 @@ -1,5 +1,5 @@ /** - * Copyright 2020 Fujitsu Limited. + * Copyright 2020 - 2021 Fujitsu, ZTE Limited. * <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 @@ -42,7 +42,7 @@ import static org.onap.holmes.common.config.MicroServiceConfig.MSB_ADDR; @RunWith(PowerMockRunner.class) @PowerMockIgnore("javax.net.ssl.*") -@PrepareForTest({AaiQueryMdons.class, MicroServiceConfig.class}) +@PrepareForTest({AaiQueryMdons.class, MicroServiceConfig.class, JerseyClient.class}) public class AaiQueryMdonsTest { @Rule 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 0eec082..a9f3cea 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 @@ -43,7 +43,7 @@ import static org.hamcrest.core.IsNull.nullValue; import static org.junit.Assert.assertThat; -@PrepareForTest({AaiQuery.class, MicroServiceConfig.class}) +@PrepareForTest({AaiQuery.class, MicroServiceConfig.class, JerseyClient.class}) @PowerMockIgnore("javax.net.ssl.*") @RunWith(PowerMockRunner.class) public class AaiQueryTest { 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 0183740..df1e2a2 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 @@ -44,8 +44,9 @@ import static org.junit.Assert.assertThat; 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 JerseyClient jerseyClient = JerseyClient.newInstance(); private String url = "http://www.onap.org/holmes/test"; + @Rule private ExpectedException exception = ExpectedException.none(); @@ -62,7 +63,7 @@ public class JerseyClientTest { 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.getStatusInfo()).andReturn(Response.Status.OK); EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Normal"); WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient); @@ -88,7 +89,7 @@ public class JerseyClientTest { 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.getStatusInfo()).andReturn(Response.Status.OK); EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn(GsonUtil.beanToJson(bean)); WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient); @@ -110,7 +111,7 @@ public class JerseyClientTest { 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.getStatusInfo()).andReturn(Response.Status.OK); EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Normal"); WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient); @@ -133,6 +134,7 @@ public class JerseyClientTest { EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder); EasyMock.expect(mockedBuilder.get()).andReturn(mockedResponse); EasyMock.expect(mockedResponse.getStatus()).andReturn(404); + EasyMock.expect(mockedResponse.getStatusInfo()).andReturn(Response.Status.NOT_FOUND); EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Error"); WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient); @@ -157,7 +159,7 @@ public class JerseyClientTest { 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.getStatusInfo()).andReturn(Response.Status.OK); EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Normal"); WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient); @@ -180,6 +182,7 @@ public class JerseyClientTest { EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder); EasyMock.expect(mockedBuilder.post(null)).andReturn(mockedResponse); EasyMock.expect(mockedResponse.getStatus()).andReturn(404); + EasyMock.expect(mockedResponse.getStatusInfo()).andReturn(Response.Status.NOT_FOUND); EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Error"); WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient); @@ -205,7 +208,7 @@ public class JerseyClientTest { 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.getStatusInfo()).andReturn(Response.Status.OK); EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn(GsonUtil.beanToJson(bean)); WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient); @@ -228,7 +231,7 @@ public class JerseyClientTest { 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.getStatusInfo()).andReturn(Response.Status.OK); EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Normal"); WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient); @@ -251,7 +254,7 @@ public class JerseyClientTest { 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.getStatusInfo()).andReturn(Response.Status.OK); EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn(GsonUtil.beanToJson(bean)); WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient); @@ -274,7 +277,7 @@ public class JerseyClientTest { 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.getStatusInfo()).andReturn(Response.Status.OK); EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn(GsonUtil.beanToJson(bean)); WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient); @@ -297,6 +300,7 @@ public class JerseyClientTest { EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder); EasyMock.expect(mockedBuilder.put(null)).andReturn(mockedResponse); EasyMock.expect(mockedResponse.getStatus()).andReturn(404); + EasyMock.expect(mockedResponse.getStatusInfo()).andReturn(Response.Status.NOT_FOUND); EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Error"); WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient); @@ -322,7 +326,7 @@ public class JerseyClientTest { 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.getStatusInfo()).andReturn(Response.Status.OK); EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Normal"); WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient); @@ -345,7 +349,7 @@ public class JerseyClientTest { 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.getStatusInfo()).andReturn(Response.Status.OK); EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn(GsonUtil.beanToJson(bean)); WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient); @@ -367,7 +371,7 @@ public class JerseyClientTest { 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.getStatusInfo()).andReturn(Response.Status.OK); EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Normal"); WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient); @@ -390,6 +394,7 @@ public class JerseyClientTest { EasyMock.expect(mockedTarget.request()).andReturn(mockedBuilder); EasyMock.expect(mockedBuilder.delete()).andReturn(mockedResponse); EasyMock.expect(mockedResponse.getStatus()).andReturn(404); + EasyMock.expect(mockedResponse.getStatusInfo()).andReturn(Response.Status.NOT_FOUND); EasyMock.expect(mockedResponse.readEntity(String.class)).andReturn("Error"); WhiteboxImpl.setInternalState(jerseyClient, "client", mockedClient); 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 1a12f85..19b3251 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 @@ -35,7 +35,7 @@ import static org.easymock.EasyMock.anyObject; import static org.easymock.EasyMock.expect; import static org.powermock.api.easymock.PowerMock.createPartialMock; -@PrepareForTest({MicroServiceConfig.class}) +@PrepareForTest({MicroServiceConfig.class, JerseyClient.class}) @RunWith(PowerMockRunner.class) @PowerMockIgnore({"javax.net.ssl.*", "javax.security.*"}) public class MsbRegisterTest { @@ -20,7 +20,7 @@ <artifactId>holmes-common-parent</artifactId>
<packaging>pom</packaging>
- <version>1.3.4-SNAPSHOT</version>
+ <version>1.3.5-SNAPSHOT</version>
<name>holmes-common</name>
<modules>
<module>holmes-actions</module>
|