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