diff options
3 files changed, 44 insertions, 58 deletions
diff --git a/main/src/main/java/org/onap/policy/distribution/main/rest/DistributionRestServer.java b/main/src/main/java/org/onap/policy/distribution/main/rest/DistributionRestServer.java index 0a03bcbb..895827fc 100644 --- a/main/src/main/java/org/onap/policy/distribution/main/rest/DistributionRestServer.java +++ b/main/src/main/java/org/onap/policy/distribution/main/rest/DistributionRestServer.java @@ -28,6 +28,7 @@ import java.util.Properties; import org.onap.policy.common.capabilities.Startable; import org.onap.policy.common.endpoints.http.server.HttpServletServer; +import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance; import org.onap.policy.common.gson.JacksonHandler; import org.onap.policy.distribution.main.parameters.RestServerParameters; import org.slf4j.Logger; @@ -63,7 +64,7 @@ public class DistributionRestServer implements Startable { @Override public boolean start() { try { - servers = HttpServletServer.factory.build(getServerProperties()); + servers = HttpServletServerFactoryInstance.getServerFactory().build(getServerProperties()); for (final HttpServletServer server : servers) { server.start(); } diff --git a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/XacmlPdpPolicyForwarder.java b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/XacmlPdpPolicyForwarder.java index 6db56653..196818b5 100644 --- a/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/XacmlPdpPolicyForwarder.java +++ b/plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/XacmlPdpPolicyForwarder.java @@ -2,6 +2,7 @@ * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. * Copyright (C) 2019 Intel Corp. All rights reserved. + * Modifications Copyright (C) 2019 AT&T Intellectual Property. 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. @@ -21,8 +22,6 @@ package org.onap.policy.distribution.forwarding.xacml.pdp; -import java.security.KeyManagementException; -import java.security.NoSuchAlgorithmException; import java.util.Collection; import java.util.Collections; import javax.ws.rs.client.Entity; @@ -32,12 +31,14 @@ import org.onap.policy.api.PolicyParameters; import org.onap.policy.api.PushPolicyParameters; 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.HttpClientFactory; +import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance; import org.onap.policy.common.parameters.ParameterService; import org.onap.policy.distribution.forwarding.PolicyForwarder; import org.onap.policy.distribution.forwarding.xacml.pdp.adapters.XacmlPdpOptimizationPolicyAdapter; import org.onap.policy.distribution.model.OptimizationPolicy; import org.onap.policy.distribution.model.Policy; - import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.http.HttpStatus; @@ -109,7 +110,7 @@ public class XacmlPdpPolicyForwarder implements PolicyForwarder { method, policyName, response.getStatus(), response.getStatusInfo()); return false; } - } catch (KeyManagementException | NoSuchAlgorithmException | ClassNotFoundException exception) { + } catch (HttpClientConfigException exception) { LOGGER.error("Invocation of method " + method + " failed for policy " + policyName + " due to error opening Http client", exception); return false; @@ -117,8 +118,7 @@ public class XacmlPdpPolicyForwarder implements PolicyForwarder { return true; } - private HttpClient getHttpClient() - throws KeyManagementException, NoSuchAlgorithmException, ClassNotFoundException { + private HttpClient getHttpClient() throws HttpClientConfigException { boolean useHttps = configurationParameters.isUseHttps(); String hostname = configurationParameters.getHostname(); int port = configurationParameters.getPort(); @@ -127,7 +127,7 @@ public class XacmlPdpPolicyForwarder implements PolicyForwarder { boolean managed = configurationParameters.isManaged(); BusTopicParams params = BusTopicParams.builder().clientName("SDC Dist").useHttps(useHttps).hostname(hostname) .port(port).userName(userName).password(password).basePath(BASE_PATH).managed(managed).build(); - return HttpClient.factory.build(params); + return getHttpClientFactory().build(params); } @Override @@ -135,4 +135,9 @@ public class XacmlPdpPolicyForwarder implements PolicyForwarder { configurationParameters = ParameterService.get(parameterGroupName); } + // these may be overridden by junit tests + + protected HttpClientFactory getHttpClientFactory() { + return HttpClientFactoryInstance.getClientFactory(); + } } diff --git a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/engine/XacmlPdpPolicyForwarderTest.java b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/engine/XacmlPdpPolicyForwarderTest.java index 6982b33b..43fac718 100644 --- a/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/engine/XacmlPdpPolicyForwarderTest.java +++ b/plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/engine/XacmlPdpPolicyForwarderTest.java @@ -1,19 +1,20 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2019 AT&T Intellectual Property. 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. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * 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. - * + * * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ @@ -28,10 +29,6 @@ import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; -import java.lang.reflect.Field; -import java.lang.reflect.Modifier; -import java.security.KeyManagementException; -import java.security.NoSuchAlgorithmException; import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; @@ -45,12 +42,12 @@ import org.onap.policy.api.PolicyParameters; import org.onap.policy.api.PushPolicyParameters; 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.HttpClientFactory; import org.onap.policy.common.parameters.ParameterGroup; import org.onap.policy.common.parameters.ParameterService; import org.onap.policy.distribution.forwarding.xacml.pdp.XacmlPdpPolicyForwarder; import org.onap.policy.distribution.forwarding.xacml.pdp.XacmlPdpPolicyForwarderParameterGroup.XacmlPdpPolicyForwarderParameterGroupBuilder; -import org.onap.policy.distribution.main.PolicyDistributionException; import org.onap.policy.distribution.model.OptimizationPolicy; import org.onap.policy.distribution.model.Policy; @@ -79,9 +76,7 @@ public class XacmlPdpPolicyForwarderTest { } @Test - public void testForwardPolicy() - throws KeyManagementException, NoSuchAlgorithmException, NoSuchFieldException,SecurityException, - IllegalArgumentException, IllegalAccessException, PolicyDistributionException, ClassNotFoundException { + public void testForwardPolicy() throws Exception { HttpClient httpClientMock = mock(HttpClient.class); headers.put(CLIENT_AUTH, CLIENT_AUTH_VALUE); @@ -91,9 +86,7 @@ public class XacmlPdpPolicyForwarderTest { HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class); when(httpClientFactoryMock.build(argThat(matcher))).thenReturn(httpClientMock); - overwriteField(HttpClient.class, "factory", null, httpClientFactoryMock); - - XacmlPdpPolicyForwarder forwarder = new XacmlPdpPolicyForwarder(); + XacmlPdpPolicyForwarder forwarder = new MyXacmlPdpPolicyForwarder(httpClientFactoryMock); forwarder.configure("xacmlPdpConfiguration"); Collection<Policy> policies = new ArrayList<>(); @@ -122,9 +115,7 @@ public class XacmlPdpPolicyForwarderTest { } @Test - public void testForwardPolicy_CreateFailsPushNotInvoked() - throws KeyManagementException, NoSuchAlgorithmException, NoSuchFieldException, SecurityException, - IllegalArgumentException, IllegalAccessException, PolicyDistributionException, ClassNotFoundException { + public void testForwardPolicy_CreateFailsPushNotInvoked() throws Exception { HttpClient httpClientMock = mock(HttpClient.class); headers.put(CLIENT_AUTH, CLIENT_AUTH_VALUE); @@ -134,9 +125,7 @@ public class XacmlPdpPolicyForwarderTest { HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class); when(httpClientFactoryMock.build(argThat(matcher))).thenReturn(httpClientMock); - overwriteField(HttpClient.class, "factory", null, httpClientFactoryMock); - - XacmlPdpPolicyForwarder forwarder = new XacmlPdpPolicyForwarder(); + XacmlPdpPolicyForwarder forwarder = new MyXacmlPdpPolicyForwarder(httpClientFactoryMock); forwarder.configure("xacmlPdpConfiguration"); Collection<Policy> policies = new ArrayList<>(); @@ -150,9 +139,7 @@ public class XacmlPdpPolicyForwarderTest { } @Test - public void testForwardPolicy_PushFails() - throws KeyManagementException, NoSuchAlgorithmException, NoSuchFieldException, SecurityException, - IllegalArgumentException, IllegalAccessException, PolicyDistributionException, ClassNotFoundException { + public void testForwardPolicy_PushFails() throws Exception { HttpClient httpClientMock = mock(HttpClient.class); headers.put(CLIENT_AUTH, CLIENT_AUTH_VALUE); @@ -162,9 +149,7 @@ public class XacmlPdpPolicyForwarderTest { HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class); when(httpClientFactoryMock.build(argThat(matcher))).thenReturn(httpClientMock); - overwriteField(HttpClient.class, "factory", null, httpClientFactoryMock); - - XacmlPdpPolicyForwarder forwarder = new XacmlPdpPolicyForwarder(); + XacmlPdpPolicyForwarder forwarder = new MyXacmlPdpPolicyForwarder(httpClientFactoryMock); forwarder.configure("xacmlPdpConfiguration"); Collection<Policy> policies = new ArrayList<>(); @@ -179,9 +164,7 @@ public class XacmlPdpPolicyForwarderTest { } @Test - public void testForwardPolicy_HttpClientInitFailureForPolicyCreate() - throws KeyManagementException, NoSuchAlgorithmException, NoSuchFieldException, SecurityException, - IllegalArgumentException, IllegalAccessException, PolicyDistributionException, ClassNotFoundException { + public void testForwardPolicy_HttpClientInitFailureForPolicyCreate() throws Exception { HttpClient httpClientMock = mock(HttpClient.class); headers.put(CLIENT_AUTH, CLIENT_AUTH_VALUE); @@ -189,11 +172,9 @@ public class XacmlPdpPolicyForwarderTest { when(httpClientMock.put(eq("pushPolicy"), anyObject(), eq(headers))).thenReturn(Response.status(400).build()); HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class); - when(httpClientFactoryMock.build(argThat(matcher))).thenThrow(new KeyManagementException()); - - overwriteField(HttpClient.class, "factory", null, httpClientFactoryMock); + when(httpClientFactoryMock.build(argThat(matcher))).thenThrow(new HttpClientConfigException()); - XacmlPdpPolicyForwarder forwarder = new XacmlPdpPolicyForwarder(); + XacmlPdpPolicyForwarder forwarder = new MyXacmlPdpPolicyForwarder(httpClientFactoryMock); forwarder.configure("xacmlPdpConfiguration"); Collection<Policy> policies = new ArrayList<>(); @@ -207,9 +188,7 @@ public class XacmlPdpPolicyForwarderTest { } @Test - public void testForwardPolicy_HttpClientInitFailureForPolicyPush() - throws KeyManagementException, NoSuchAlgorithmException, NoSuchFieldException, SecurityException, - IllegalArgumentException, IllegalAccessException, PolicyDistributionException, ClassNotFoundException { + public void testForwardPolicy_HttpClientInitFailureForPolicyPush() throws Exception { HttpClient httpClientMock = mock(HttpClient.class); headers.put(CLIENT_AUTH, CLIENT_AUTH_VALUE); @@ -218,11 +197,9 @@ public class XacmlPdpPolicyForwarderTest { HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class); when(httpClientFactoryMock.build(argThat(matcher))).thenReturn(httpClientMock) - .thenThrow(new KeyManagementException()); + .thenThrow(new HttpClientConfigException()); - overwriteField(HttpClient.class, "factory", null, httpClientFactoryMock); - - XacmlPdpPolicyForwarder forwarder = new XacmlPdpPolicyForwarder(); + XacmlPdpPolicyForwarder forwarder = new MyXacmlPdpPolicyForwarder(httpClientFactoryMock); forwarder.configure("xacmlPdpConfiguration"); Collection<Policy> policies = new ArrayList<>(); @@ -235,16 +212,6 @@ public class XacmlPdpPolicyForwarderTest { verify(httpClientMock, times(0)).put(eq("pushPolicy"), anyObject(), anyObject()); } - private void overwriteField(final Class<?> clazz, final String fieldName, final Object object, final Object value) - throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException { - Field field = clazz.getField(fieldName); - field.setAccessible(true); - Field modifiersField = Field.class.getDeclaredField("modifiers"); - modifiersField.setAccessible(true); - modifiersField.setInt(field, field.getModifiers() & ~Modifier.FINAL); - field.set(object, value); - } - class BusTopicParamsMatcher extends BaseMatcher<BusTopicParams> { private BusTopicParams busTopicParams; @@ -330,4 +297,17 @@ public class XacmlPdpPolicyForwarderTest { return "unsupported"; } } + + private class MyXacmlPdpPolicyForwarder extends XacmlPdpPolicyForwarder { + private HttpClientFactory factory; + + public MyXacmlPdpPolicyForwarder(HttpClientFactory httpClientFactory) { + this.factory = httpClientFactory; + } + + @Override + protected HttpClientFactory getHttpClientFactory() { + return this.factory; + } + } } |