2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 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;
59 * All you need to do here is create an instance of StateManagementFeature class. Then,
60 * check it initial state and the state after diableFailed() and promote()
64 public static void setUpClass() throws Exception {
66 logger.info("setUpClass: Entering");
68 String userDir = System.getProperty("user.dir");
69 logger.debug("setUpClass: userDir=" + userDir);
70 System.setProperty("com.sun.management.jmxremote.port", "9980");
71 System.setProperty("com.sun.management.jmxremote.authenticate","false");
75 logger.info("setUpClass: Exiting");
79 public static void tearDownClass() throws Exception {
84 public void setUp() throws Exception {
89 public void tearDown() throws Exception {
94 * Verifies that StateManagementFeature starts and runs successfully.
99 public void testStateManagementOperation() throws Exception {
101 logger.debug("\n\ntestStateManagementOperation: Entering\n\n");
103 logger.debug("testStateManagementOperation: Reading StateManagementProperties");
105 String configDir = "src/test/resources";
107 DbAudit.setIsJunit(true);
109 Properties fsmProperties = new Properties();
110 fsmProperties.load(new FileInputStream(new File(
111 configDir + "/feature-state-management.properties")));
112 String thisPdpId = fsmProperties
113 .getProperty(StateManagementProperties.NODE_NAME);
115 StateManagementFeatureAPI stateManagementFeature = null;
116 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
118 ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
119 stateManagementFeature = feature;
120 logger.debug("testStateManagementOperation stateManagementFeature.getResourceName(): " + stateManagementFeature.getResourceName());
123 if(stateManagementFeature == null){
124 String msg = "testStateManagementOperation failed to initialize. "
125 + "Unable to get instance of StateManagementFeatureAPI "
126 + "with resourceID: " + thisPdpId;
131 String admin = stateManagementFeature.getAdminState();
132 String oper = stateManagementFeature.getOpState();
133 String avail = stateManagementFeature.getAvailStatus();
134 String standby = stateManagementFeature.getStandbyStatus();
136 logger.debug("admin = {}", admin);
137 logger.debug("oper = {}", oper);
138 logger.debug("avail = {}", avail);
139 logger.debug("standby = {}", standby);
141 assertTrue("Admin state not unlocked after initialization", admin.equals(StateManagement.UNLOCKED));
142 assertTrue("Operational state not enabled after initialization", oper.equals(StateManagement.ENABLED));
145 stateManagementFeature.disableFailed();
147 logger.error(e.getMessage());
148 assertTrue(e.getMessage(), false);
151 admin = stateManagementFeature.getAdminState();
152 oper = stateManagementFeature.getOpState();
153 avail = stateManagementFeature.getAvailStatus();
154 standby = stateManagementFeature.getStandbyStatus();
156 logger.debug("after disableFailed()");
157 logger.debug("admin = {}", admin);
158 logger.debug("oper = {}", oper);
159 logger.debug("avail = {}", avail);
160 logger.debug("standby = {}", standby);
162 assertTrue("Operational state not disabled after disableFailed()", oper.equals(StateManagement.DISABLED));
163 assertTrue("Availability status not failed after disableFailed()", avail.equals(StateManagement.FAILED));
167 stateManagementFeature.promote();
169 logger.debug(e.getMessage());
172 admin = stateManagementFeature.getAdminState();
173 oper = stateManagementFeature.getOpState();
174 avail = stateManagementFeature.getAvailStatus();
175 standby = stateManagementFeature.getStandbyStatus();
177 logger.debug("after promote()");
178 logger.debug("admin = {}", admin);
179 logger.debug("oper = {}", oper);
180 logger.debug("avail = {}", avail);
181 logger.debug("standby = {}", standby);
183 assertTrue("Standby status not coldstandby after promote()", standby.equals(StateManagement.COLD_STANDBY));
185 /**************Repository Audit Test**************/
186 logger.debug("\n\ntestStateManagementOperation: Repository Audit\n\n");
188 RepositoryAudit repositoryAudit = (RepositoryAudit) RepositoryAudit.getInstance();
189 repositoryAudit.invoke(fsmProperties);
191 //Should not throw an IOException in Linux Foundation env
193 }catch(IOException e){
194 //Note: this catch is here because in a local environment mvn will not run in
195 //in the temp directory
196 logger.debug("testSubsytemTest RepositoryAudit IOException", e);
197 }catch(InterruptedException e){
199 logger.debug("testSubsytemTest RepositoryAudit InterruptedException", e);
202 /*****************Db Audit Test***************/
203 logger.debug("\n\ntestStateManagementOperation: DB Audit\n\n");
206 DbAudit dbAudit = (DbAudit) DbAudit.getInstance();
207 dbAudit.invoke(fsmProperties);
212 logger.debug("testSubsytemTest DbAudit exception", e);
215 /*************IntegrityMonitorRestManager Test*************/
216 logger.debug("\n\ntestStateManagementOperation: IntegrityMonitorRestManager\n\n");
217 IntegrityMonitorRestManager integrityMonitorRestManager = new IntegrityMonitorRestManager();
219 Response response = integrityMonitorRestManager.test();
220 logger.debug("\n\nIntegrityMonitorRestManager response: " + response.toString());
222 assertTrue(response.toString().contains("status=500"));
225 logger.debug("\n\ntestStateManagementOperation: Exiting\n\n");
229 * This method initializes and cleans the DB so that PDP-D will be able to
230 * store fresh records in the DB.
233 public static void initializeDb(){
235 logger.debug("initializeDb: Entering");
237 Properties cleanProperties = new Properties();
238 cleanProperties.put(StateManagementProperties.DB_DRIVER,"org.h2.Driver");
239 cleanProperties.put(StateManagementProperties.DB_URL, "jdbc:h2:file:./sql/statemanagement");
240 cleanProperties.put(StateManagementProperties.DB_USER, "sa");
241 cleanProperties.put(StateManagementProperties.DB_PWD, "");
243 EntityManagerFactory emf = Persistence.createEntityManagerFactory("junitPU", cleanProperties);
245 EntityManager em = emf.createEntityManager();
246 // Start a transaction
247 EntityTransaction et = em.getTransaction();
252 em.createQuery("Delete from StateManagementEntity").executeUpdate();
253 em.createQuery("Delete from ForwardProgressEntity").executeUpdate();
254 em.createQuery("Delete from ResourceRegistrationEntity").executeUpdate();
256 // commit transaction
260 logger.debug("initializeDb: Exiting");