2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2018 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);
56 StateManagementFeatureAPI stateManagementFeature;
60 * All you need to do here is create an instance of StateManagementFeature class. Then,
61 * check it initial state and the state after diableFailed() and promote()
63 * @throws Exception exception
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 {
96 * Verifies that StateManagementFeature starts and runs successfully.
101 public void testStateManagementOperation() throws Exception {
103 logger.debug("\n\ntestStateManagementOperation: Entering\n\n");
105 logger.debug("testStateManagementOperation: Reading StateManagementProperties");
107 String configDir = "src/test/resources";
109 DbAudit.setIsJunit(true);
111 Properties fsmProperties = new Properties();
112 fsmProperties.load(new FileInputStream(new File(
113 configDir + "/feature-state-management.properties")));
114 String thisPdpId = fsmProperties
115 .getProperty(StateManagementProperties.NODE_NAME);
117 StateManagementFeatureAPI stateManagementFeature = null;
118 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList()) {
119 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
120 stateManagementFeature = feature;
121 logger.debug("testStateManagementOperation stateManagementFeature.getResourceName(): "
122 + stateManagementFeature.getResourceName());
125 if (stateManagementFeature == null) {
126 String msg = "testStateManagementOperation failed to initialize. "
127 + "Unable to get instance of StateManagementFeatureAPI "
128 + "with resourceID: " + thisPdpId;
133 String admin = stateManagementFeature.getAdminState();
134 String oper = stateManagementFeature.getOpState();
135 String avail = stateManagementFeature.getAvailStatus();
136 String standby = stateManagementFeature.getStandbyStatus();
138 logger.debug("admin = {}", admin);
139 logger.debug("oper = {}", oper);
140 logger.debug("avail = {}", avail);
141 logger.debug("standby = {}", standby);
143 assertTrue("Admin state not unlocked after initialization", admin.equals(StateManagement.UNLOCKED));
144 assertTrue("Operational state not enabled after initialization", oper.equals(StateManagement.ENABLED));
147 stateManagementFeature.disableFailed();
148 } catch (Exception e) {
149 logger.error(e.getMessage());
150 assertTrue(e.getMessage(), false);
153 admin = stateManagementFeature.getAdminState();
154 oper = stateManagementFeature.getOpState();
155 avail = stateManagementFeature.getAvailStatus();
156 standby = stateManagementFeature.getStandbyStatus();
158 logger.debug("after disableFailed()");
159 logger.debug("admin = {}", admin);
160 logger.debug("oper = {}", oper);
161 logger.debug("avail = {}", avail);
162 logger.debug("standby = {}", standby);
164 assertTrue("Operational state not disabled after disableFailed()", oper.equals(StateManagement.DISABLED));
165 assertTrue("Availability status not failed after disableFailed()", avail.equals(StateManagement.FAILED));
169 stateManagementFeature.promote();
170 } catch (Exception e) {
171 logger.debug(e.getMessage());
174 admin = stateManagementFeature.getAdminState();
175 oper = stateManagementFeature.getOpState();
176 avail = stateManagementFeature.getAvailStatus();
177 standby = stateManagementFeature.getStandbyStatus();
179 logger.debug("after promote()");
180 logger.debug("admin = {}", admin);
181 logger.debug("oper = {}", oper);
182 logger.debug("avail = {}", avail);
183 logger.debug("standby = {}", standby);
185 assertTrue("Standby status not coldstandby after promote()", standby.equals(StateManagement.COLD_STANDBY));
187 /**************Repository Audit Test. **************/
188 logger.debug("\n\ntestStateManagementOperation: Repository Audit\n\n");
190 StateManagementProperties.initProperties(fsmProperties);
191 RepositoryAudit repositoryAudit = (RepositoryAudit) RepositoryAudit.getInstance();
192 repositoryAudit.invoke(fsmProperties);
194 //Should not throw an IOException in Linux Foundation env
196 } catch (IOException e) {
197 //Note: this catch is here because in a local environment mvn will not run in
198 //in the temp directory
199 logger.debug("testSubsytemTest RepositoryAudit IOException", e);
200 } catch (InterruptedException e) {
202 logger.debug("testSubsytemTest RepositoryAudit InterruptedException", e);
205 /*****************Db Audit Test. ***************/
206 logger.debug("\n\ntestStateManagementOperation: DB Audit\n\n");
209 DbAudit dbAudit = (DbAudit) DbAudit.getInstance();
210 dbAudit.invoke(fsmProperties);
213 } catch (Exception e) {
215 logger.debug("testSubsytemTest DbAudit exception", e);
218 /*************IntegrityMonitorRestManager Test. *************/
219 logger.debug("\n\ntestStateManagementOperation: IntegrityMonitorRestManager\n\n");
220 IntegrityMonitorRestManager integrityMonitorRestManager = new IntegrityMonitorRestManager();
222 Response response = integrityMonitorRestManager.test();
223 logger.debug("\n\nIntegrityMonitorRestManager response: " + response.toString());
225 assertTrue(response.toString().contains("status=500"));
228 logger.debug("\n\ntestStateManagementOperation: Exiting\n\n");
232 * This method initializes and cleans the DB so that PDP-D will be able to
233 * store fresh records in the DB.
235 public static void initializeDb() {
237 logger.debug("initializeDb: Entering");
239 Properties cleanProperties = new Properties();
240 cleanProperties.put(StateManagementProperties.DB_DRIVER,"org.h2.Driver");
241 cleanProperties.put(StateManagementProperties.DB_URL, "jdbc:h2:file:./sql/statemanagement");
242 cleanProperties.put(StateManagementProperties.DB_USER, "sa");
243 cleanProperties.put(StateManagementProperties.DB_PWD, "");
245 EntityManagerFactory emf = Persistence.createEntityManagerFactory("junitPU", cleanProperties);
247 EntityManager em = emf.createEntityManager();
248 // Start a transaction
249 EntityTransaction et = em.getTransaction();
254 em.createQuery("Delete from StateManagementEntity").executeUpdate();
255 em.createQuery("Delete from ForwardProgressEntity").executeUpdate();
256 em.createQuery("Delete from ResourceRegistrationEntity").executeUpdate();
258 // commit transaction
262 logger.debug("initializeDb: Exiting");