aboutsummaryrefslogtreecommitdiffstats
path: root/applications/common
diff options
context:
space:
mode:
authorJim Hahn <jrh3@att.com>2021-07-19 11:59:46 -0400
committerJim Hahn <jrh3@att.com>2021-07-19 13:58:51 -0400
commit1c35a18a77bd4d55e419a8b6c275bce0a1037e05 (patch)
tree4e318624467a5b7b273cc906f4067db57b5c05eb /applications/common
parente3d0ebb1599c36f23ec82ca2f428af6db8fa4373 (diff)
Use new RestClientParameters class in xacml-pdp
Replaced generic BusTopicParams with more specific RestClientParameters. Also modified code to pass the HttpClient around instead of passing the client parameters around. Issue-ID: POLICY-3456 Change-Id: Ic07b710645eeab696e6df0a015171578ad08ca83 Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'applications/common')
-rw-r--r--applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/PolicyApiCaller.java21
-rw-r--r--applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/XacmlApplicationServiceProvider.java6
-rw-r--r--applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslator.java6
-rw-r--r--applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdXacmlApplicationServiceProvider.java8
-rw-r--r--applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/PolicyApiCallerTest.java33
-rw-r--r--applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslatorTest.java13
-rw-r--r--applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdXacmlApplicationServiceProviderTest.java16
7 files changed, 47 insertions, 56 deletions
diff --git a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/PolicyApiCaller.java b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/PolicyApiCaller.java
index 52ecd84b..0f0f7ea2 100644
--- a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/PolicyApiCaller.java
+++ b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/PolicyApiCaller.java
@@ -23,10 +23,7 @@ package org.onap.policy.pdp.xacml.application.common;
import java.net.HttpURLConnection;
import javax.ws.rs.core.Response;
-import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
import org.onap.policy.common.endpoints.http.client.HttpClient;
-import org.onap.policy.common.endpoints.http.client.HttpClientConfigException;
-import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
import org.slf4j.Logger;
@@ -46,16 +43,10 @@ public class PolicyApiCaller {
/**
* Constructs the object.
*
- * @param params target specification
- * @throws PolicyApiException if an error occurs
+ * @param httpClient API REST client
*/
- public PolicyApiCaller(BusTopicParams params) throws PolicyApiException {
- try {
- params.setClientName("policy-api");
- httpClient = makeClient(params);
- } catch (HttpClientConfigException e) {
- throw new PolicyApiException("connection to host: " + params.getHostname(), e);
- }
+ public PolicyApiCaller(HttpClient httpClient) {
+ this.httpClient = httpClient;
}
/**
@@ -87,10 +78,4 @@ public class PolicyApiCaller {
throw new PolicyApiException(type.toString(), e);
}
}
-
- // these methods may be overridden by junit tests
-
- protected HttpClient makeClient(BusTopicParams busParams) throws HttpClientConfigException {
- return HttpClientFactoryInstance.getClientFactory().build(busParams);
- }
}
diff --git a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/XacmlApplicationServiceProvider.java b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/XacmlApplicationServiceProvider.java
index 7512fb76..8d1bf617 100644
--- a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/XacmlApplicationServiceProvider.java
+++ b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/XacmlApplicationServiceProvider.java
@@ -27,7 +27,7 @@ import java.nio.file.Path;
import java.util.List;
import java.util.Map;
import org.apache.commons.lang3.tuple.Pair;
-import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
+import org.onap.policy.common.endpoints.http.client.HttpClient;
import org.onap.policy.models.decisions.concepts.DecisionRequest;
import org.onap.policy.models.decisions.concepts.DecisionResponse;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
@@ -64,9 +64,9 @@ public interface XacmlApplicationServiceProvider {
* Also gives api rest parameters if needed.
*
* @param pathForData Local Path
- * @param policyApiParameters API rest parameters
+ * @param policyApiClient API rest client
*/
- void initialize(Path pathForData, BusTopicParams policyApiParameters)
+ void initialize(Path pathForData, HttpClient policyApiClient)
throws XacmlApplicationException;
/**
diff --git a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslator.java b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslator.java
index cca2d218..e473de88 100644
--- a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslator.java
+++ b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslator.java
@@ -52,7 +52,7 @@ import oasis.names.tc.xacml._3_0.core.schema.wd_17.PolicyType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.RuleType;
import oasis.names.tc.xacml._3_0.core.schema.wd_17.TargetType;
import org.apache.commons.lang3.tuple.Pair;
-import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
+import org.onap.policy.common.endpoints.http.client.HttpClient;
import org.onap.policy.common.utils.coder.CoderException;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.common.utils.coder.StandardYamlCoder;
@@ -94,7 +94,7 @@ public class StdMatchableTranslator extends StdBaseTranslator implements Matcha
private final Map<ToscaConceptIdentifier, MatchablePolicyType> matchableCache = new HashMap<>();
@Setter
- private BusTopicParams apiRestParameters;
+ private HttpClient apiClient;
@Setter
private Path pathForData;
@@ -572,7 +572,7 @@ public class StdMatchableTranslator extends StdBaseTranslator implements Matcha
//
ToscaServiceTemplate policyTemplate = null;
try {
- var api = new PolicyApiCaller(this.apiRestParameters);
+ var api = new PolicyApiCaller(this.apiClient);
policyTemplate = api.getPolicyType(policyTypeId);
} catch (PolicyApiException e) {
diff --git a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdXacmlApplicationServiceProvider.java b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdXacmlApplicationServiceProvider.java
index 40968758..466aae12 100644
--- a/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdXacmlApplicationServiceProvider.java
+++ b/applications/common/src/main/java/org/onap/policy/pdp/xacml/application/common/std/StdXacmlApplicationServiceProvider.java
@@ -43,7 +43,7 @@ import lombok.AccessLevel;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.apache.commons.lang3.tuple.Pair;
-import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
+import org.onap.policy.common.endpoints.http.client.HttpClient;
import org.onap.policy.models.decisions.concepts.DecisionRequest;
import org.onap.policy.models.decisions.concepts.DecisionResponse;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
@@ -67,7 +67,7 @@ public abstract class StdXacmlApplicationServiceProvider implements XacmlApplica
private Path pathForData = null;
@Getter
- private BusTopicParams policyApiParameters;
+ private HttpClient policyApiClient;
private Properties pdpProperties = null;
private PDPEngine pdpEngine = null;
private Map<ToscaPolicy, Path> mapLoadedPolicies = new HashMap<>();
@@ -83,7 +83,7 @@ public abstract class StdXacmlApplicationServiceProvider implements XacmlApplica
}
@Override
- public void initialize(Path pathForData, BusTopicParams policyApiParameters)
+ public void initialize(Path pathForData, HttpClient policyApiClient)
throws XacmlApplicationException {
//
// Save our path
@@ -93,7 +93,7 @@ public abstract class StdXacmlApplicationServiceProvider implements XacmlApplica
//
// Save our params
//
- this.policyApiParameters = policyApiParameters;
+ this.policyApiClient = policyApiClient;
//
// Look for and load the properties object
//
diff --git a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/PolicyApiCallerTest.java b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/PolicyApiCallerTest.java
index 23ebbaa9..8a856698 100644
--- a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/PolicyApiCallerTest.java
+++ b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/PolicyApiCallerTest.java
@@ -42,11 +42,11 @@ import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
-import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
import org.onap.policy.common.endpoints.http.client.HttpClient;
-import org.onap.policy.common.endpoints.http.client.HttpClientConfigException;
+import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
import org.onap.policy.common.endpoints.http.server.HttpServletServer;
import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
+import org.onap.policy.common.endpoints.parameters.RestClientParameters;
import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
import org.onap.policy.common.gson.GsonMessageBodyHandler;
import org.onap.policy.common.utils.network.NetworkUtil;
@@ -68,7 +68,9 @@ public class PolicyApiCallerTest {
private static final String UNKNOWN_TYPE = "unknown";
private static int port;
- private static BusTopicParams clientParams;
+ private static RestClientParameters clientParams;
+
+ private static HttpClient apiClient;
private PolicyApiCaller api;
@@ -82,7 +84,8 @@ public class PolicyApiCallerTest {
public static void setUpBeforeClass() throws Exception {
port = NetworkUtil.allocPort();
- clientParams = mock(BusTopicParams.class);
+ clientParams = mock(RestClientParameters.class);
+ when(clientParams.getClientName()).thenReturn("apiClient");
when(clientParams.getHostname()).thenReturn("localhost");
when(clientParams.getPort()).thenReturn(port);
@@ -104,6 +107,7 @@ public class PolicyApiCallerTest {
GsonMessageBodyHandler.class.getName());
HttpServletServerFactoryInstance.getServerFactory().build(props).forEach(HttpServletServer::start);
+ apiClient = HttpClientFactoryInstance.getClientFactory().build(clientParams);
assertTrue(NetworkUtil.isTcpPortOpen(clientParams.getHostname(), clientParams.getPort(), 100, 100));
}
@@ -122,17 +126,7 @@ public class PolicyApiCallerTest {
public void setUp() throws PolicyApiException {
when(clientParams.getPort()).thenReturn(port);
- api = new PolicyApiCaller(clientParams);
- }
-
- @Test
- public void testPolicyApi() {
- assertThatThrownBy(() -> new PolicyApiCaller(clientParams) {
- @Override
- protected HttpClient makeClient(BusTopicParams busParams) throws HttpClientConfigException {
- throw new HttpClientConfigException("expected exception");
- }
- }).isInstanceOf(PolicyApiException.class);
+ api = new PolicyApiCaller(apiClient);
}
@Test
@@ -150,8 +144,13 @@ public class PolicyApiCallerTest {
.isInstanceOf(PolicyApiException.class);
// connect to a port that has no server
- when(clientParams.getPort()).thenReturn(NetworkUtil.allocPort());
- api = new PolicyApiCaller(clientParams);
+ RestClientParameters params2 = mock(RestClientParameters.class);
+ when(params2.getClientName()).thenReturn("apiClient");
+ when(params2.getHostname()).thenReturn("localhost");
+ when(params2.getPort()).thenReturn(NetworkUtil.allocPort());
+
+ HttpClient apiClient2 = HttpClientFactoryInstance.getClientFactory().build(params2);
+ api = new PolicyApiCaller(apiClient2);
assertThatThrownBy(() -> api.getPolicyType(new ToscaConceptIdentifier(MY_TYPE, MY_VERSION)))
.isInstanceOf(PolicyApiException.class);
diff --git a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslatorTest.java b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslatorTest.java
index 5cd54e8f..1d2754b8 100644
--- a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslatorTest.java
+++ b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdMatchableTranslatorTest.java
@@ -58,9 +58,11 @@ import org.junit.BeforeClass;
import org.junit.ClassRule;
import org.junit.Test;
import org.junit.rules.TemporaryFolder;
-import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
+import org.onap.policy.common.endpoints.http.client.HttpClient;
+import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
import org.onap.policy.common.endpoints.http.server.HttpServletServer;
import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
+import org.onap.policy.common.endpoints.parameters.RestClientParameters;
import org.onap.policy.common.endpoints.properties.PolicyEndPointProperties;
import org.onap.policy.common.gson.GsonMessageBodyHandler;
import org.onap.policy.common.utils.coder.CoderException;
@@ -85,8 +87,9 @@ public class StdMatchableTranslatorTest {
private static final String CLIENT_NAME = "policy-api";
private static final StandardYamlCoder yamlCoder = new StandardYamlCoder();
private static int port;
- private static BusTopicParams clientParams;
+ private static RestClientParameters clientParams;
private static ToscaServiceTemplate testTemplate;
+ private static HttpClient apiClient;
@ClassRule
public static final TemporaryFolder policyFolder = new TemporaryFolder();
@@ -106,7 +109,8 @@ public class StdMatchableTranslatorTest {
//
port = NetworkUtil.allocPort();
- clientParams = mock(BusTopicParams.class);
+ clientParams = mock(RestClientParameters.class);
+ when(clientParams.getClientName()).thenReturn("apiClient");
when(clientParams.getHostname()).thenReturn("localhost");
when(clientParams.getPort()).thenReturn(port);
@@ -128,6 +132,7 @@ public class StdMatchableTranslatorTest {
GsonMessageBodyHandler.class.getName());
HttpServletServerFactoryInstance.getServerFactory().build(props).forEach(HttpServletServer::start);
+ apiClient = HttpClientFactoryInstance.getClientFactory().build(clientParams);
assertTrue(NetworkUtil.isTcpPortOpen(clientParams.getHostname(), clientParams.getPort(), 100, 100));
//
@@ -170,7 +175,7 @@ public class StdMatchableTranslatorTest {
// Set it up
//
translator.setPathForData(policyFolder.getRoot().toPath());
- translator.setApiRestParameters(clientParams);
+ translator.setApiClient(apiClient);
//
// Load policies to test
//
diff --git a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdXacmlApplicationServiceProviderTest.java b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdXacmlApplicationServiceProviderTest.java
index 76f22551..ea1e04d1 100644
--- a/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdXacmlApplicationServiceProviderTest.java
+++ b/applications/common/src/test/java/org/onap/policy/pdp/xacml/application/common/std/StdXacmlApplicationServiceProviderTest.java
@@ -56,7 +56,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
-import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
+import org.onap.policy.common.endpoints.http.client.HttpClient;
import org.onap.policy.models.decisions.concepts.DecisionRequest;
import org.onap.policy.models.decisions.concepts.DecisionResponse;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
@@ -80,7 +80,9 @@ public class StdXacmlApplicationServiceProviderTest {
private static final String POLICY_NAME = "my-name";
private static final String POLICY_VERSION = "1.2.3";
private static final String POLICY_TYPE = "my-type";
- private static final BusTopicParams apiRestParameters = new BusTopicParams();
+
+ @Mock
+ private HttpClient apiClient;
@Mock
private ToscaPolicyTranslator trans;
@@ -165,7 +167,7 @@ public class StdXacmlApplicationServiceProviderTest {
@Test
public void testInitialize_testGetXxx() throws XacmlApplicationException {
- prov.initialize(TEMP_PATH, apiRestParameters);
+ prov.initialize(TEMP_PATH, apiClient);
assertEquals(TEMP_PATH, prov.getDataPath());
assertNotNull(prov.getEngine());
@@ -176,7 +178,7 @@ public class StdXacmlApplicationServiceProviderTest {
@Test
public void testInitialize_Ex() throws XacmlApplicationException {
- assertThatThrownBy(() -> prov.initialize(new File(TEMP_DIR_NAME + "-nonExistent").toPath(), apiRestParameters))
+ assertThatThrownBy(() -> prov.initialize(new File(TEMP_DIR_NAME + "-nonExistent").toPath(), apiClient))
.isInstanceOf(XacmlApplicationException.class).hasMessage("Failed to load xacml.properties");
}
@@ -199,7 +201,7 @@ public class StdXacmlApplicationServiceProviderTest {
@Test
public void testLoadPolicy_testUnloadPolicy() throws Exception {
- prov.initialize(TEMP_PATH, apiRestParameters);
+ prov.initialize(TEMP_PATH, apiClient);
PROP_FILE.delete();
final Set<String> set = XACMLProperties.getRootPolicyIDs(prov.getProperties());
@@ -247,7 +249,7 @@ public class StdXacmlApplicationServiceProviderTest {
@Test
public void testUnloadPolicy_NotDeployed() throws Exception {
- prov.initialize(TEMP_PATH, apiRestParameters);
+ prov.initialize(TEMP_PATH, apiClient);
assertFalse(prov.unloadPolicy(policy));
@@ -313,7 +315,7 @@ public class StdXacmlApplicationServiceProviderTest {
engineFactory = null;
prov = new MyProv();
- prov.initialize(TEMP_PATH, apiRestParameters);
+ prov.initialize(TEMP_PATH, apiClient);
assertNotNull(prov.getEngine());
}