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