2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.drools.statemanagement.test;
23 import static org.junit.Assert.assertTrue;
26 import java.io.FileInputStream;
27 import java.io.IOException;
28 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;
36 import org.junit.After;
37 import org.junit.AfterClass;
38 import org.junit.Before;
39 import org.junit.BeforeClass;
40 import org.junit.Test;
41 import org.onap.policy.common.im.StateManagement;
42 import org.onap.policy.drools.core.PolicySessionFeatureAPI;
43 import org.onap.policy.drools.statemanagement.DbAudit;
44 import org.onap.policy.drools.statemanagement.IntegrityMonitorRestManager;
45 import org.onap.policy.drools.statemanagement.RepositoryAudit;
46 import org.onap.policy.drools.statemanagement.StateManagementFeatureAPI;
47 import org.onap.policy.drools.statemanagement.StateManagementProperties;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
51 public class StateManagementTest {
53 // get an instance of logger
54 private static Logger logger = LoggerFactory.getLogger(StateManagementTest.class);
57 * Sleep after each test to allow interrupt (shutdown) recovery.
60 private long interruptRecoveryTime = 1500L;
62 StateManagementFeatureAPI stateManagementFeature;
65 * All you need to do here is create an instance of StateManagementFeature class. Then,
66 * check it initial state and the state after diableFailed() and promote()
70 public static void setUpClass() throws Exception {
72 logger.info("setUpClass: Entering");
74 String userDir = System.getProperty("user.dir");
75 logger.debug("setUpClass: userDir=" + userDir);
76 System.setProperty("com.sun.management.jmxremote.port", "9980");
77 System.setProperty("com.sun.management.jmxremote.authenticate","false");
81 logger.info("setUpClass: Exiting");
85 public static void tearDownClass() throws Exception {
90 public void setUp() throws Exception {
95 public void tearDown() throws Exception {
100 * Verifies that StateManagementFeature starts and runs successfully.
105 public void testStateManagementOperation() throws Exception {
107 logger.debug("\n\ntestStateManagementOperation: Entering\n\n");
109 logger.debug("testStateManagementOperation: Reading StateManagementProperties");
111 String configDir = "src/test/resources";
113 DbAudit.isJunit = true;
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);
121 StateManagementFeatureAPI stateManagementFeature = null;
122 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
124 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
125 stateManagementFeature = feature;
126 logger.debug("testStateManagementOperation stateManagementFeature.getResourceName(): " + stateManagementFeature.getResourceName());
129 if(stateManagementFeature == null){
130 String msg = "testStateManagementOperation failed to initialize. "
131 + "Unable to get instance of StateManagementFeatureAPI "
132 + "with resourceID: " + thisPdpId;
137 Thread.sleep(interruptRecoveryTime);
139 String admin = stateManagementFeature.getAdminState();
140 String oper = stateManagementFeature.getOpState();
141 String avail = stateManagementFeature.getAvailStatus();
142 String standby = stateManagementFeature.getStandbyStatus();
144 logger.debug("admin = {}", admin);
145 logger.debug("oper = {}", oper);
146 logger.debug("avail = {}", avail);
147 logger.debug("standby = {}", standby);
149 assertTrue("Admin state not unlocked after initialization", admin.equals(StateManagement.UNLOCKED));
150 assertTrue("Operational state not enabled after initialization", oper.equals(StateManagement.ENABLED));
153 stateManagementFeature.disableFailed();
155 logger.error(e.getMessage());
156 assertTrue(e.getMessage(), false);
159 Thread.sleep(interruptRecoveryTime);
161 admin = stateManagementFeature.getAdminState();
162 oper = stateManagementFeature.getOpState();
163 avail = stateManagementFeature.getAvailStatus();
164 standby = stateManagementFeature.getStandbyStatus();
166 logger.debug("after disableFailed()");
167 logger.debug("admin = {}", admin);
168 logger.debug("oper = {}", oper);
169 logger.debug("avail = {}", avail);
170 logger.debug("standby = {}", standby);
172 assertTrue("Operational state not disabled after disableFailed()", oper.equals(StateManagement.DISABLED));
173 assertTrue("Availability status not failed after disableFailed()", avail.equals(StateManagement.FAILED));
177 stateManagementFeature.promote();
179 logger.debug(e.getMessage());
182 Thread.sleep(interruptRecoveryTime);
184 admin = stateManagementFeature.getAdminState();
185 oper = stateManagementFeature.getOpState();
186 avail = stateManagementFeature.getAvailStatus();
187 standby = stateManagementFeature.getStandbyStatus();
189 logger.debug("after promote()");
190 logger.debug("admin = {}", admin);
191 logger.debug("oper = {}", oper);
192 logger.debug("avail = {}", avail);
193 logger.debug("standby = {}", standby);
195 assertTrue("Standby status not coldstandby after promote()", standby.equals(StateManagement.COLD_STANDBY));
197 /**************Repository Audit Test**************/
198 logger.debug("\n\ntestStateManagementOperation: Repository Audit\n\n");
200 RepositoryAudit repositoryAudit = (RepositoryAudit) RepositoryAudit.getInstance();
201 repositoryAudit.invoke(fsmProperties);
203 //Should not throw an IOException in Linux Foundation env
205 }catch(IOException e){
206 //Note: this catch is here because in a local environment mvn will not run in
207 //in the temp directory
208 logger.debug("testSubsytemTest RepositoryAudit IOException", e);
209 }catch(InterruptedException e){
211 logger.debug("testSubsytemTest RepositoryAudit InterruptedException", e);
214 /*****************Db Audit Test***************/
215 logger.debug("\n\ntestStateManagementOperation: DB Audit\n\n");
218 DbAudit dbAudit = (DbAudit) DbAudit.getInstance();
219 dbAudit.invoke(fsmProperties);
224 logger.debug("testSubsytemTest DbAudit exception", e);
227 /*************IntegrityMonitorRestManager Test*************/
228 logger.debug("\n\ntestStateManagementOperation: IntegrityMonitorRestManager\n\n");
229 IntegrityMonitorRestManager integrityMonitorRestManager = new IntegrityMonitorRestManager();
231 Response response = integrityMonitorRestManager.test();
232 logger.debug("\n\nIntegrityMonitorRestManager response: " + response.toString());
234 assertTrue(response.toString().contains("status=500"));
237 logger.debug("\n\ntestStateManagementOperation: Exiting\n\n");
241 * This method initializes and cleans the DB so that PDP-D will be able to
242 * store fresh records in the DB.
245 public static void initializeDb(){
247 logger.debug("initializeDb: Entering");
249 Properties cleanProperties = new Properties();
250 cleanProperties.put(StateManagementProperties.DB_DRIVER,"org.h2.Driver");
251 cleanProperties.put(StateManagementProperties.DB_URL, "jdbc:h2:file:./sql/statemanagement");
252 cleanProperties.put(StateManagementProperties.DB_USER, "sa");
253 cleanProperties.put(StateManagementProperties.DB_PWD, "");
255 EntityManagerFactory emf = Persistence.createEntityManagerFactory("junitPU", cleanProperties);
257 EntityManager em = emf.createEntityManager();
258 // Start a transaction
259 EntityTransaction et = em.getTransaction();
264 em.createQuery("Delete from StateManagementEntity").executeUpdate();
265 em.createQuery("Delete from ForwardProgressEntity").executeUpdate();
266 em.createQuery("Delete from ResourceRegistrationEntity").executeUpdate();
268 // commit transaction
272 logger.debug("initializeDb: Exiting");