2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2021 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
9 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
19 package org.onap.policy.pdp.xacml.application.common.operationshistory;
21 import static org.assertj.core.api.Assertions.assertThatCode;
22 import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.Mockito.when;
26 import com.att.research.xacml.api.Attribute;
27 import com.att.research.xacml.api.AttributeValue;
28 import com.att.research.xacml.api.Status;
29 import com.att.research.xacml.api.pip.PIPException;
30 import com.att.research.xacml.api.pip.PIPFinder;
31 import com.att.research.xacml.api.pip.PIPRequest;
32 import com.att.research.xacml.api.pip.PIPResponse;
33 import com.att.research.xacml.std.pip.StdPIPResponse;
34 import java.io.FileInputStream;
35 import java.io.IOException;
37 import java.time.Instant;
38 import java.util.Arrays;
39 import java.util.LinkedList;
40 import java.util.Properties;
41 import java.util.Queue;
42 import java.util.UUID;
43 import javax.persistence.EntityManager;
44 import javax.persistence.Persistence;
45 import javax.persistence.Query;
46 import org.junit.AfterClass;
47 import org.junit.Before;
48 import org.junit.BeforeClass;
49 import org.junit.Test;
50 import org.junit.runner.RunWith;
51 import org.mockito.Mock;
52 import org.mockito.junit.MockitoJUnitRunner;
53 import org.onap.policy.guard.OperationsHistory;
54 import org.slf4j.Logger;
55 import org.slf4j.LoggerFactory;
57 @RunWith(MockitoJUnitRunner.class)
58 public class CountRecentOperationsPipTest {
59 private static final Logger LOGGER = LoggerFactory.getLogger(CountRecentOperationsPipTest.class);
61 private static final String ACTOR = "my-actor";
62 private static final String RECIPE = "my-recipe";
63 private static final String TARGET = "my-target";
64 private static final String TEST_PROPERTIES = "src/test/resources/test.properties";
66 private static EntityManager em;
69 private PIPRequest pipRequest;
72 private PIPFinder pipFinder;
75 private PIPResponse resp1;
78 private PIPResponse resp2;
81 private PIPResponse resp3;
84 private Status okStatus;
86 private Properties properties;
87 private Queue<PIPResponse> responses;
88 private Queue<String> attributes;
90 private CountRecentOperationsPip pipEngine;
93 * Establishes a connection to the DB and keeps it open until all tests have
96 * @throws IOException if properties cannot be loaded
99 public static void setUpBeforeClass() throws IOException {
101 // Load our test properties to use
103 Properties props2 = new Properties();
104 try (FileInputStream is = new FileInputStream(TEST_PROPERTIES)) {
108 // Connect to in-mem db
110 String persistenceUnit = CountRecentOperationsPip.ISSUER_NAME + ".persistenceunit";
111 LOGGER.info("persistenceunit {}", persistenceUnit);
112 em = Persistence.createEntityManagerFactory(props2.getProperty(persistenceUnit), props2).createEntityManager();
116 LOGGER.info("Configured own entity manager", em.toString());
120 * Close the entity manager.
123 public static void cleanup() {
130 * Create an instance of our engine.
132 * @throws Exception if an error occurs
135 public void setUp() throws Exception {
136 when(pipRequest.getIssuer()).thenReturn("urn:org:onap:xacml:guard:tw:1:hour");
138 pipEngine = new MyPip();
140 properties = new Properties();
141 try (FileInputStream is = new FileInputStream(TEST_PROPERTIES)) {
145 responses = new LinkedList<>(Arrays.asList(resp1, resp2, resp3));
146 attributes = new LinkedList<>(Arrays.asList(ACTOR, RECIPE, TARGET));
150 public void testAttributesRequired() {
151 assertEquals(3, pipEngine.attributesRequired().size());
155 public void testConfigure_DbException() throws Exception {
156 properties.put("javax.persistence.jdbc.url", "invalid");
158 pipEngine.configure("issuer", properties)
159 ).doesNotThrowAnyException();
163 public void testGetAttributes_NullIssuer() throws PIPException {
164 when(pipRequest.getIssuer()).thenReturn(null);
165 assertEquals(StdPIPResponse.PIP_RESPONSE_EMPTY, pipEngine.getAttributes(pipRequest, pipFinder));
169 public void testGetAttributes_WrongIssuer() throws PIPException {
170 when(pipRequest.getIssuer()).thenReturn("wrong-issuer");
171 assertEquals(StdPIPResponse.PIP_RESPONSE_EMPTY, pipEngine.getAttributes(pipRequest, pipFinder));
175 public void testGetAttributes_NullActor() throws PIPException {
176 attributes = new LinkedList<>(Arrays.asList(null, RECIPE, TARGET));
177 assertEquals(StdPIPResponse.PIP_RESPONSE_EMPTY, pipEngine.getAttributes(pipRequest, pipFinder));
181 public void testGetAttributes_NullRecipe() throws PIPException {
182 attributes = new LinkedList<>(Arrays.asList(ACTOR, null, TARGET));
183 assertEquals(StdPIPResponse.PIP_RESPONSE_EMPTY, pipEngine.getAttributes(pipRequest, pipFinder));
187 public void testGetAttributes_NullTarget() throws PIPException {
188 attributes = new LinkedList<>(Arrays.asList(ACTOR, RECIPE, null));
189 assertEquals(StdPIPResponse.PIP_RESPONSE_EMPTY, pipEngine.getAttributes(pipRequest, pipFinder));
193 public void testShutdown() {
194 pipEngine.shutdown();
195 assertThatExceptionOfType(PIPException.class).isThrownBy(() -> pipEngine.getAttributes(pipRequest, pipFinder))
196 .withMessageContaining("Engine is shutdown");
200 public void testGetCountFromDb() throws Exception {
202 // Configure it using properties
204 pipEngine.configure("issuer", properties);
205 LOGGER.info("PIP configured now creating our entity manager");
206 LOGGER.info("properties {}", properties);
210 OperationsHistory newEntry = createEntry("cl-foobar-1", "vnf-1", "SUCCESS");
214 assertEquals(0, getCount(newEntry));
218 em.getTransaction().begin();
219 em.persist(newEntry);
220 em.getTransaction().commit();
222 // Directly check ground truth
224 Query queryCount = em.createNativeQuery("select count(*) as numops from operationshistory").setParameter(1, 1);
225 LOGGER.info("{} entries", queryCount.getSingleResult());
227 // Should count 1 entry now
229 assertEquals(1, getCount(newEntry));
233 public void testStringToChronoUnit() throws PIPException {
234 // not configured yet
235 OperationsHistory newEntry = createEntry("cl-foobar-1", "vnf-1", "SUCCESS");
236 assertEquals(-1, getCount(newEntry));
239 pipEngine.configure("issuer", properties);
241 String[] units = {"second", "minute", "hour", "day", "week", "month", "year"};
243 for (String unit : units) {
244 when(pipRequest.getIssuer()).thenReturn("urn:org:onap:xacml:guard:tw:1:" + unit);
247 * It would be better to use assertEquals below, but the test DB doesn't
248 * support week, month, or year.
251 // should run without throwing an exception
256 when(pipRequest.getIssuer()).thenReturn("urn:org:onap:xacml:guard:tw:1:invalid");
257 assertEquals(-1, getCount(newEntry));
260 private long getCount(OperationsHistory newEntry) throws PIPException {
261 responses = new LinkedList<>(Arrays.asList(resp1, resp2, resp3));
262 attributes = new LinkedList<>(
263 Arrays.asList(newEntry.getActor(), newEntry.getOperation(), newEntry.getTarget()));
265 PIPResponse result = pipEngine.getAttributes(pipRequest, pipFinder);
267 Attribute attr = result.getAttributes().iterator().next();
268 AttributeValue<?> value = attr.getValues().iterator().next();
270 return ((Number) value.getValue()).longValue();
273 private OperationsHistory createEntry(String cl, String target, String outcome) {
277 OperationsHistory newEntry = new OperationsHistory();
278 newEntry.setClosedLoopName(cl);
279 newEntry.setTarget(target);
280 newEntry.setOutcome(outcome);
281 newEntry.setActor("Controller");
282 newEntry.setOperation("operationA");
283 newEntry.setStarttime(Date.from(Instant.now().minusMillis(20000)));
284 newEntry.setEndtime(Date.from(Instant.now()));
285 newEntry.setRequestId(UUID.randomUUID().toString());
289 private class MyPip extends CountRecentOperationsPip {
292 protected PIPResponse getAttribute(PIPRequest pipRequest, PIPFinder pipFinder) {
293 return responses.remove();
297 protected String findFirstAttributeValue(PIPResponse pipResponse) {
298 return attributes.remove();