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.assertEquals;
24 import static org.junit.Assert.assertTrue;
27 import java.io.FileInputStream;
28 import java.util.Properties;
29 import javax.persistence.EntityManager;
30 import javax.persistence.EntityManagerFactory;
31 import javax.persistence.EntityTransaction;
32 import javax.persistence.Persistence;
33 import javax.ws.rs.core.Response;
34 import org.junit.After;
35 import org.junit.AfterClass;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.onap.policy.common.im.IntegrityMonitorException;
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;
51 public class StateManagementTest {
53 // get an instance of logger
54 private static Logger logger = LoggerFactory.getLogger(StateManagementTest.class);
56 private static EntityManagerFactory emf;
57 private static EntityManager em;
59 StateManagementFeatureApi stateManagementFeature;
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()
66 * @throws Exception exception
69 public static void setUpClass() throws Exception {
71 logger.info("setUpClass: Entering");
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");
80 logger.info("setUpClass: Exiting");
84 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.setIsJunit(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 : StateManagementFeatureApiConstants.getImpl().getList()) {
123 ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
124 stateManagementFeature = feature;
125 logger.debug("testStateManagementOperation stateManagementFeature.getResourceName(): "
126 + 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 String admin = stateManagementFeature.getAdminState();
138 String oper = stateManagementFeature.getOpState();
139 String avail = stateManagementFeature.getAvailStatus();
140 String standby = stateManagementFeature.getStandbyStatus();
142 logger.debug("admin = {}", admin);
143 logger.debug("oper = {}", oper);
144 logger.debug("avail = {}", avail);
145 logger.debug("standby = {}", standby);
147 assertEquals("Admin state not unlocked after initialization", StateManagement.UNLOCKED, admin);
148 assertEquals("Operational state not enabled after initialization", StateManagement.ENABLED, oper);
151 stateManagementFeature.disableFailed();
152 } catch (Exception e) {
153 logger.error(e.getMessage());
154 assertTrue(e.getMessage(), false);
157 admin = stateManagementFeature.getAdminState();
158 oper = stateManagementFeature.getOpState();
159 avail = stateManagementFeature.getAvailStatus();
160 standby = stateManagementFeature.getStandbyStatus();
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);
168 assertEquals("Operational state not disabled after disableFailed()", StateManagement.DISABLED, oper);
169 assertEquals("Availability status not failed after disableFailed()", StateManagement.FAILED, avail);
173 stateManagementFeature.promote();
174 } catch (Exception e) {
175 logger.debug(e.getMessage());
178 admin = stateManagementFeature.getAdminState();
179 oper = stateManagementFeature.getOpState();
180 avail = stateManagementFeature.getAvailStatus();
181 standby = stateManagementFeature.getStandbyStatus();
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);
189 assertEquals("Standby status not coldstandby after promote()", StateManagement.COLD_STANDBY, standby);
191 /* *************Repository Audit Test. ************* */
192 logger.debug("\n\ntestStateManagementOperation: Repository Audit\n\n");
194 StateManagementProperties.initProperties(fsmProperties);
195 RepositoryAudit repositoryAudit = (RepositoryAudit) RepositoryAudit.getInstance();
196 repositoryAudit.invoke(fsmProperties);
198 //Should not throw an IOException in Linux Foundation env
200 } catch (IntegrityMonitorException 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);
206 /* ****************Db Audit Test. ************** */
207 logger.debug("\n\ntestStateManagementOperation: DB Audit\n\n");
209 DbAudit dbAudit = (DbAudit) DbAudit.getInstance();
210 dbAudit.invoke(fsmProperties);
212 /* ************IntegrityMonitorRestManager Test. ************ */
213 logger.debug("\n\ntestStateManagementOperation: IntegrityMonitorRestManager\n\n");
214 IntegrityMonitorRestManager integrityMonitorRestManager = new IntegrityMonitorRestManager();
216 Response response = integrityMonitorRestManager.test();
217 logger.debug("\n\nIntegrityMonitorRestManager response: " + response.toString());
219 assertTrue(response.toString().contains("status=500"));
222 logger.debug("\n\ntestStateManagementOperation: Exiting\n\n");
226 * This method initializes and cleans the DB so that PDP-D will be able to
227 * store fresh records in the DB.
229 public static void initializeDb() {
231 logger.debug("initializeDb: Entering");
233 Properties cleanProperties = new Properties();
234 cleanProperties.put(StateManagementProperties.DB_DRIVER, "org.h2.Driver");
235 cleanProperties.put(StateManagementProperties.DB_URL, "jdbc:h2:mem:statemanagement");
236 cleanProperties.put(StateManagementProperties.DB_USER, "sa");
237 cleanProperties.put(StateManagementProperties.DB_PWD, "");
239 emf = Persistence.createEntityManagerFactory("junitPU", cleanProperties);
241 em = emf.createEntityManager();
242 // Start a transaction
243 EntityTransaction et = em.getTransaction();
248 em.createQuery("Delete from StateManagementEntity").executeUpdate();
249 em.createQuery("Delete from ForwardProgressEntity").executeUpdate();
250 em.createQuery("Delete from ResourceRegistrationEntity").executeUpdate();
252 // commit transaction
255 logger.debug("initializeDb: Exiting");