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