Modify xacml-pdp to use RestServer from common
[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 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.junit.Assert.assertEquals;
22
23 import java.io.FileInputStream;
24 import java.lang.reflect.Method;
25 import java.sql.Date;
26 import java.time.Instant;
27
28 import java.util.Properties;
29 import java.util.UUID;
30
31 import javax.persistence.EntityManager;
32 import javax.persistence.Persistence;
33 import javax.persistence.Query;
34
35 import org.junit.AfterClass;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.slf4j.Logger;
39 import org.slf4j.LoggerFactory;
40
41 public class CountRecentOperationsPipTest {
42     private static final Logger LOGGER = LoggerFactory.getLogger(CountRecentOperationsPipTest.class);
43     private static CountRecentOperationsPip pipEngine;
44
45     private static EntityManager em;
46
47     /**
48      * Create an instance of our engine and also the persistence
49      * factory.
50      *
51      * @throws Exception connectivity issues
52      */
53     @BeforeClass
54     public static void setup() throws Exception {
55         LOGGER.info("Setting up PIP Testing");
56         //
57         // Create instance
58         //
59         pipEngine = new CountRecentOperationsPip();
60         //
61         // Load our test properties to use
62         //
63         Properties properties = new Properties();
64         try (FileInputStream is = new FileInputStream("src/test/resources/test.properties")) {
65             properties.load(is);
66         }
67         //
68         // Configure it using properties
69         //
70         pipEngine.configure("issuer", properties);
71         LOGGER.info("PIP configured now creating our entity manager");
72         LOGGER.info("properties {}", properties);
73         //
74         // Connect to in-mem db
75         //
76         String persistenceUnit = CountRecentOperationsPip.ISSUER_NAME + ".persistenceunit";
77         LOGGER.info("persistenceunit {}", persistenceUnit);
78         em = Persistence.createEntityManagerFactory(properties.getProperty(persistenceUnit), properties)
79                 .createEntityManager();
80         //
81         //
82         //
83         LOGGER.info("Configured own entity manager", em.toString());
84     }
85
86     private Dbao createEntry(String cl, String target, String outcome) {
87         //
88         // Create entry
89         //
90         Dbao newEntry = new Dbao();
91         newEntry.setClosedLoopName(cl);
92         newEntry.setTarget(target);
93         newEntry.setOutcome(outcome);
94         newEntry.setActor("Controller");
95         newEntry.setOperation("operationA");
96         newEntry.setStarttime(Date.from(Instant.now().minusMillis(20000)));
97         newEntry.setEndtime(Date.from(Instant.now()));
98         newEntry.setRequestId(UUID.randomUUID().toString());
99         return newEntry;
100     }
101
102     @Test
103     public void testGetCountFromDb() throws Exception {
104         //
105         // Use reflection to run getCountFromDB
106         //
107         Method method = CountRecentOperationsPip.class.getDeclaredMethod("doDatabaseQuery",
108                                                                             String.class,
109                                                                             String.class,
110                                                                             String.class,
111                                                                             int.class,
112                                                                             String.class);
113         method.setAccessible(true);
114         //
115         // create entry
116         //
117         Dbao newEntry = createEntry("cl-foobar-1", "vnf-1", "SUCCESS");
118         //
119         // Test pipEngine
120         //
121         long count = (long) method.invoke(pipEngine, newEntry.getActor(), newEntry.getOperation(), newEntry.getTarget(),
122                 1, "HOUR");
123         //
124         // No entries yet
125         //
126         assertEquals(0, count);
127         //
128         // Add entry
129         //
130         em.getTransaction().begin();
131         em.persist(newEntry);
132         em.getTransaction().commit();
133         //
134         // Directly check ground truth
135         //
136         Query queryCount = em.createNativeQuery("select count(*) as numops from operationshistory")
137                 .setParameter(1, 1);
138         LOGGER.info("{} entries", queryCount.getSingleResult());
139         //
140         // Test pipEngine
141         //
142         count = (long) method.invoke(pipEngine, newEntry.getActor(), newEntry.getOperation(), newEntry.getTarget(),
143                 1, "HOUR");
144         //
145         // Should count 1 entry now
146         //
147         assertEquals(1, count);
148     }
149
150     /**
151      * Close the entity manager.
152      */
153     @AfterClass
154     public static void cleanup() {
155         if (em != null) {
156             em.close();
157         }
158     }
159
160 }