578b62224b35e42ee3b6abcd771af1846ab58f89
[policy/drools-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy-persistence
4  * ================================================================================
5  * Copyright (C) 2017-2021 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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.drools.statemanagement.test;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25
26 import java.io.File;
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;
50
51 public class StateManagementTest {
52
53     // get an instance of logger
54     private static Logger  logger = LoggerFactory.getLogger(StateManagementTest.class);
55
56     private static EntityManagerFactory emf;
57     private static EntityManager em;
58
59     StateManagementFeatureApi stateManagementFeature;
60
61     /**
62      * Setup the class.
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()
65      *
66      * @throws Exception exception
67      */
68     @BeforeClass
69     public static void setUpClass() throws Exception {
70
71         logger.info("setUpClass: Entering");
72
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");
77
78         initializeDb();
79
80         logger.info("setUpClass: Exiting");
81     }
82
83     @AfterClass
84     public static void tearDownClass() throws Exception {
85         em.close();
86         emf.close();
87     }
88
89     @Before
90     public void setUp() throws Exception {
91
92     }
93
94     @After
95     public void tearDown() throws Exception {
96
97     }
98
99     /*
100      * Verifies that StateManagementFeature starts and runs successfully.
101      */
102
103     //@Ignore
104     @Test
105     public void testStateManagementOperation() throws Exception {
106
107         logger.debug("\n\ntestStateManagementOperation: Entering\n\n");
108
109         logger.debug("testStateManagementOperation: Reading StateManagementProperties");
110
111         String configDir = "src/test/resources";
112
113         DbAudit.setJunit(true);
114
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);
120
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());
127             break;
128         }
129         if (stateManagementFeature == null) {
130             String msg = "testStateManagementOperation failed to initialize.  "
131                     + "Unable to get instance of StateManagementFeatureApi "
132                     + "with resourceID: " + thisPdpId;
133             logger.error(msg);
134             logger.debug(msg);
135         }
136
137         String admin = stateManagementFeature.getAdminState();
138         String oper = stateManagementFeature.getOpState();
139         String avail = stateManagementFeature.getAvailStatus();
140         String standby = stateManagementFeature.getStandbyStatus();
141
142         logger.debug("admin = {}", admin);
143         logger.debug("oper = {}", oper);
144         logger.debug("avail = {}", avail);
145         logger.debug("standby = {}", standby);
146
147         assertEquals("Admin state not unlocked after initialization", StateManagement.UNLOCKED, admin);
148         assertEquals("Operational state not enabled after initialization", StateManagement.ENABLED, oper);
149
150         try {
151             stateManagementFeature.disableFailed();
152         } catch (Exception e) {
153             logger.error(e.getMessage());
154             assertTrue(e.getMessage(), false);
155         }
156
157         admin = stateManagementFeature.getAdminState();
158         oper = stateManagementFeature.getOpState();
159         avail = stateManagementFeature.getAvailStatus();
160         standby = stateManagementFeature.getStandbyStatus();
161
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);
167
168         assertEquals("Operational state not disabled after disableFailed()", StateManagement.DISABLED, oper);
169         assertEquals("Availability status not failed after disableFailed()", StateManagement.FAILED, avail);
170
171
172         try {
173             stateManagementFeature.promote();
174         } catch (Exception e) {
175             logger.debug(e.getMessage());
176         }
177
178         admin = stateManagementFeature.getAdminState();
179         oper = stateManagementFeature.getOpState();
180         avail = stateManagementFeature.getAvailStatus();
181         standby = stateManagementFeature.getStandbyStatus();
182
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);
188
189         assertEquals("Standby status not coldstandby after promote()", StateManagement.COLD_STANDBY, standby);
190
191         /* *************Repository Audit Test. ************* */
192         logger.debug("\n\ntestStateManagementOperation: Repository Audit\n\n");
193         try {
194             StateManagementProperties.initProperties(fsmProperties);
195             RepositoryAudit repositoryAudit = (RepositoryAudit) RepositoryAudit.getInstance();
196             repositoryAudit.invoke(fsmProperties);
197
198             //Should not throw an IOException in Linux Foundation env
199
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);
204         }
205
206         /* ****************Db Audit Test. ************** */
207         logger.debug("\n\ntestStateManagementOperation: DB Audit\n\n");
208
209         DbAudit dbAudit = (DbAudit) DbAudit.getInstance();
210         dbAudit.invoke(fsmProperties);
211
212         /* ************IntegrityMonitorRestManager Test. ************ */
213         logger.debug("\n\ntestStateManagementOperation: IntegrityMonitorRestManager\n\n");
214         IntegrityMonitorRestManager integrityMonitorRestManager = new IntegrityMonitorRestManager();
215
216         Response response = integrityMonitorRestManager.test();
217         logger.debug("\n\nIntegrityMonitorRestManager response: " + response.toString());
218
219         assertTrue(response.toString().contains("status=500"));
220
221         //All done
222         logger.debug("\n\ntestStateManagementOperation: Exiting\n\n");
223     }
224
225     /**
226      * This method initializes and cleans the DB so that PDP-D will be able to
227      * store fresh records in the DB.
228      */
229     public static void initializeDb() {
230
231         logger.debug("initializeDb: Entering");
232
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, "");
238
239         emf = Persistence.createEntityManagerFactory("junitPU", cleanProperties);
240
241         em = emf.createEntityManager();
242         // Start a transaction
243         EntityTransaction et = em.getTransaction();
244
245         et.begin();
246
247         // Clean up the DB
248         em.createQuery("Delete from StateManagementEntity").executeUpdate();
249         em.createQuery("Delete from ForwardProgressEntity").executeUpdate();
250         em.createQuery("Delete from ResourceRegistrationEntity").executeUpdate();
251
252         // commit transaction
253         et.commit();
254
255         logger.debug("initializeDb: Exiting");
256     }
257 }