Fix distribution due to sonar changes in common 45/91045/3
authorJim Hahn <jrh3@att.com>
Mon, 8 Jul 2019 16:07:42 +0000 (12:07 -0400)
committerJim Hahn <jrh3@att.com>
Mon, 8 Jul 2019 20:17:49 +0000 (16:17 -0400)
Fixed breakages due to changes made in policy/common to satisfy
sonar.

Change-Id: Ib1e57668f727db1b87a9aa3bc9a8266884f5e994
Issue-ID: POLICY-1791
Signed-off-by: Jim Hahn <jrh3@att.com>
main/src/main/java/org/onap/policy/distribution/main/rest/DistributionRestServer.java
plugins/forwarding-plugins/src/main/java/org/onap/policy/distribution/forwarding/xacml/pdp/XacmlPdpPolicyForwarder.java
plugins/forwarding-plugins/src/test/java/org/onap/policy/distribution/forwarding/xacml/pdp/engine/XacmlPdpPolicyForwarderTest.java

index 0a03bcb..895827f 100644 (file)
@@ -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();
             }
index 6db5665..196818b 100644 (file)
@@ -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();
+    }
 }
index 6982b33..43fac71 100644 (file)
@@ -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;
+        }
+    }
 }