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;
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.StateManagement;
40 import org.onap.policy.drools.core.PolicySessionFeatureApi;
41 import org.onap.policy.drools.statemanagement.DbAudit;
42 import org.onap.policy.drools.statemanagement.IntegrityMonitorRestManager;
43 import org.onap.policy.drools.statemanagement.RepositoryAudit;
44 import org.onap.policy.drools.statemanagement.StateManagementFeatureApi;
45 import org.onap.policy.drools.statemanagement.StateManagementFeatureApiConstants;
46 import org.onap.policy.drools.statemanagement.StateManagementProperties;
47 import org.slf4j.Logger;
48 import org.slf4j.LoggerFactory;
50 public class StateManagementTest {
52 // get an instance of logger
53 private static Logger logger = LoggerFactory.getLogger(StateManagementTest.class);
55 private static EntityManagerFactory emf;
56 private static EntityManager em;
58 StateManagementFeatureApi stateManagementFeature;
62 * All you need to do here is create an instance of StateManagementFeature class. Then,
63 * check it initial state and the state after diableFailed() and promote()
65 * @throws Exception exception
68 public static void setUpClass() throws Exception {
70 logger.info("setUpClass: Entering");
72 String userDir = System.getProperty("user.dir");
73 logger.debug("setUpClass: userDir=" + userDir);
74 System.setProperty("com.sun.management.jmxremote.port", "9980");
75 System.setProperty("com.sun.management.jmxremote.authenticate", "false");
79 logger.info("setUpClass: Exiting");
83 public static void tearDownClass() throws Exception {
89 public void setUp() throws Exception {
94 public void tearDown() throws Exception {
99 * Verifies that StateManagementFeature starts and runs successfully.
104 public void testStateManagementOperation() throws Exception {
106 logger.debug("\n\ntestStateManagementOperation: Entering\n\n");
108 logger.debug("testStateManagementOperation: Reading StateManagementProperties");
110 String configDir = "src/test/resources";
112 DbAudit.setIsJunit(true);
114 Properties fsmProperties = new Properties();
115 fsmProperties.load(new FileInputStream(new File(
116 configDir + "/feature-state-management.properties")));
117 String thisPdpId = fsmProperties
118 .getProperty(StateManagementProperties.NODE_NAME);
120 StateManagementFeatureApi stateManagementFeature = null;
121 for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
122 ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
123 stateManagementFeature = feature;
124 logger.debug("testStateManagementOperation stateManagementFeature.getResourceName(): "
125 + stateManagementFeature.getResourceName());
128 if (stateManagementFeature == null) {
129 String msg = "testStateManagementOperation failed to initialize. "
130 + "Unable to get instance of StateManagementFeatureApi "
131 + "with resourceID: " + thisPdpId;
136 String admin = stateManagementFeature.getAdminState();
137 String oper = stateManagementFeature.getOpState();
138 String avail = stateManagementFeature.getAvailStatus();
139 String standby = stateManagementFeature.getStandbyStatus();
141 logger.debug("admin = {}", admin);
142 logger.debug("oper = {}", oper);
143 logger.debug("avail = {}", avail);
144 logger.debug("standby = {}", standby);
146 assertTrue("Admin state not unlocked after initialization", admin.equals(StateManagement.UNLOCKED));
147 assertTrue("Operational state not enabled after initialization", oper.equals(StateManagement.ENABLED));
150 stateManagementFeature.disableFailed();
151 } catch (Exception e) {
152 logger.error(e.getMessage());
153 assertTrue(e.getMessage(), false);
156 admin = stateManagementFeature.getAdminState();
157 oper = stateManagementFeature.getOpState();
158 avail = stateManagementFeature.getAvailStatus();
159 standby = stateManagementFeature.getStandbyStatus();
161 logger.debug("after disableFailed()");
162 logger.debug("admin = {}", admin);
163 logger.debug("oper = {}", oper);
164 logger.debug("avail = {}", avail);
165 logger.debug("standby = {}", standby);
167 assertTrue("Operational state not disabled after disableFailed()", oper.equals(StateManagement.DISABLED));
168 assertTrue("Availability status not failed after disableFailed()", avail.equals(StateManagement.FAILED));
172 stateManagementFeature.promote();
173 } catch (Exception e) {
174 logger.debug(e.getMessage());
177 admin = stateManagementFeature.getAdminState();
178 oper = stateManagementFeature.getOpState();
179 avail = stateManagementFeature.getAvailStatus();
180 standby = stateManagementFeature.getStandbyStatus();
182 logger.debug("after promote()");
183 logger.debug("admin = {}", admin);
184 logger.debug("oper = {}", oper);
185 logger.debug("avail = {}", avail);
186 logger.debug("standby = {}", standby);
188 assertTrue("Standby status not coldstandby after promote()", standby.equals(StateManagement.COLD_STANDBY));
190 /* *************Repository Audit Test. ************* */
191 logger.debug("\n\ntestStateManagementOperation: Repository Audit\n\n");
193 StateManagementProperties.initProperties(fsmProperties);
194 RepositoryAudit repositoryAudit = (RepositoryAudit) RepositoryAudit.getInstance();
195 repositoryAudit.invoke(fsmProperties);
197 //Should not throw an IOException in Linux Foundation env
199 } catch (IOException e) {
200 //Note: this catch is here because in a local environment mvn will not run in
201 //in the temp directory
202 logger.debug("testSubsytemTest RepositoryAudit IOException", e);
203 } catch (InterruptedException e) {
205 logger.debug("testSubsytemTest RepositoryAudit InterruptedException", e);
208 /* ****************Db Audit Test. ************** */
209 logger.debug("\n\ntestStateManagementOperation: DB Audit\n\n");
212 DbAudit dbAudit = (DbAudit) DbAudit.getInstance();
213 dbAudit.invoke(fsmProperties);
216 } catch (Exception e) {
218 logger.debug("testSubsytemTest DbAudit exception", e);
221 /* ************IntegrityMonitorRestManager Test. ************ */
222 logger.debug("\n\ntestStateManagementOperation: IntegrityMonitorRestManager\n\n");
223 IntegrityMonitorRestManager integrityMonitorRestManager = new IntegrityMonitorRestManager();
225 Response response = integrityMonitorRestManager.test();
226 logger.debug("\n\nIntegrityMonitorRestManager response: " + response.toString());
228 assertTrue(response.toString().contains("status=500"));
231 logger.debug("\n\ntestStateManagementOperation: Exiting\n\n");
235 * This method initializes and cleans the DB so that PDP-D will be able to
236 * store fresh records in the DB.
238 public static void initializeDb() {
240 logger.debug("initializeDb: Entering");
242 Properties cleanProperties = new Properties();
243 cleanProperties.put(StateManagementProperties.DB_DRIVER, "org.h2.Driver");
244 cleanProperties.put(StateManagementProperties.DB_URL, "jdbc:h2:mem:statemanagement");
245 cleanProperties.put(StateManagementProperties.DB_USER, "sa");
246 cleanProperties.put(StateManagementProperties.DB_PWD, "");
248 emf = Persistence.createEntityManagerFactory("junitPU", cleanProperties);
250 em = emf.createEntityManager();
251 // Start a transaction
252 EntityTransaction et = em.getTransaction();
257 em.createQuery("Delete from StateManagementEntity").executeUpdate();
258 em.createQuery("Delete from ForwardProgressEntity").executeUpdate();
259 em.createQuery("Delete from ResourceRegistrationEntity").executeUpdate();
261 // commit transaction
264 logger.debug("initializeDb: Exiting");