037f49a476deffc7ff3c096e2215683bef94bd31
[policy/xacml-pdp.git] / applications / common / src / test / java / org / onap / policy / pdp / xacml / application / common / operationshistory / GetOperationOutcomePipTest.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 import java.util.Properties;
28 import java.util.UUID;
29
30 import javax.persistence.EntityManager;
31 import javax.persistence.Persistence;
32
33 import org.junit.AfterClass;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.slf4j.Logger;
37 import org.slf4j.LoggerFactory;
38
39 public class GetOperationOutcomePipTest {
40     private static final Logger LOGGER = LoggerFactory.getLogger(GetOperationOutcomePipTest.class);
41     private static GetOperationOutcomePip pipEngine;
42
43     private static EntityManager em;
44
45     /**
46      * Create an instance of our engine and also the persistence
47      * factory.
48      *
49      * @throws Exception connectivity issues
50      */
51     @BeforeClass
52     public static void setup() throws Exception {
53         LOGGER.info("Setting up PIP Testing");
54         //
55         // Create instance
56         //
57         pipEngine = new GetOperationOutcomePip();
58         //
59         // Load our test properties to use
60         //
61         Properties properties = new Properties();
62         try (FileInputStream is = new FileInputStream("src/test/resources/test.properties")) {
63             properties.load(is);
64         }
65         //
66         // Configure it using properties
67         //
68         pipEngine.configure("issuer", properties);
69         LOGGER.info("PIP configured now creating our entity manager");
70         LOGGER.info("properties {}", properties);
71         //
72         // Connect to in-mem db
73         //
74         String persistenceUnit = GetOperationOutcomePip.ISSUER_NAME + ".persistenceunit";
75         LOGGER.info("persistenceunit {}", persistenceUnit);
76         em = Persistence.createEntityManagerFactory(properties.getProperty(persistenceUnit), properties)
77             .createEntityManager();
78         //
79         //
80         //
81         LOGGER.info("Configured own entity manager", em.toString());
82     }
83
84     private void insertEntry(String cl, String target, String outcome) {
85         //
86         // Create entry
87         //
88         Dbao newEntry = new Dbao();
89         newEntry.setClosedLoopName(cl);
90         newEntry.setTarget(target);
91         newEntry.setOutcome(outcome);
92         newEntry.setActor("Controller");
93         newEntry.setOperation("operationA");
94         newEntry.setStarttime(Date.from(Instant.now().minusMillis(20000)));
95         newEntry.setEndtime(Date.from(Instant.now()));
96         newEntry.setRequestId(UUID.randomUUID().toString());
97         //
98         // Add entry
99         //
100         em.getTransaction().begin();
101         em.persist(newEntry);
102         em.getTransaction().commit();
103     }
104
105
106     @Test
107     public void testGetOutcomeFromDb() throws Exception {
108         //
109         // Use reflection to run getCountFromDB
110         //
111         Method method = GetOperationOutcomePip.class.getDeclaredMethod("doDatabaseQuery",
112                                                                        String.class,
113                                                                        String.class);
114         method.setAccessible(true);
115         //
116         // Insert entry
117         //
118         insertEntry("testcl1", "testtarget1", "1");
119         //
120         // Test pipEngine
121         //
122         String outcome = (String) method.invoke(pipEngine, "testcl1", "testtarget1");
123         //
124         // outcome should be "1"
125         //
126         assertEquals("1", outcome);
127         //
128         // Insert more entries
129         //
130         insertEntry("testcl1", "testtarget1", "2");
131         insertEntry("testcl2", "testtarget2", "3");
132         insertEntry("testcl1", "testtarget2", "4");
133         //
134         // Test pipEngine
135         //
136         outcome = (String) method.invoke(pipEngine, "testcl1", "testtarget1");
137         assertEquals("2", outcome);
138
139         outcome = (String) method.invoke(pipEngine, "testcl2", "testtarget2");
140         assertEquals("3", outcome);
141
142         outcome = (String) method.invoke(pipEngine, "testcl1", "testtarget2");
143         assertEquals("4", outcome);
144     }
145
146     /**
147      * Close the entity manager.
148      */
149     @AfterClass
150     public static void cleanup() {
151         if (em != null) {
152             em.close();
153         }
154     }
155
156 }