2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020 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.assertThat;
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.junit.Assert.assertNotEquals;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.when;
30 import com.att.research.xacml.api.Status;
31 import com.att.research.xacml.api.pip.PIPException;
32 import com.att.research.xacml.api.pip.PIPFinder;
33 import com.att.research.xacml.api.pip.PIPRequest;
34 import com.att.research.xacml.api.pip.PIPResponse;
35 import com.att.research.xacml.std.pip.StdPIPResponse;
36 import java.io.FileInputStream;
37 import java.lang.reflect.Method;
39 import java.time.Instant;
40 import java.util.Properties;
41 import java.util.UUID;
42 import javax.persistence.EntityManager;
43 import javax.persistence.Persistence;
44 import org.junit.AfterClass;
45 import org.junit.Before;
46 import org.junit.BeforeClass;
47 import org.junit.Test;
48 import org.mockito.Mock;
49 import org.mockito.MockitoAnnotations;
50 import org.onap.policy.pdp.xacml.application.common.ToscaDictionary;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
54 public class GetOperationOutcomePipTest {
55 private static final Logger LOGGER = LoggerFactory.getLogger(GetOperationOutcomePipTest.class);
56 private static final String TEST_PROPERTIES = "src/test/resources/test.properties";
58 private static EntityManager em;
60 private Properties properties;
61 private GetOperationOutcomePip pipEngine;
64 private PIPRequest pipRequest;
67 private PIPFinder pipFinder;
70 private PIPResponse resp1;
73 private Status okStatus;
76 * Create an instance of our engine and also the persistence
79 * @throws Exception connectivity issues
82 public static void setupDatabase() throws Exception {
83 LOGGER.info("Setting up PIP Testing");
85 // Load our test properties to use
87 Properties props = new Properties();
88 try (FileInputStream is = new FileInputStream(TEST_PROPERTIES)) {
92 // Connect to in-mem db
94 String persistenceUnit = GetOperationOutcomePip.ISSUER_NAME + ".persistenceunit";
95 LOGGER.info("persistenceunit {}", persistenceUnit);
96 em = Persistence.createEntityManagerFactory(props.getProperty(persistenceUnit), props)
97 .createEntityManager();
101 LOGGER.info("Configured own entity manager", em.toString());
105 * Close the entity manager.
108 public static void cleanup() {
115 * Create an instance of our engine.
117 * @throws Exception if an error occurs
120 public void setupEngine() throws Exception {
121 MockitoAnnotations.initMocks(this);
123 when(pipRequest.getIssuer()).thenReturn("urn:org:onap:xacml:guard:tw:1:hour");
127 pipEngine = new GetOperationOutcomePip();
129 // Load the properties
131 properties = new Properties();
132 try (FileInputStream is = new FileInputStream(TEST_PROPERTIES)) {
136 // Configure it using properties
138 pipEngine.configure("issuer", properties);
139 LOGGER.info("PIP configured now creating our entity manager");
140 LOGGER.info("properties {}", properties);
144 public void testAttributesRequired() {
145 assertEquals(1, pipEngine.attributesRequired().size());
149 public void testConfigure_DbException() throws Exception {
150 properties.put("javax.persistence.jdbc.url", "invalid");
152 pipEngine.configure("issuer", properties)
153 ).doesNotThrowAnyException();
157 public void testGetAttributes_NullIssuer() throws PIPException {
158 when(pipRequest.getIssuer()).thenReturn(null);
159 assertEquals(StdPIPResponse.PIP_RESPONSE_EMPTY, pipEngine.getAttributes(pipRequest, pipFinder));
163 public void testGetAttributes_WrongIssuer() throws PIPException {
164 when(pipRequest.getIssuer()).thenReturn("wrong-issuer");
165 assertEquals(StdPIPResponse.PIP_RESPONSE_EMPTY, pipEngine.getAttributes(pipRequest, pipFinder));
169 public void testGetAttributes() throws Exception {
173 when(pipRequest.getIssuer()).thenReturn(ToscaDictionary.GUARD_ISSUER_PREFIX + "clname:clfoo");
174 when(pipFinder.getMatchingAttributes(any(), eq(pipEngine))).thenReturn(resp1);
175 when(resp1.getStatus()).thenReturn(okStatus);
176 when(okStatus.isOk()).thenReturn(true);
178 assertNotEquals(StdPIPResponse.PIP_RESPONSE_EMPTY, pipEngine.getAttributes(pipRequest, pipFinder));
180 pipEngine.shutdown();
182 assertThatExceptionOfType(PIPException.class).isThrownBy(() -> pipEngine.getAttributes(pipRequest, pipFinder))
183 .withMessageContaining("Engine is shutdown");
187 public void testGetOutcomeFromDb() throws Exception {
189 // Use reflection to run getCountFromDB
191 Method method = GetOperationOutcomePip.class.getDeclaredMethod("doDatabaseQuery",
194 method.setAccessible(true);
198 String outcome = (String) method.invoke(pipEngine, "testcl1", "testtarget1");
199 assertThat(outcome).isNull();
203 insertEntry("testcl1", "testtarget1", "1");
207 outcome = (String) method.invoke(pipEngine, "testcl1", "testtarget1");
209 // outcome should be "1"
211 assertEquals("1", outcome);
213 // Insert more entries
215 insertEntry("testcl1", "testtarget1", "2");
216 insertEntry("testcl2", "testtarget2", "3");
217 insertEntry("testcl1", "testtarget2", "4");
221 outcome = (String) method.invoke(pipEngine, "testcl1", "testtarget1");
222 assertEquals("2", outcome);
224 outcome = (String) method.invoke(pipEngine, "testcl2", "testtarget2");
225 assertEquals("3", outcome);
227 outcome = (String) method.invoke(pipEngine, "testcl1", "testtarget2");
228 assertEquals("4", outcome);
233 pipEngine.shutdown();
235 assertThat(method.invoke(pipEngine, "testcl1", "testtarget2")).isNull();
238 private void insertEntry(String cl, String target, String outcome) {
242 Dbao newEntry = new Dbao();
243 newEntry.setClosedLoopName(cl);
244 newEntry.setTarget(target);
245 newEntry.setOutcome(outcome);
246 newEntry.setActor("Controller");
247 newEntry.setOperation("operationA");
248 newEntry.setStarttime(Date.from(Instant.now().minusMillis(20000)));
249 newEntry.setEndtime(Date.from(Instant.now()));
250 newEntry.setRequestId(UUID.randomUUID().toString());
254 em.getTransaction().begin();
255 em.persist(newEntry);
256 em.getTransaction().commit();