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