f6c42b5f3ed0774774b5f443237e5a0c5fbcaced
[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
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;
35
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;
51
52 public class StateManagementTest {
53
54     // get an instance of logger
55     private static Logger  logger = LoggerFactory.getLogger(StateManagementTest.class);
56
57     private static EntityManagerFactory emf;
58     private static EntityManager em;
59
60     StateManagementFeatureApi stateManagementFeature;
61
62     /**
63      * Setup the class.
64      * All you need to do here is create an instance of StateManagementFeature class.  Then,
65      * check it initial state and the state after diableFailed() and promote()
66      *
67      * @throws Exception exception
68      */
69     @BeforeClass
70     public static void setUpClass() throws Exception {
71
72         logger.info("setUpClass: Entering");
73
74         String userDir = System.getProperty("user.dir");
75         logger.debug("setUpClass: userDir=" + userDir);
76         System.setProperty("com.sun.management.jmxremote.port", "9980");
77         System.setProperty("com.sun.management.jmxremote.authenticate","false");
78
79         initializeDb();
80
81         logger.info("setUpClass: Exiting");
82     }
83
84     @AfterClass
85     public static void tearDownClass() throws Exception {
86         em.close();
87         emf.close();
88     }
89
90     @Before
91     public void setUp() throws Exception {
92
93     }
94
95     @After
96     public void tearDown() throws Exception {
97
98     }
99
100     /*
101      * Verifies that StateManagementFeature starts and runs successfully.
102      */
103
104     //@Ignore
105     @Test
106     public void testStateManagementOperation() throws Exception {
107
108         logger.debug("\n\ntestStateManagementOperation: Entering\n\n");
109
110         logger.debug("testStateManagementOperation: Reading StateManagementProperties");
111
112         String configDir = "src/test/resources";
113
114         DbAudit.setIsJunit(true);
115
116         Properties fsmProperties = new Properties();
117         fsmProperties.load(new FileInputStream(new File(
118                 configDir + "/feature-state-management.properties")));
119         String thisPdpId = fsmProperties
120                 .getProperty(StateManagementProperties.NODE_NAME);
121
122         StateManagementFeatureApi stateManagementFeature = null;
123         for (StateManagementFeatureApi feature : StateManagementFeatureApiConstants.getImpl().getList()) {
124             ((PolicySessionFeatureApi) feature).globalInit(null, configDir);
125             stateManagementFeature = feature;
126             logger.debug("testStateManagementOperation stateManagementFeature.getResourceName(): "
127                 + stateManagementFeature.getResourceName());
128             break;
129         }
130         if (stateManagementFeature == null) {
131             String msg = "testStateManagementOperation failed to initialize.  "
132                     + "Unable to get instance of StateManagementFeatureApi "
133                     + "with resourceID: " + thisPdpId;
134             logger.error(msg);
135             logger.debug(msg);
136         }
137
138         String admin = stateManagementFeature.getAdminState();
139         String oper = stateManagementFeature.getOpState();
140         String avail = stateManagementFeature.getAvailStatus();
141         String standby = stateManagementFeature.getStandbyStatus();
142
143         logger.debug("admin = {}", admin);
144         logger.debug("oper = {}", oper);
145         logger.debug("avail = {}", avail);
146         logger.debug("standby = {}", standby);
147
148         assertTrue("Admin state not unlocked after initialization", admin.equals(StateManagement.UNLOCKED));
149         assertTrue("Operational state not enabled after initialization", oper.equals(StateManagement.ENABLED));
150
151         try {
152             stateManagementFeature.disableFailed();
153         } catch (Exception e) {
154             logger.error(e.getMessage());
155             assertTrue(e.getMessage(), false);
156         }
157
158         admin = stateManagementFeature.getAdminState();
159         oper = stateManagementFeature.getOpState();
160         avail = stateManagementFeature.getAvailStatus();
161         standby = stateManagementFeature.getStandbyStatus();
162
163         logger.debug("after disableFailed()");
164         logger.debug("admin = {}", admin);
165         logger.debug("oper = {}", oper);
166         logger.debug("avail = {}", avail);
167         logger.debug("standby = {}", standby);
168
169         assertTrue("Operational state not disabled after disableFailed()", oper.equals(StateManagement.DISABLED));
170         assertTrue("Availability status not failed after disableFailed()", avail.equals(StateManagement.FAILED));
171
172
173         try {
174             stateManagementFeature.promote();
175         } catch (Exception e) {
176             logger.debug(e.getMessage());
177         }
178
179         admin = stateManagementFeature.getAdminState();
180         oper = stateManagementFeature.getOpState();
181         avail = stateManagementFeature.getAvailStatus();
182         standby = stateManagementFeature.getStandbyStatus();
183
184         logger.debug("after promote()");
185         logger.debug("admin = {}", admin);
186         logger.debug("oper = {}", oper);
187         logger.debug("avail = {}", avail);
188         logger.debug("standby = {}", standby);
189
190         assertTrue("Standby status not coldstandby after promote()", standby.equals(StateManagement.COLD_STANDBY));
191
192         /**************Repository Audit Test. **************/
193         logger.debug("\n\ntestStateManagementOperation: Repository Audit\n\n");
194         try {
195             StateManagementProperties.initProperties(fsmProperties);
196             RepositoryAudit repositoryAudit = (RepositoryAudit) RepositoryAudit.getInstance();
197             repositoryAudit.invoke(fsmProperties);
198
199             //Should not throw an IOException in Linux Foundation env
200             assertTrue(true);
201         } catch (IOException e) {
202             //Note: this catch is here because in a local environment mvn will not run in
203             //in the temp directory
204             logger.debug("testSubsytemTest RepositoryAudit IOException", e);
205         } catch (InterruptedException e) {
206             assertTrue(false);
207             logger.debug("testSubsytemTest RepositoryAudit InterruptedException", e);
208         }
209
210         /*****************Db Audit Test. ***************/
211         logger.debug("\n\ntestStateManagementOperation: DB Audit\n\n");
212
213         try {
214             DbAudit dbAudit = (DbAudit) DbAudit.getInstance();
215             dbAudit.invoke(fsmProperties);
216
217             assertTrue(true);
218         } catch (Exception e) {
219             assertTrue(false);
220             logger.debug("testSubsytemTest DbAudit exception", e);
221         }
222
223         /*************IntegrityMonitorRestManager Test. *************/
224         logger.debug("\n\ntestStateManagementOperation: IntegrityMonitorRestManager\n\n");
225         IntegrityMonitorRestManager integrityMonitorRestManager = new IntegrityMonitorRestManager();
226
227         Response response = integrityMonitorRestManager.test();
228         logger.debug("\n\nIntegrityMonitorRestManager response: " + response.toString());
229
230         assertTrue(response.toString().contains("status=500"));
231
232         //All done
233         logger.debug("\n\ntestStateManagementOperation: Exiting\n\n");
234     }
235
236     /**
237      * This method initializes and cleans the DB so that PDP-D will be able to
238      * store fresh records in the DB.
239      */
240     public static void initializeDb() {
241
242         logger.debug("initializeDb: Entering");
243
244         Properties cleanProperties = new Properties();
245         cleanProperties.put(StateManagementProperties.DB_DRIVER,"org.h2.Driver");
246         cleanProperties.put(StateManagementProperties.DB_URL, "jdbc:h2:mem:statemanagement");
247         cleanProperties.put(StateManagementProperties.DB_USER, "sa");
248         cleanProperties.put(StateManagementProperties.DB_PWD, "");
249
250         emf = Persistence.createEntityManagerFactory("junitPU", cleanProperties);
251
252         em = emf.createEntityManager();
253         // Start a transaction
254         EntityTransaction et = em.getTransaction();
255
256         et.begin();
257
258         // Clean up the DB
259         em.createQuery("Delete from StateManagementEntity").executeUpdate();
260         em.createQuery("Delete from ForwardProgressEntity").executeUpdate();
261         em.createQuery("Delete from ResourceRegistrationEntity").executeUpdate();
262
263         // commit transaction
264         et.commit();
265
266         logger.debug("initializeDb: Exiting");
267     }
268 }