2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017-2019 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 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()
64 * @throws Exception exception
67 public static void setUpClass() throws Exception {
69 logger.info("setUpClass: Entering");
71 String userDir = System.getProperty("user.dir");
72 logger.debug("setUpClass: userDir=" + userDir);
73 System.setProperty("com.sun.management.jmxremote.port", "9980");
74 System.setProperty("com.sun.management.jmxremote.authenticate","false");
78 logger.info("setUpClass: Exiting");
82 public static void tearDownClass() throws Exception {
87 public void setUp() throws Exception {
92 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 DbAudit.setIsJunit(true);
112 Properties fsmProperties = new Properties();
113 fsmProperties.load(new FileInputStream(new File(
114 configDir + "/feature-state-management.properties")));
115 String thisPdpId = fsmProperties
116 .getProperty(StateManagementProperties.NODE_NAME);
118 StateManagementFeatureApi stateManagementFeature = null;
119 for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
120 ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
121 stateManagementFeature = feature;
122 logger.debug("testStateManagementOperation stateManagementFeature.getResourceName(): "
123 + stateManagementFeature.getResourceName());
126 if (stateManagementFeature == null) {
127 String msg = "testStateManagementOperation failed to initialize. "
128 + "Unable to get instance of StateManagementFeatureApi "
129 + "with resourceID: " + thisPdpId;
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 logger.debug("oper = {}", oper);
141 logger.debug("avail = {}", avail);
142 logger.debug("standby = {}", standby);
144 assertTrue("Admin state not unlocked after initialization", admin.equals(StateManagement.UNLOCKED));
145 assertTrue("Operational state not enabled after initialization", oper.equals(StateManagement.ENABLED));
148 stateManagementFeature.disableFailed();
149 } catch (Exception e) {
150 logger.error(e.getMessage());
151 assertTrue(e.getMessage(), false);
154 admin = stateManagementFeature.getAdminState();
155 oper = stateManagementFeature.getOpState();
156 avail = stateManagementFeature.getAvailStatus();
157 standby = stateManagementFeature.getStandbyStatus();
159 logger.debug("after disableFailed()");
160 logger.debug("admin = {}", admin);
161 logger.debug("oper = {}", oper);
162 logger.debug("avail = {}", avail);
163 logger.debug("standby = {}", standby);
165 assertTrue("Operational state not disabled after disableFailed()", oper.equals(StateManagement.DISABLED));
166 assertTrue("Availability status not failed after disableFailed()", avail.equals(StateManagement.FAILED));
170 stateManagementFeature.promote();
171 } catch (Exception e) {
172 logger.debug(e.getMessage());
175 admin = stateManagementFeature.getAdminState();
176 oper = stateManagementFeature.getOpState();
177 avail = stateManagementFeature.getAvailStatus();
178 standby = stateManagementFeature.getStandbyStatus();
180 logger.debug("after promote()");
181 logger.debug("admin = {}", admin);
182 logger.debug("oper = {}", oper);
183 logger.debug("avail = {}", avail);
184 logger.debug("standby = {}", standby);
186 assertTrue("Standby status not coldstandby after promote()", standby.equals(StateManagement.COLD_STANDBY));
188 /**************Repository Audit Test. **************/
189 logger.debug("\n\ntestStateManagementOperation: Repository Audit\n\n");
191 StateManagementProperties.initProperties(fsmProperties);
192 RepositoryAudit repositoryAudit = (RepositoryAudit) RepositoryAudit.getInstance();
193 repositoryAudit.invoke(fsmProperties);
195 //Should not throw an IOException in Linux Foundation env
197 } catch (IOException e) {
198 //Note: this catch is here because in a local environment mvn will not run in
199 //in the temp directory
200 logger.debug("testSubsytemTest RepositoryAudit IOException", e);
201 } catch (InterruptedException e) {
203 logger.debug("testSubsytemTest RepositoryAudit InterruptedException", e);
206 /*****************Db Audit Test. ***************/
207 logger.debug("\n\ntestStateManagementOperation: DB Audit\n\n");
210 DbAudit dbAudit = (DbAudit) DbAudit.getInstance();
211 dbAudit.invoke(fsmProperties);
214 } catch (Exception e) {
216 logger.debug("testSubsytemTest DbAudit exception", e);
219 /*************IntegrityMonitorRestManager Test. *************/
220 logger.debug("\n\ntestStateManagementOperation: IntegrityMonitorRestManager\n\n");
221 IntegrityMonitorRestManager integrityMonitorRestManager = new IntegrityMonitorRestManager();
223 Response response = integrityMonitorRestManager.test();
224 logger.debug("\n\nIntegrityMonitorRestManager response: " + response.toString());
226 assertTrue(response.toString().contains("status=500"));
229 logger.debug("\n\ntestStateManagementOperation: Exiting\n\n");
233 * This method initializes and cleans the DB so that PDP-D will be able to
234 * store fresh records in the DB.
236 public static void initializeDb() {
238 logger.debug("initializeDb: Entering");
240 Properties cleanProperties = new Properties();
241 cleanProperties.put(StateManagementProperties.DB_DRIVER,"org.h2.Driver");
242 cleanProperties.put(StateManagementProperties.DB_URL, "jdbc:h2:mem:statemanagement");
243 cleanProperties.put(StateManagementProperties.DB_USER, "sa");
244 cleanProperties.put(StateManagementProperties.DB_PWD, "");
246 EntityManagerFactory emf = Persistence.createEntityManagerFactory("junitPU", cleanProperties);
248 EntityManager em = emf.createEntityManager();
249 // Start a transaction
250 EntityTransaction et = em.getTransaction();
255 em.createQuery("Delete from StateManagementEntity").executeUpdate();
256 em.createQuery("Delete from ForwardProgressEntity").executeUpdate();
257 em.createQuery("Delete from ResourceRegistrationEntity").executeUpdate();
259 // commit transaction
263 logger.debug("initializeDb: Exiting");