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.util.Properties;
29 import javax.persistence.EntityManager;
30 import javax.persistence.EntityManagerFactory;
31 import javax.persistence.EntityTransaction;
32 import javax.persistence.Persistence;
34 import org.junit.After;
35 import org.junit.AfterClass;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Ignore;
39 import org.junit.Test;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.onap.policy.common.im.StateManagement;
43 import org.onap.policy.drools.core.PolicySessionFeatureAPI;
44 import org.onap.policy.drools.statemanagement.StateManagementFeatureAPI;
45 import org.onap.policy.drools.statemanagement.StateManagementProperties;
47 public class StateManagementTest {
49 // get an instance of logger
50 private static Logger logger = LoggerFactory.getLogger(StateManagementTest.class);
53 * Sleep 5 seconds after each test to allow interrupt (shutdown) recovery.
56 private long interruptRecoveryTime = 1000;
58 StateManagementFeatureAPI stateManagementFeature;
61 * All you need to do here is create an instance of StateManagementFeature class. Then,
62 * check it initial state and the state after diableFailed() and promote()
66 public static void setUpClass() throws Exception {
68 logger.info("setUpClass: Entering");
70 String userDir = System.getProperty("user.dir");
71 logger.debug("setUpClass: userDir=" + userDir);
72 System.setProperty("com.sun.management.jmxremote.port", "9980");
73 System.setProperty("com.sun.management.jmxremote.authenticate","false");
77 logger.info("setUpClass: Exiting");
81 public static void tearDownClass() throws Exception {
86 public void setUp() throws Exception {
91 public void tearDown() throws Exception {
97 * Verifies that StateManagementFeature starts and runs successfully.
102 public void testStateManagementOperation() throws Exception {
104 logger.debug("\n\ntestStateManagementOperation: Entering\n\n");
106 logger.debug("testStateManagementOperation: Reading StateManagementProperties");
108 String configDir = "src/test/resources";
110 Properties fsmProperties = new Properties();
111 fsmProperties.load(new FileInputStream(new File(
112 configDir + "/feature-state-management.properties")));
113 String thisPdpId = fsmProperties
114 .getProperty(StateManagementProperties.NODE_NAME);
116 StateManagementFeatureAPI stateManagementFeature = null;
117 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
119 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
120 stateManagementFeature = feature;
121 logger.debug("testStateManagementOperation stateManagementFeature.getResourceName(): " + stateManagementFeature.getResourceName());
124 if(stateManagementFeature == null){
125 String msg = "testStateManagementOperation failed to initialize. "
126 + "Unable to get instance of StateManagementFeatureAPI "
127 + "with resourceID: " + thisPdpId;
132 Thread.sleep(interruptRecoveryTime);
134 String admin = stateManagementFeature.getAdminState();
135 String oper = stateManagementFeature.getOpState();
136 String avail = stateManagementFeature.getAvailStatus();
137 String standby = stateManagementFeature.getStandbyStatus();
139 logger.debug("admin = {}", admin);
140 System.out.println("admin = " + admin);
141 logger.debug("oper = {}", oper);
142 System.out.println("oper = " + oper);
143 logger.debug("avail = {}", avail);
144 System.out.println("avail = " + avail);
145 logger.debug("standby = {}", standby);
146 System.out.println("standby = " + standby);
148 assertTrue("Admin state not unlocked after initialization", admin.equals(StateManagement.UNLOCKED));
149 assertTrue("Operational state not enabled after initialization", oper.equals(StateManagement.ENABLED));
152 stateManagementFeature.disableFailed();
154 logger.error(e.getMessage());
155 System.out.println(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 System.out.println("after disableFailed()");
168 logger.debug("admin = {}", admin);
169 System.out.println("admin = " + admin);
170 logger.debug("oper = {}", oper);
171 System.out.println("oper = " + oper);
172 logger.debug("avail = {}", avail);
173 System.out.println("avail = " + avail);
174 logger.debug("standby = {}", standby);
175 System.out.println("standby = " + standby);
177 assertTrue("Operational state not disabled after disableFailed()", oper.equals(StateManagement.DISABLED));
178 assertTrue("Availability status not failed after disableFailed()", avail.equals(StateManagement.FAILED));
182 stateManagementFeature.promote();
184 logger.debug(e.getMessage());
185 System.out.println(e.getMessage());
188 Thread.sleep(interruptRecoveryTime);
190 admin = stateManagementFeature.getAdminState();
191 oper = stateManagementFeature.getOpState();
192 avail = stateManagementFeature.getAvailStatus();
193 standby = stateManagementFeature.getStandbyStatus();
195 logger.debug("after promote()");
196 System.out.println("after promote()");
197 logger.debug("admin = {}", admin);
198 System.out.println("admin = " + admin);
199 logger.debug("oper = {}", oper);
200 System.out.println("oper = " + oper);
201 logger.debug("avail = {}", avail);
202 System.out.println("avail = " + avail);
203 logger.debug("standby = {}", standby);
204 System.out.println("standby = " + standby);
206 assertTrue("Standby status not coldstandby after promote()", standby.equals(StateManagement.COLD_STANDBY));
208 logger.debug("\n\ntestStateManagementOperation: Exiting\n\n");
212 * This method initializes and cleans the DB so that PDP-D will be able to
213 * store fresh records in the DB.
216 public static void initializeDb(){
218 logger.debug("initializeDb: Entering");
220 Properties cleanProperties = new Properties();
221 cleanProperties.put(StateManagementProperties.DB_DRIVER,"org.h2.Driver");
222 cleanProperties.put(StateManagementProperties.DB_URL, "jdbc:h2:file:./sql/statemanagement");
223 cleanProperties.put(StateManagementProperties.DB_USER, "sa");
224 cleanProperties.put(StateManagementProperties.DB_PWD, "");
226 EntityManagerFactory emf = Persistence.createEntityManagerFactory("junitPU", cleanProperties);
228 EntityManager em = emf.createEntityManager();
229 // Start a transaction
230 EntityTransaction et = em.getTransaction();
235 em.createQuery("Delete from StateManagementEntity").executeUpdate();
236 em.createQuery("Delete from ForwardProgressEntity").executeUpdate();
237 em.createQuery("Delete from ResourceRegistrationEntity").executeUpdate();
239 // commit transaction
243 logger.debug("initializeDb: Exiting");