Changes to handle PDPX deploy/undeploy
[policy/xacml-pdp.git] / applications / common / src / test / java / org / onap / policy / pdp / xacml / application / common / OnapOperationsHistoryPipEngineTest.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;
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 import java.util.Properties;
28 import java.util.UUID;
29
30 import javax.persistence.EntityManager;
31 import javax.persistence.Persistence;
32 import javax.persistence.Query;
33
34 import org.junit.AfterClass;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.slf4j.Logger;
38 import org.slf4j.LoggerFactory;
39
40 public class OnapOperationsHistoryPipEngineTest {
41     private static final Logger LOGGER = LoggerFactory.getLogger(OnapOperationsHistoryPipEngineTest.class);
42     private static OnapOperationsHistoryPipEngine pipEngine;
43
44     private static EntityManager em;
45
46     /**
47      * Create an instance of our engine and also the persistence
48      * factory.
49      *
50      * @throws Exception connectivity issues
51      */
52     @BeforeClass
53     public static void setUp() throws Exception {
54         LOGGER.info("Setting up PIP Testing");
55         //
56         // Create instance
57         //
58         pipEngine = new OnapOperationsHistoryPipEngine();
59         //
60         // Load our test properties to use
61         //
62         Properties properties = new Properties();
63         try (FileInputStream is = new FileInputStream("src/test/resources/test.properties")) {
64             properties.load(is);
65         }
66         //
67         // Configure it using properties
68         //
69         pipEngine.configure("issuer", properties);
70         LOGGER.info("PIP configured now creating our entity manager");
71         //
72         // Connect to in-mem db
73         //
74         em = Persistence.createEntityManagerFactory(properties.getProperty("historydb.persistenceunit"), properties)
75                 .createEntityManager();
76         //
77         //
78         //
79         LOGGER.info("Configured own entity manager", em.toString());
80     }
81
82     /**
83      * Close the entity manager.
84      */
85     @AfterClass
86     public static void tearDown() {
87         if (em != null) {
88             em.close();
89         }
90     }
91
92     @Test
93     public void testGetCountFromDb() throws Exception {
94
95         // Add an entry
96         OnapOperationsHistoryDbao newEntry = new OnapOperationsHistoryDbao();
97         newEntry.setActor("Controller");
98         newEntry.setOperation("operationA");
99         newEntry.setClName("cl-foobar-1");
100         newEntry.setOutcome("SUCCESS");
101         newEntry.setStarttime(Date.from(Instant.now().minusMillis(20000)));
102         newEntry.setEndtime(Date.from(Instant.now()));
103         newEntry.setRequestId(UUID.randomUUID().toString());
104         newEntry.setTarget("vnf-1");
105
106         // Use reflection to run getCountFromDB
107         Method method = OnapOperationsHistoryPipEngine.class.getDeclaredMethod("doDatabaseQuery",
108                                                                             String.class,
109                                                                             String.class,
110                                                                             String.class,
111                                                                             int.class,
112                                                                             String.class);
113         method.setAccessible(true);
114         int count = (int) method.invoke(pipEngine, newEntry.getActor(), newEntry.getOperation(), newEntry.getTarget(),
115                 1, "HOUR");
116
117         // No entries yet
118         assertEquals(0, count);
119
120
121         em.getTransaction().begin();
122         em.persist(newEntry);
123         em.getTransaction().commit();
124
125         Query queryCount = em.createNativeQuery("select count(*) as numops from operationshistory")
126                 .setParameter(1, 1);
127         LOGGER.info("{} entries", queryCount.getSingleResult());
128
129         count = (int) method.invoke(pipEngine, newEntry.getActor(), newEntry.getOperation(), newEntry.getTarget(),
130                 1, "HOUR");
131         // Should count 1 entry now
132         assertEquals(1, count);
133     }
134
135 }