2 * ============LICENSE_START=======================================================
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
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.StateManagementFeatureApiConstants;
48 import org.onap.policy.drools.statemanagement.StateManagementProperties;
49 import org.slf4j.Logger;
50 import org.slf4j.LoggerFactory;
52 public class StateManagementTest {
54 // get an instance of logger
55 private static Logger logger = LoggerFactory.getLogger(StateManagementTest.class);
57 private static EntityManagerFactory emf;
58 private static EntityManager em;
60 StateManagementFeatureApi stateManagementFeature;
64 * All you need to do here is create an instance of StateManagementFeature class. Then,
65 * check it initial state and the state after diableFailed() and promote()
67 * @throws Exception exception
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 {
91 public void setUp() throws Exception {
96 public void tearDown() throws Exception {
101 * Verifies that StateManagementFeature starts and runs successfully.
106 public void testStateManagementOperation() throws Exception {
108 logger.debug("\n\ntestStateManagementOperation: Entering\n\n");
110 logger.debug("testStateManagementOperation: Reading StateManagementProperties");
112 String configDir = "src/test/resources";
114 DbAudit.setIsJunit(true);
116 Properties fsmProperties = new Properties();
117 fsmProperties.load(new FileInputStream(new File(
118 configDir + "/feature-state-management.properties")));
119 String thisPdpId = fsmProperties
120 .getProperty(StateManagementProperties.NODE_NAME);
122 StateManagementFeatureApi stateManagementFeature = null;
123 for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
124 ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
125 stateManagementFeature = feature;
126 logger.debug("testStateManagementOperation stateManagementFeature.getResourceName(): "
127 + stateManagementFeature.getResourceName());
130 if (stateManagementFeature == null) {
131 String msg = "testStateManagementOperation failed to initialize. "
132 + "Unable to get instance of StateManagementFeatureApi "
133 + "with resourceID: " + thisPdpId;
138 String admin = stateManagementFeature.getAdminState();
139 String oper = stateManagementFeature.getOpState();
140 String avail = stateManagementFeature.getAvailStatus();
141 String standby = stateManagementFeature.getStandbyStatus();
143 logger.debug("admin = {}", admin);
144 logger.debug("oper = {}", oper);
145 logger.debug("avail = {}", avail);
146 logger.debug("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();
153 } catch (Exception e) {
154 logger.error(e.getMessage());
155 assertTrue(e.getMessage(), false);
158 admin = stateManagementFeature.getAdminState();
159 oper = stateManagementFeature.getOpState();
160 avail = stateManagementFeature.getAvailStatus();
161 standby = stateManagementFeature.getStandbyStatus();
163 logger.debug("after disableFailed()");
164 logger.debug("admin = {}", admin);
165 logger.debug("oper = {}", oper);
166 logger.debug("avail = {}", avail);
167 logger.debug("standby = {}", standby);
169 assertTrue("Operational state not disabled after disableFailed()", oper.equals(StateManagement.DISABLED));
170 assertTrue("Availability status not failed after disableFailed()", avail.equals(StateManagement.FAILED));
174 stateManagementFeature.promote();
175 } catch (Exception e) {
176 logger.debug(e.getMessage());
179 admin = stateManagementFeature.getAdminState();
180 oper = stateManagementFeature.getOpState();
181 avail = stateManagementFeature.getAvailStatus();
182 standby = stateManagementFeature.getStandbyStatus();
184 logger.debug("after promote()");
185 logger.debug("admin = {}", admin);
186 logger.debug("oper = {}", oper);
187 logger.debug("avail = {}", avail);
188 logger.debug("standby = {}", standby);
190 assertTrue("Standby status not coldstandby after promote()", standby.equals(StateManagement.COLD_STANDBY));
192 /**************Repository Audit Test. **************/
193 logger.debug("\n\ntestStateManagementOperation: Repository Audit\n\n");
195 StateManagementProperties.initProperties(fsmProperties);
196 RepositoryAudit repositoryAudit = (RepositoryAudit) RepositoryAudit.getInstance();
197 repositoryAudit.invoke(fsmProperties);
199 //Should not throw an IOException in Linux Foundation env
201 } catch (IOException e) {
202 //Note: this catch is here because in a local environment mvn will not run in
203 //in the temp directory
204 logger.debug("testSubsytemTest RepositoryAudit IOException", e);
205 } catch (InterruptedException e) {
207 logger.debug("testSubsytemTest RepositoryAudit InterruptedException", e);
210 /*****************Db Audit Test. ***************/
211 logger.debug("\n\ntestStateManagementOperation: DB Audit\n\n");
214 DbAudit dbAudit = (DbAudit) DbAudit.getInstance();
215 dbAudit.invoke(fsmProperties);
218 } catch (Exception e) {
220 logger.debug("testSubsytemTest DbAudit exception", e);
223 /*************IntegrityMonitorRestManager Test. *************/
224 logger.debug("\n\ntestStateManagementOperation: IntegrityMonitorRestManager\n\n");
225 IntegrityMonitorRestManager integrityMonitorRestManager = new IntegrityMonitorRestManager();
227 Response response = integrityMonitorRestManager.test();
228 logger.debug("\n\nIntegrityMonitorRestManager response: " + response.toString());
230 assertTrue(response.toString().contains("status=500"));
233 logger.debug("\n\ntestStateManagementOperation: Exiting\n\n");
237 * This method initializes and cleans the DB so that PDP-D will be able to
238 * store fresh records in the DB.
240 public static void initializeDb() {
242 logger.debug("initializeDb: Entering");
244 Properties cleanProperties = new Properties();
245 cleanProperties.put(StateManagementProperties.DB_DRIVER,"org.h2.Driver");
246 cleanProperties.put(StateManagementProperties.DB_URL, "jdbc:h2:mem:statemanagement");
247 cleanProperties.put(StateManagementProperties.DB_USER, "sa");
248 cleanProperties.put(StateManagementProperties.DB_PWD, "");
250 emf = Persistence.createEntityManagerFactory("junitPU", cleanProperties);
252 em = emf.createEntityManager();
253 // Start a transaction
254 EntityTransaction et = em.getTransaction();
259 em.createQuery("Delete from StateManagementEntity").executeUpdate();
260 em.createQuery("Delete from ForwardProgressEntity").executeUpdate();
261 em.createQuery("Delete from ResourceRegistrationEntity").executeUpdate();
263 // commit transaction
266 logger.debug("initializeDb: Exiting");