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