222614d9e61c337f28d41e5ca2c66d89ee7d16b2
[policy/distribution.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
5  *  Modifications Copyright (C) 2019 Nordix Foundation.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.distribution.forwarding.xacml.pdp.engine;
24
25 import static org.junit.Assert.assertSame;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.argThat;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33
34 import java.util.ArrayList;
35 import java.util.Collection;
36 import java.util.HashMap;
37
38 import javax.ws.rs.client.Entity;
39 import javax.ws.rs.core.Response;
40
41 import org.junit.BeforeClass;
42 import org.junit.Test;
43 import org.mockito.ArgumentMatcher;
44 import org.onap.policy.api.PolicyParameters;
45 import org.onap.policy.api.PushPolicyParameters;
46 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
47 import org.onap.policy.common.endpoints.http.client.HttpClient;
48 import org.onap.policy.common.endpoints.http.client.HttpClientConfigException;
49 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
50 import org.onap.policy.common.parameters.ParameterGroup;
51 import org.onap.policy.common.parameters.ParameterService;
52 import org.onap.policy.distribution.forwarding.xacml.pdp.XacmlPdpPolicyForwarder;
53 import org.onap.policy.distribution.forwarding.xacml.pdp.XacmlPdpPolicyForwarderParameterGroup;
54 import org.onap.policy.distribution.forwarding.xacml.pdp.testclasses.CommonTestData;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaEntity;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
57
58 public class XacmlPdpPolicyForwarderTest {
59
60     private static final BusTopicParams BUS_TOPIC_PARAMS = BusTopicParams.builder().useHttps(true)
61             .hostname("10.10.10.10").port(1234).userName("myUser").password("myPassword").managed(false).build();
62     private static final String CLIENT_AUTH = "ClientAuth";
63     private static final String CLIENT_AUTH_VALUE = "myClientAuth";
64     private static final String PDP_GROUP_VALUE = "myPdpGroup";
65     private HashMap<String, Object> headers = new HashMap<>();
66     private BusTopicParamsMatcher matcher = new BusTopicParamsMatcher(BUS_TOPIC_PARAMS);
67
68     /**
69      * Set up.
70      */
71     @BeforeClass
72     public static void setUp() {
73         final ParameterGroup parameterGroup = CommonTestData.getPolicyForwarderParameters(
74                 "src/test/resources/parameters/XacmlPdpPolicyForwarderParameters.json",
75                 XacmlPdpPolicyForwarderParameterGroup.class);
76         parameterGroup.setName("xacmlPdpConfiguration");
77         ParameterService.register(parameterGroup);
78     }
79
80     @Test
81     public void testForwardPolicy() throws Exception {
82
83         final HttpClient httpClientMock = mock(HttpClient.class);
84         headers.put(CLIENT_AUTH, CLIENT_AUTH_VALUE);
85         when(httpClientMock.put(eq("createPolicy"), any(), eq(headers))).thenReturn(Response.ok().build());
86         when(httpClientMock.put(eq("pushPolicy"), any(), eq(headers))).thenReturn(Response.ok().build());
87
88         final HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class);
89         when(httpClientFactoryMock.build(argThat(matcher))).thenReturn(httpClientMock);
90
91         final XacmlPdpPolicyForwarder forwarder = new MyXacmlPdpPolicyForwarder(httpClientFactoryMock);
92         forwarder.configure("xacmlPdpConfiguration");
93
94         final Collection<ToscaEntity> policies = new ArrayList<>();
95
96         final ToscaPolicy policy1 = createPolicy(policies, "policy1", "optimization");
97
98         final ToscaEntity policy2 = new UnsupportedPolicy();
99         policies.add(policy2);
100
101         final ToscaPolicy policy3 = createPolicy(policies, "policy3", "optimization");
102
103         forwarder.forward(policies);
104
105         verify(httpClientMock).put(eq("createPolicy"), argThat(new PolicyParametersEntityMatcher(policy1)),
106                 eq(headers));
107         verify(httpClientMock).put(eq("createPolicy"), argThat(new PolicyParametersEntityMatcher(policy3)),
108                 eq(headers));
109         verify(httpClientMock).put(eq("pushPolicy"), argThat(new PushPolicyParametersEntityMatcher(policy1)),
110                 eq(headers));
111         verify(httpClientMock).put(eq("pushPolicy"), argThat(new PushPolicyParametersEntityMatcher(policy3)),
112                 eq(headers));
113     }
114
115     @Test
116     public void testForwardPolicy_CreateFailsPushNotInvoked() throws Exception {
117
118         final HttpClient httpClientMock = mock(HttpClient.class);
119         headers.put(CLIENT_AUTH, CLIENT_AUTH_VALUE);
120         when(httpClientMock.put(eq("createPolicy"), any(), eq(headers))).thenReturn(Response.status(400).build());
121         when(httpClientMock.put(eq("pushPolicy"), any(), eq(headers))).thenReturn(Response.ok().build());
122
123         final HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class);
124         when(httpClientFactoryMock.build(argThat(matcher))).thenReturn(httpClientMock);
125
126         final XacmlPdpPolicyForwarder forwarder = new MyXacmlPdpPolicyForwarder(httpClientFactoryMock);
127         forwarder.configure("xacmlPdpConfiguration");
128
129         final Collection<ToscaEntity> policies = new ArrayList<>();
130         final ToscaPolicy policy = createPolicy(policies, "policy", "optimization");
131         forwarder.forward(policies);
132
133         verify(httpClientMock).put(eq("createPolicy"), argThat(new PolicyParametersEntityMatcher(policy)), eq(headers));
134         verify(httpClientMock, times(0)).put(eq("pushPolicy"), any(), any());
135     }
136
137     @Test
138     public void testForwardPolicy_PushFails() throws Exception {
139
140         final HttpClient httpClientMock = mock(HttpClient.class);
141         headers.put(CLIENT_AUTH, CLIENT_AUTH_VALUE);
142         when(httpClientMock.put(eq("createPolicy"), any(), eq(headers))).thenReturn(Response.ok().build());
143         when(httpClientMock.put(eq("pushPolicy"), any(), eq(headers))).thenReturn(Response.status(400).build());
144
145         final HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class);
146         when(httpClientFactoryMock.build(argThat(matcher))).thenReturn(httpClientMock);
147
148         final XacmlPdpPolicyForwarder forwarder = new MyXacmlPdpPolicyForwarder(httpClientFactoryMock);
149         forwarder.configure("xacmlPdpConfiguration");
150
151         final Collection<ToscaEntity> policies = new ArrayList<>();
152         final ToscaPolicy policy = createPolicy(policies, "policy", "optimization");
153         forwarder.forward(policies);
154
155         verify(httpClientMock).put(eq("createPolicy"), argThat(new PolicyParametersEntityMatcher(policy)), eq(headers));
156         verify(httpClientMock).put(eq("pushPolicy"), argThat(new PushPolicyParametersEntityMatcher(policy)),
157                 eq(headers));
158     }
159
160     @Test
161     public void testForwardPolicy_HttpClientInitFailureForPolicyCreate() throws Exception {
162
163         final HttpClient httpClientMock = mock(HttpClient.class);
164         headers.put(CLIENT_AUTH, CLIENT_AUTH_VALUE);
165         when(httpClientMock.put(eq("createPolicy"), any(), eq(headers))).thenReturn(Response.ok().build());
166         when(httpClientMock.put(eq("pushPolicy"), any(), eq(headers))).thenReturn(Response.status(400).build());
167
168         final HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class);
169         when(httpClientFactoryMock.build(argThat(matcher))).thenThrow(new HttpClientConfigException());
170
171         final XacmlPdpPolicyForwarder forwarder = new MyXacmlPdpPolicyForwarder(httpClientFactoryMock);
172         forwarder.configure("xacmlPdpConfiguration");
173
174         final Collection<ToscaEntity> policies = new ArrayList<>();
175         final ToscaPolicy policy = createPolicy(policies, "policy", "optimization");
176         forwarder.forward(policies);
177
178         assertSame(policy, policies.iterator().next());
179
180         verify(httpClientMock, times(0)).put(eq("createPolicy"), any(), any());
181         verify(httpClientMock, times(0)).put(eq("pushPolicy"), any(), any());
182     }
183
184     @Test
185     public void testForwardPolicy_HttpClientInitFailureForPolicyPush() throws Exception {
186
187         final HttpClient httpClientMock = mock(HttpClient.class);
188         headers.put(CLIENT_AUTH, CLIENT_AUTH_VALUE);
189         when(httpClientMock.put(eq("createPolicy"), any(), eq(headers))).thenReturn(Response.ok().build());
190         when(httpClientMock.put(eq("pushPolicy"), any(), eq(headers))).thenReturn(Response.status(400).build());
191
192         final HttpClientFactory httpClientFactoryMock = mock(HttpClientFactory.class);
193         when(httpClientFactoryMock.build(argThat(matcher))).thenReturn(httpClientMock)
194                 .thenThrow(new HttpClientConfigException());
195
196         final XacmlPdpPolicyForwarder forwarder = new MyXacmlPdpPolicyForwarder(httpClientFactoryMock);
197         forwarder.configure("xacmlPdpConfiguration");
198
199         final Collection<ToscaEntity> policies = new ArrayList<>();
200         final ToscaPolicy policy = createPolicy(policies, "policy", "optimization");
201         forwarder.forward(policies);
202
203         verify(httpClientMock).put(eq("createPolicy"), argThat(new PolicyParametersEntityMatcher(policy)), eq(headers));
204         verify(httpClientMock, times(0)).put(eq("pushPolicy"), any(), any());
205     }
206
207     class BusTopicParamsMatcher implements ArgumentMatcher<BusTopicParams> {
208
209         private BusTopicParams busTopicParams;
210
211         BusTopicParamsMatcher(final BusTopicParams busTopicParams) {
212             this.busTopicParams = busTopicParams;
213         }
214
215         @Override
216         public boolean matches(final BusTopicParams arg0) {
217             if (arg0 instanceof BusTopicParams) {
218                 final BusTopicParams toCompareTo = (BusTopicParams) arg0;
219                 return toCompareTo.isUseHttps() == busTopicParams.isUseHttps()
220                         && toCompareTo.getHostname().equals(busTopicParams.getHostname())
221                         && toCompareTo.getPort() == busTopicParams.getPort()
222                         && toCompareTo.getUserName().equals(busTopicParams.getUserName())
223                         && toCompareTo.getPassword().equals(busTopicParams.getPassword())
224                         && toCompareTo.isManaged() == busTopicParams.isManaged();
225             }
226             return false;
227         }
228     }
229
230     class PolicyParametersEntityMatcher implements ArgumentMatcher<Entity<PolicyParameters>> {
231
232         private ToscaPolicy policy;
233
234         PolicyParametersEntityMatcher(final ToscaPolicy policy) {
235             this.policy = policy;
236         }
237
238         @SuppressWarnings("unchecked")
239         @Override
240         public boolean matches(final Entity<PolicyParameters> arg0) {
241             if (arg0 instanceof Entity) {
242                 final PolicyParameters toCompareTo = ((Entity<PolicyParameters>) arg0).getEntity();
243                 return toCompareTo.getPolicyName().equals(policy.getName());
244             }
245             return false;
246         }
247     }
248
249     class PushPolicyParametersEntityMatcher implements ArgumentMatcher<Entity<PushPolicyParameters>> {
250
251         private ToscaPolicy policy;
252
253         PushPolicyParametersEntityMatcher(final ToscaPolicy policy) {
254             this.policy = policy;
255         }
256
257         @SuppressWarnings("unchecked")
258         @Override
259         public boolean matches(final Entity<PushPolicyParameters> arg0) {
260             if (arg0 instanceof Entity) {
261                 final PushPolicyParameters toCompareTo = ((Entity<PushPolicyParameters>) arg0).getEntity();
262                 return toCompareTo.getPolicyName().equals(policy.getName())
263                         && toCompareTo.getPolicyType().equals(policy.getType())
264                         && toCompareTo.getPdpGroup().equals(PDP_GROUP_VALUE);
265             }
266             return false;
267         }
268     }
269
270     class UnsupportedPolicy extends ToscaEntity {
271
272         @Override
273         public String getName() {
274             return "unsupported";
275         }
276     }
277
278     private class MyXacmlPdpPolicyForwarder extends XacmlPdpPolicyForwarder {
279         private HttpClientFactory factory;
280
281         public MyXacmlPdpPolicyForwarder(final HttpClientFactory httpClientFactory) {
282             this.factory = httpClientFactory;
283         }
284
285         @Override
286         protected HttpClientFactory getHttpClientFactory() {
287             return this.factory;
288         }
289     }
290
291     private ToscaPolicy createPolicy(final Collection<ToscaEntity> policies, final String name, final String type) {
292         final ToscaPolicy policy1 = new ToscaPolicy();
293         policy1.setName(name);
294         policy1.setType(type);
295         policies.add(policy1);
296         return policy1;
297     }
298 }