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
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.database.operationshistory;
21 import static org.junit.Assert.assertEquals;
22 import static org.mockito.ArgumentMatchers.any;
23 import static org.mockito.ArgumentMatchers.anyInt;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.when;
27 import com.att.research.xacml.api.Attribute;
28 import com.att.research.xacml.api.AttributeValue;
29 import com.att.research.xacml.api.XACML3;
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;
37 import java.time.Instant;
38 import java.util.Collection;
39 import java.util.Properties;
40 import java.util.UUID;
41 import javax.persistence.EntityManager;
42 import javax.persistence.EntityManagerFactory;
43 import javax.persistence.Persistence;
44 import javax.persistence.Query;
45 import org.junit.AfterClass;
46 import org.junit.Before;
47 import org.junit.BeforeClass;
48 import org.junit.Test;
49 import org.onap.policy.database.ToscaDictionary;
50 import org.slf4j.Logger;
51 import org.slf4j.LoggerFactory;
53 public class CountRecentOperationsPipTest {
54 private static final String ID = "issuer";
55 private static final Logger LOGGER = LoggerFactory.getLogger(CountRecentOperationsPipTest.class);
56 private static final String ISSUER = ToscaDictionary.GUARD_ISSUER_PREFIX + "-my-issuer:tw:1:HOUR";
57 private static final String ACTOR = "Controller";
58 private static final String RECIPE = "operationA";
59 private static final String TARGET = "vnf-1";
60 private static final String EXPECTED_EXCEPTION = "expected exception";
62 private static MyPip pipEngine;
63 private static Properties properties;
65 private static EntityManagerFactory emf;
66 private static EntityManager em;
68 private PIPRequest req;
71 * Create an instance of our engine and also the persistence
74 * @throws Exception connectivity issues
77 public static void setUpBeforeClass() throws Exception {
78 LOGGER.info("Setting up PIP Testing");
82 pipEngine = new MyPip();
84 // Load our test properties to use
86 properties = new Properties();
87 try (FileInputStream is = new FileInputStream("src/test/resources/test.properties")) {
91 // Configure it using properties
93 pipEngine.configure(ID, properties);
94 LOGGER.info("PIP configured now creating our entity manager");
95 LOGGER.info("properties {}", properties);
97 // Connect to in-mem db
99 String persistenceUnit = CountRecentOperationsPip.ISSUER_NAME + ".persistenceunit";
100 LOGGER.info("persistenceunit {}", persistenceUnit);
101 emf = Persistence.createEntityManagerFactory(properties.getProperty(persistenceUnit), properties);
102 em = emf.createEntityManager();
106 LOGGER.info("Configured own entity manager");
110 public void setUp() {
111 req = mock(PIPRequest.class);
112 when(req.getIssuer()).thenReturn(ISSUER);
115 private Dbao createEntry(String cl, String target, String outcome) {
119 Dbao newEntry = new Dbao();
120 newEntry.setClosedLoopName(cl);
121 newEntry.setTarget(target);
122 newEntry.setOutcome(outcome);
123 newEntry.setActor(ACTOR);
124 newEntry.setOperation(RECIPE);
125 newEntry.setStarttime(Date.from(Instant.now().minusMillis(20000)));
126 newEntry.setEndtime(Date.from(Instant.now()));
127 newEntry.setRequestId(UUID.randomUUID().toString());
132 public void testAttributesRequired() {
133 assertEquals(3, pipEngine.attributesRequired().size());
137 public void testGetAttributes_InvalidRequestInfo() throws PIPException {
138 // invalid request - null issuer
139 when(req.getIssuer()).thenReturn(null);
140 assertEquals(StdPIPResponse.PIP_RESPONSE_EMPTY, pipEngine.getAttributes(req, null));
143 * Make a valid issuer in the request, for subsequent tests.
145 when(req.getIssuer()).thenReturn(ISSUER);
148 MyPip pip = new MyPip() {
150 protected String getActor(PIPFinder pipFinder) {
154 pip.configure(ID, properties);
155 assertEquals(StdPIPResponse.PIP_RESPONSE_EMPTY, pip.getAttributes(req, null));
160 protected String getRecipe(PIPFinder pipFinder) {
164 pip.configure(ID, properties);
165 assertEquals(StdPIPResponse.PIP_RESPONSE_EMPTY, pip.getAttributes(req, null));
170 protected String getTarget(PIPFinder pipFinder) {
174 pip.configure(ID, properties);
175 assertEquals(StdPIPResponse.PIP_RESPONSE_EMPTY, pip.getAttributes(req, null));
179 public void testDoDatabaseQuery() throws Exception {
184 assertEquals(0, getCount(pipEngine.getAttributes(req, null)));
188 em.getTransaction().begin();
189 em.persist(createEntry("cl-foobar-1", TARGET, "SUCCESS"));
190 em.getTransaction().commit();
192 // Directly check ground truth
194 Query queryCount = em.createNativeQuery("select count(*) as numops from operationshistory")
196 LOGGER.info("{} entries", queryCount.getSingleResult());
198 // Should count 1 entry now
200 assertEquals(1, getCount(pipEngine.getAttributes(req, null)));
204 public void testDoDatabaseQuery_InvalidTimeWindow() throws Exception {
205 when(req.getIssuer()).thenReturn(ISSUER + "invalid time window");
207 assertEquals(-1, getCount(pipEngine.getAttributes(req, null)));
211 public void testDoDatabaseQuery_NullEm() throws Exception {
212 assertEquals(-1, getCount(new MyPip().getAttributes(req, null)));
216 public void testDoDatabaseQuery_EmException() throws Exception {
217 MyPip pip = new MyPip() {
219 public void configure(String id, Properties properties) throws PIPException {
220 em = mock(EntityManager.class);
221 when(em.createNativeQuery(any())).thenThrow(new RuntimeException(EXPECTED_EXCEPTION));
224 pip.configure(ID, properties);
226 assertEquals(-1, getCount(pip.getAttributes(req, null)));
230 public void testDoDatabaseQuery_NonNumeric() throws Exception {
231 MyPip pip = new MyPip() {
233 public void configure(String id, Properties properties) throws PIPException {
234 em = mock(EntityManager.class);
235 Query query = mock(Query.class);
236 when(em.createNativeQuery(any())).thenReturn(query);
237 when(query.setParameter(anyInt(), any())).thenReturn(query);
238 when(query.getSingleResult()).thenReturn("200");
241 pip.configure(ID, properties);
243 assertEquals(200, getCount(pip.getAttributes(req, null)));
246 private int getCount(PIPResponse resp) {
247 Collection<Attribute> attrs = resp.getAttributes();
248 assertEquals(1, attrs.size());
250 Attribute attr = attrs.iterator().next();
251 assertEquals(XACML3.ID_ATTRIBUTE_CATEGORY_RESOURCE, attr.getCategory());
252 assertEquals(ToscaDictionary.ID_RESOURCE_GUARD_OPERATIONCOUNT, attr.getAttributeId());
254 Collection<AttributeValue<?>> values = attr.getValues();
255 assertEquals(1, values.size());
257 AttributeValue<?> value = values.iterator().next();
258 return ((Number) value.getValue()).intValue();
262 * Close the entity manager.
265 public static void cleanup() {
271 private static class MyPip extends CountRecentOperationsPip {
274 protected String getActor(PIPFinder pipFinder) {
279 protected String getRecipe(PIPFinder pipFinder) {
284 protected String getTarget(PIPFinder pipFinder) {