ac8a0c29596961d9b12738017a700fb1aa41978d
[policy/xacml-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=========================================================
17  */
18
19 package org.onap.policy.pdp.xacml.application.common.operationshistory;
20
21 import static org.assertj.core.api.Assertions.assertThatCode;
22 import static org.junit.Assert.assertEquals;
23 import static org.mockito.Matchers.any;
24 import static org.mockito.Matchers.eq;
25 import static org.mockito.Mockito.when;
26
27 import com.att.research.xacml.api.Attribute;
28 import com.att.research.xacml.api.AttributeValue;
29 import com.att.research.xacml.api.Status;
30 import com.att.research.xacml.api.pip.PIPException;
31 import com.att.research.xacml.api.pip.PIPFinder;
32 import com.att.research.xacml.api.pip.PIPRequest;
33 import com.att.research.xacml.api.pip.PIPResponse;
34 import com.att.research.xacml.std.pip.StdPIPResponse;
35 import java.io.FileInputStream;
36 import java.io.IOException;
37 import java.sql.Date;
38 import java.time.Instant;
39 import java.util.Arrays;
40 import java.util.LinkedList;
41 import java.util.Properties;
42 import java.util.Queue;
43 import java.util.UUID;
44 import javax.persistence.EntityManager;
45 import javax.persistence.Persistence;
46 import javax.persistence.Query;
47 import org.junit.AfterClass;
48 import org.junit.Before;
49 import org.junit.BeforeClass;
50 import org.junit.Test;
51 import org.mockito.Mock;
52 import org.mockito.MockitoAnnotations;
53 import org.slf4j.Logger;
54 import org.slf4j.LoggerFactory;
55
56 public class CountRecentOperationsPipTest {
57     private static final Logger LOGGER = LoggerFactory.getLogger(CountRecentOperationsPipTest.class);
58
59     private static final String ACTOR = "my-actor";
60     private static final String RECIPE = "my-recipe";
61     private static final String TARGET = "my-target";
62     private static final String TEST_PROPERTIES = "src/test/resources/test.properties";
63
64     private static EntityManager em;
65
66     @Mock
67     private PIPRequest pipRequest;
68
69     @Mock
70     private PIPFinder pipFinder;
71
72     @Mock
73     private PIPResponse resp1;
74
75     @Mock
76     private PIPResponse resp2;
77
78     @Mock
79     private PIPResponse resp3;
80
81     @Mock
82     private Status okStatus;
83
84     private Properties properties;
85     private Queue<PIPResponse> responses;
86     private Queue<String> attributes;
87
88     private CountRecentOperationsPip pipEngine;
89
90     /**
91      * Establishes a connection to the DB and keeps it open until all tests have
92      * completed.
93      *
94      * @throws IOException if properties cannot be loaded
95      */
96     @BeforeClass
97     public static void setUpBeforeClass() throws IOException {
98         //
99         // Load our test properties to use
100         //
101         Properties props2 = new Properties();
102         try (FileInputStream is = new FileInputStream(TEST_PROPERTIES)) {
103             props2.load(is);
104         }
105         //
106         // Connect to in-mem db
107         //
108         String persistenceUnit = CountRecentOperationsPip.ISSUER_NAME + ".persistenceunit";
109         LOGGER.info("persistenceunit {}", persistenceUnit);
110         em = Persistence.createEntityManagerFactory(props2.getProperty(persistenceUnit), props2).createEntityManager();
111         //
112         //
113         //
114         LOGGER.info("Configured own entity manager", em.toString());
115     }
116
117     /**
118      * Create an instance of our engine.
119      *
120      * @throws Exception if an error occurs
121      */
122     @Before
123     public void setUp() throws Exception {
124         MockitoAnnotations.initMocks(this);
125
126         when(pipRequest.getIssuer()).thenReturn("urn:org:onap:xacml:guard:tw:1:hour");
127
128         pipEngine = new MyPip();
129
130         properties = new Properties();
131         try (FileInputStream is = new FileInputStream(TEST_PROPERTIES)) {
132             properties.load(is);
133         }
134
135         when(pipFinder.getMatchingAttributes(any(), eq(pipEngine))).thenReturn(resp1, resp2, resp3);
136
137         responses = new LinkedList<>(Arrays.asList(resp1, resp2, resp3));
138         attributes = new LinkedList<>(Arrays.asList(ACTOR, RECIPE, TARGET));
139
140         when(resp1.getStatus()).thenReturn(okStatus);
141         when(resp2.getStatus()).thenReturn(okStatus);
142         when(resp3.getStatus()).thenReturn(okStatus);
143
144         when(okStatus.isOk()).thenReturn(true);
145     }
146
147     private Dbao createEntry(String cl, String target, String outcome) {
148         //
149         // Create entry
150         //
151         Dbao newEntry = new Dbao();
152         newEntry.setClosedLoopName(cl);
153         newEntry.setTarget(target);
154         newEntry.setOutcome(outcome);
155         newEntry.setActor("Controller");
156         newEntry.setOperation("operationA");
157         newEntry.setStarttime(Date.from(Instant.now().minusMillis(20000)));
158         newEntry.setEndtime(Date.from(Instant.now()));
159         newEntry.setRequestId(UUID.randomUUID().toString());
160         return newEntry;
161     }
162
163     @Test
164     public void testAttributesRequired() {
165         assertEquals(3, pipEngine.attributesRequired().size());
166     }
167
168     @Test
169     public void testConfigure_DbException() throws Exception {
170         properties.put("javax.persistence.jdbc.url", "invalid");
171         assertThatCode(() ->
172             pipEngine.configure("issuer", properties)
173         ).doesNotThrowAnyException();
174     }
175
176     @Test
177     public void testGetAttributes_NullIssuer() throws PIPException {
178         when(pipRequest.getIssuer()).thenReturn(null);
179         assertEquals(StdPIPResponse.PIP_RESPONSE_EMPTY, pipEngine.getAttributes(pipRequest, pipFinder));
180     }
181
182     @Test
183     public void testGetAttributes_WrongIssuer() throws PIPException {
184         when(pipRequest.getIssuer()).thenReturn("wrong-issuer");
185         assertEquals(StdPIPResponse.PIP_RESPONSE_EMPTY, pipEngine.getAttributes(pipRequest, pipFinder));
186     }
187
188     @Test
189     public void testGetAttributes_NullActor() throws PIPException {
190         attributes = new LinkedList<>(Arrays.asList(null, RECIPE, TARGET));
191         assertEquals(StdPIPResponse.PIP_RESPONSE_EMPTY, pipEngine.getAttributes(pipRequest, pipFinder));
192     }
193
194     @Test
195     public void testGetAttributes_NullRecipe() throws PIPException {
196         attributes = new LinkedList<>(Arrays.asList(ACTOR, null, TARGET));
197         assertEquals(StdPIPResponse.PIP_RESPONSE_EMPTY, pipEngine.getAttributes(pipRequest, pipFinder));
198     }
199
200     @Test
201     public void testGetAttributes_NullTarget() throws PIPException {
202         attributes = new LinkedList<>(Arrays.asList(ACTOR, RECIPE, null));
203         assertEquals(StdPIPResponse.PIP_RESPONSE_EMPTY, pipEngine.getAttributes(pipRequest, pipFinder));
204     }
205
206     @Test
207     public void testGetCountFromDb() throws Exception {
208         //
209         // Configure it using properties
210         //
211         pipEngine.configure("issuer", properties);
212         LOGGER.info("PIP configured now creating our entity manager");
213         LOGGER.info("properties {}", properties);
214         //
215         // create entry
216         //
217         Dbao newEntry = createEntry("cl-foobar-1", "vnf-1", "SUCCESS");
218         //
219         // No entries yet
220         //
221         assertEquals(0, getCount(newEntry));
222         //
223         // Add entry
224         //
225         em.getTransaction().begin();
226         em.persist(newEntry);
227         em.getTransaction().commit();
228         //
229         // Directly check ground truth
230         //
231         Query queryCount = em.createNativeQuery("select count(*) as numops from operationshistory").setParameter(1, 1);
232         LOGGER.info("{} entries", queryCount.getSingleResult());
233         //
234         // Should count 1 entry now
235         //
236         assertEquals(1, getCount(newEntry));
237     }
238
239     private long getCount(Dbao newEntry) throws PIPException {
240         responses = new LinkedList<>(Arrays.asList(resp1, resp2, resp3));
241         attributes = new LinkedList<>(
242                         Arrays.asList(newEntry.getActor(), newEntry.getOperation(), newEntry.getTarget()));
243
244         PIPResponse result = pipEngine.getAttributes(pipRequest, pipFinder);
245
246         Attribute attr = result.getAttributes().iterator().next();
247         AttributeValue<?> value = attr.getValues().iterator().next();
248
249         return ((Number) value.getValue()).longValue();
250     }
251
252     @Test
253     public void testStringToChronoUnit() throws PIPException {
254         // not configured yet
255         Dbao newEntry = createEntry("cl-foobar-1", "vnf-1", "SUCCESS");
256         assertEquals(-1, getCount(newEntry));
257
258         // now configure it
259         pipEngine.configure("issuer", properties);
260
261         String[] units = {"second", "minute", "hour", "day", "week", "month", "year"};
262
263         for (String unit : units) {
264             when(pipRequest.getIssuer()).thenReturn("urn:org:onap:xacml:guard:tw:1:" + unit);
265
266             /*
267              * It would be better to use assertEquals below, but the test DB doesn't
268              * support week, month, or year.
269              */
270
271             // should run without throwing an exception
272             getCount(newEntry);
273         }
274
275         // invalid time unit
276         when(pipRequest.getIssuer()).thenReturn("urn:org:onap:xacml:guard:tw:1:invalid");
277         assertEquals(-1, getCount(newEntry));
278     }
279
280     /**
281      * Close the entity manager.
282      */
283     @AfterClass
284     public static void cleanup() {
285         if (em != null) {
286             em.close();
287         }
288     }
289
290     private class MyPip extends CountRecentOperationsPip {
291
292         @Override
293         protected PIPResponse getAttribute(PIPRequest pipRequest, PIPFinder pipFinder) {
294             return responses.remove();
295         }
296
297         @Override
298         protected String findFirstAttributeValue(PIPResponse pipResponse) {
299             return attributes.remove();
300         }
301     }
302 }