33bfaedc10da0e4db5918a6116d99af7b5e570fd
[policy/drools-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy-persistence
4  * ================================================================================
5  * Copyright (C) 2017-2020 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.drools.statemanagement.test;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25
26 import java.io.File;
27 import java.io.FileInputStream;
28 import java.io.IOException;
29 import java.util.Properties;
30 import javax.persistence.EntityManager;
31 import javax.persistence.EntityManagerFactory;
32 import javax.persistence.EntityTransaction;
33 import javax.persistence.Persistence;
34 import javax.ws.rs.core.Response;
35 import org.junit.After;
36 import org.junit.AfterClass;
37 import org.junit.Before;
38 import org.junit.BeforeClass;
39 import org.junit.Test;
40 import org.onap.policy.common.im.StateManagement;
41 import org.onap.policy.drools.core.PolicySessionFeatureApi;
42 import org.onap.policy.drools.statemanagement.DbAudit;
43 import org.onap.policy.drools.statemanagement.IntegrityMonitorRestManager;
44 import org.onap.policy.drools.statemanagement.RepositoryAudit;
45 import org.onap.policy.drools.statemanagement.StateManagementFeatureApi;
46 import org.onap.policy.drools.statemanagement.StateManagementFeatureApiConstants;
47 import org.onap.policy.drools.statemanagement.StateManagementProperties;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50
51 public class StateManagementTest {
52
53     // get an instance of logger
54     private static Logger  logger = LoggerFactory.getLogger(StateManagementTest.class);
55
56     private static EntityManagerFactory emf;
57     private static EntityManager em;
58
59     StateManagementFeatureApi stateManagementFeature;
60
61     /**
62      * Setup the class.
63      * All you need to do here is create an instance of StateManagementFeature class.  Then,
64      * check it initial state and the state after diableFailed() and promote()
65      *
66      * @throws Exception exception
67      */
68     @BeforeClass
69     public static void setUpClass() throws Exception {
70
71         logger.info("setUpClass: Entering");
72
73         String userDir = System.getProperty("user.dir");
74         logger.debug("setUpClass: userDir=" + userDir);
75         System.setProperty("com.sun.management.jmxremote.port", "9980");
76         System.setProperty("com.sun.management.jmxremote.authenticate", "false");
77
78         initializeDb();
79
80         logger.info("setUpClass: Exiting");
81     }
82
83     @AfterClass
84     public static void tearDownClass() throws Exception {
85         em.close();
86         emf.close();
87     }
88
89     @Before
90     public void setUp() throws Exception {
91
92     }
93
94     @After
95     public void tearDown() throws Exception {
96
97     }
98
99     /*
100      * Verifies that StateManagementFeature starts and runs successfully.
101      */
102
103     //@Ignore
104     @Test
105     public void testStateManagementOperation() throws Exception {
106
107         logger.debug("\n\ntestStateManagementOperation: Entering\n\n");
108
109         logger.debug("testStateManagementOperation: Reading StateManagementProperties");
110
111         String configDir = "src/test/resources";
112
113         DbAudit.setIsJunit(true);
114
115         Properties fsmProperties = new Properties();
116         fsmProperties.load(new FileInputStream(new File(
117                 configDir + "/feature-state-management.properties")));
118         String thisPdpId = fsmProperties
119                 .getProperty(StateManagementProperties.NODE_NAME);
120
121         StateManagementFeatureApi stateManagementFeature = null;
122         for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
123             ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
124             stateManagementFeature = feature;
125             logger.debug("testStateManagementOperation stateManagementFeature.getResourceName(): "
126                 + stateManagementFeature.getResourceName());
127             break;
128         }
129         if (stateManagementFeature == null) {
130             String msg = "testStateManagementOperation failed to initialize.  "
131                     + "Unable to get instance of StateManagementFeatureApi "
132                     + "with resourceID: " + thisPdpId;
133             logger.error(msg);
134             logger.debug(msg);
135         }
136
137         String admin = stateManagementFeature.getAdminState();
138         String oper = stateManagementFeature.getOpState();
139         String avail = stateManagementFeature.getAvailStatus();
140         String standby = stateManagementFeature.getStandbyStatus();
141
142         logger.debug("admin = {}", admin);
143         logger.debug("oper = {}", oper);
144         logger.debug("avail = {}", avail);
145         logger.debug("standby = {}", standby);
146
147         assertEquals("Admin state not unlocked after initialization", StateManagement.UNLOCKED, admin);
148         assertEquals("Operational state not enabled after initialization", StateManagement.ENABLED, oper);
149
150         try {
151             stateManagementFeature.disableFailed();
152         } catch (Exception e) {
153             logger.error(e.getMessage());
154             assertTrue(e.getMessage(), false);
155         }
156
157         admin = stateManagementFeature.getAdminState();
158         oper = stateManagementFeature.getOpState();
159         avail = stateManagementFeature.getAvailStatus();
160         standby = stateManagementFeature.getStandbyStatus();
161
162         logger.debug("after disableFailed()");
163         logger.debug("admin = {}", admin);
164         logger.debug("oper = {}", oper);
165         logger.debug("avail = {}", avail);
166         logger.debug("standby = {}", standby);
167
168         assertEquals("Operational state not disabled after disableFailed()", StateManagement.DISABLED, oper);
169         assertEquals("Availability status not failed after disableFailed()", StateManagement.FAILED, avail);
170
171
172         try {
173             stateManagementFeature.promote();
174         } catch (Exception e) {
175             logger.debug(e.getMessage());
176         }
177
178         admin = stateManagementFeature.getAdminState();
179         oper = stateManagementFeature.getOpState();
180         avail = stateManagementFeature.getAvailStatus();
181         standby = stateManagementFeature.getStandbyStatus();
182
183         logger.debug("after promote()");
184         logger.debug("admin = {}", admin);
185         logger.debug("oper = {}", oper);
186         logger.debug("avail = {}", avail);
187         logger.debug("standby = {}", standby);
188
189         assertEquals("Standby status not coldstandby after promote()", StateManagement.COLD_STANDBY, standby);
190
191         /* *************Repository Audit Test. ************* */
192         logger.debug("\n\ntestStateManagementOperation: Repository Audit\n\n");
193         try {
194             StateManagementProperties.initProperties(fsmProperties);
195             RepositoryAudit repositoryAudit = (RepositoryAudit) RepositoryAudit.getInstance();
196             repositoryAudit.invoke(fsmProperties);
197
198             //Should not throw an IOException in Linux Foundation env
199             assertTrue(true);
200         } catch (IOException e) {
201             //Note: this catch is here because in a local environment mvn will not run in
202             //in the temp directory
203             logger.debug("testSubsytemTest RepositoryAudit IOException", e);
204         } catch (InterruptedException e) {
205             assertTrue(false);
206             logger.debug("testSubsytemTest RepositoryAudit InterruptedException", e);
207         }
208
209         /* ****************Db Audit Test. ************** */
210         logger.debug("\n\ntestStateManagementOperation: DB Audit\n\n");
211
212         try {
213             DbAudit dbAudit = (DbAudit) DbAudit.getInstance();
214             dbAudit.invoke(fsmProperties);
215
216             assertTrue(true);
217         } catch (Exception e) {
218             assertTrue(false);
219             logger.debug("testSubsytemTest DbAudit exception", e);
220         }
221
222         /* ************IntegrityMonitorRestManager Test. ************ */
223         logger.debug("\n\ntestStateManagementOperation: IntegrityMonitorRestManager\n\n");
224         IntegrityMonitorRestManager integrityMonitorRestManager = new IntegrityMonitorRestManager();
225
226         Response response = integrityMonitorRestManager.test();
227         logger.debug("\n\nIntegrityMonitorRestManager response: " + response.toString());
228
229         assertTrue(response.toString().contains("status=500"));
230
231         //All done
232         logger.debug("\n\ntestStateManagementOperation: Exiting\n\n");
233     }
234
235     /**
236      * This method initializes and cleans the DB so that PDP-D will be able to
237      * store fresh records in the DB.
238      */
239     public static void initializeDb() {
240
241         logger.debug("initializeDb: Entering");
242
243         Properties cleanProperties = new Properties();
244         cleanProperties.put(StateManagementProperties.DB_DRIVER, "org.h2.Driver");
245         cleanProperties.put(StateManagementProperties.DB_URL, "jdbc:h2:mem:statemanagement");
246         cleanProperties.put(StateManagementProperties.DB_USER, "sa");
247         cleanProperties.put(StateManagementProperties.DB_PWD, "");
248
249         emf = Persistence.createEntityManagerFactory("junitPU", cleanProperties);
250
251         em = emf.createEntityManager();
252         // Start a transaction
253         EntityTransaction et = em.getTransaction();
254
255         et.begin();
256
257         // Clean up the DB
258         em.createQuery("Delete from StateManagementEntity").executeUpdate();
259         em.createQuery("Delete from ForwardProgressEntity").executeUpdate();
260         em.createQuery("Delete from ResourceRegistrationEntity").executeUpdate();
261
262         // commit transaction
263         et.commit();
264
265         logger.debug("initializeDb: Exiting");
266     }
267 }