ba7ce3eae097e3e8bfe0b3d51d25498d8f85d6cd
[policy/drools-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * policy-persistence
4  * ================================================================================
5  * Copyright (C) 2017 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         /*
57          * Sleep after each test to allow interrupt (shutdown) recovery.
58          */
59          
60         private long interruptRecoveryTime = 1500L;
61         
62         StateManagementFeatureAPI stateManagementFeature;
63         
64         /*
65          * All you need to do here is create an instance of StateManagementFeature class.  Then,
66          * check it initial state and the state after diableFailed() and promote()
67          */
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                                 
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.isJunit = 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 : StateManagementFeatureAPI.impl.getList())
123                 {
124                         ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
125                         stateManagementFeature = feature;
126                         logger.debug("testStateManagementOperation stateManagementFeature.getResourceName(): " + 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                 Thread.sleep(interruptRecoveryTime);
138                 
139                 String admin = stateManagementFeature.getAdminState();
140                 String oper = stateManagementFeature.getOpState();
141                 String avail = stateManagementFeature.getAvailStatus();
142                 String standby = stateManagementFeature.getStandbyStatus();
143                 
144                 logger.debug("admin = {}", admin);
145                 logger.debug("oper = {}", oper);
146                 logger.debug("avail = {}", avail);
147                 logger.debug("standby = {}", standby);
148                 
149                 assertTrue("Admin state not unlocked after initialization", admin.equals(StateManagement.UNLOCKED));
150                 assertTrue("Operational state not enabled after initialization", oper.equals(StateManagement.ENABLED));
151                 
152                 try{
153                         stateManagementFeature.disableFailed();
154                 }catch(Exception e){
155                         logger.error(e.getMessage());
156                         assertTrue(e.getMessage(), false);
157                 }
158                 
159                 Thread.sleep(interruptRecoveryTime);
160                 
161                 admin = stateManagementFeature.getAdminState();
162                 oper = stateManagementFeature.getOpState();
163                 avail = stateManagementFeature.getAvailStatus();
164                 standby = stateManagementFeature.getStandbyStatus();
165                 
166                 logger.debug("after disableFailed()");
167                 logger.debug("admin = {}", admin);
168                 logger.debug("oper = {}", oper);
169                 logger.debug("avail = {}", avail);
170                 logger.debug("standby = {}", standby);
171                 
172                 assertTrue("Operational state not disabled after disableFailed()", oper.equals(StateManagement.DISABLED));
173                 assertTrue("Availability status not failed after disableFailed()", avail.equals(StateManagement.FAILED));
174                 
175                 
176                 try{
177                         stateManagementFeature.promote();
178                 }catch(Exception e){
179                         logger.debug(e.getMessage());
180                 }
181                 
182                 Thread.sleep(interruptRecoveryTime);
183                 
184                 admin = stateManagementFeature.getAdminState();
185                 oper = stateManagementFeature.getOpState();
186                 avail = stateManagementFeature.getAvailStatus();
187                 standby = stateManagementFeature.getStandbyStatus();
188                 
189                 logger.debug("after promote()");
190                 logger.debug("admin = {}", admin);
191                 logger.debug("oper = {}", oper);
192                 logger.debug("avail = {}", avail);
193                 logger.debug("standby = {}", standby);
194
195                 assertTrue("Standby status not coldstandby after promote()", standby.equals(StateManagement.COLD_STANDBY));
196
197                 /**************Repository Audit Test**************/
198                 logger.debug("\n\ntestStateManagementOperation: Repository Audit\n\n");
199                 try{
200                         RepositoryAudit repositoryAudit = (RepositoryAudit) RepositoryAudit.getInstance();
201                         repositoryAudit.invoke(fsmProperties);
202                 
203                         //Should not throw an IOException in Linux Foundation env 
204                         assertTrue(true);
205                 }catch(IOException e){
206                         //Note: this catch is here because in a local environment mvn will not run in
207                         //in the temp directory
208                         logger.debug("testSubsytemTest RepositoryAudit IOException", e);
209                 }catch(InterruptedException e){
210                         assertTrue(false);
211                         logger.debug("testSubsytemTest RepositoryAudit InterruptedException", e);
212                 }
213
214                 /*****************Db Audit Test***************/
215                 logger.debug("\n\ntestStateManagementOperation: DB Audit\n\n");
216
217                 try{
218                         DbAudit dbAudit = (DbAudit) DbAudit.getInstance();
219                         dbAudit.invoke(fsmProperties);
220                 
221                         assertTrue(true);
222                 }catch(Exception e){
223                         assertTrue(false);
224                         logger.debug("testSubsytemTest DbAudit exception", e);
225                 }
226
227                 /*************IntegrityMonitorRestManager Test*************/
228                 logger.debug("\n\ntestStateManagementOperation: IntegrityMonitorRestManager\n\n");
229                 IntegrityMonitorRestManager integrityMonitorRestManager = new IntegrityMonitorRestManager();
230                 
231                 Response response = integrityMonitorRestManager.test();
232                 logger.debug("\n\nIntegrityMonitorRestManager response: " + response.toString());
233                 
234                 assertTrue(response.toString().contains("status=500"));
235
236                 //All done
237                 logger.debug("\n\ntestStateManagementOperation: Exiting\n\n");
238         }       
239         
240     /*
241      * This method initializes and cleans the DB so that PDP-D will be able to 
242      * store fresh records in the DB.
243      */
244      
245         public static void initializeDb(){
246                 
247                 logger.debug("initializeDb: Entering");
248                 
249         Properties cleanProperties = new Properties();
250         cleanProperties.put(StateManagementProperties.DB_DRIVER,"org.h2.Driver");
251         cleanProperties.put(StateManagementProperties.DB_URL, "jdbc:h2:file:./sql/statemanagement");
252         cleanProperties.put(StateManagementProperties.DB_USER, "sa");
253         cleanProperties.put(StateManagementProperties.DB_PWD, "");
254
255         EntityManagerFactory emf = Persistence.createEntityManagerFactory("junitPU", cleanProperties);
256                 
257                 EntityManager em = emf.createEntityManager();
258                 // Start a transaction
259                 EntityTransaction et = em.getTransaction();
260
261                 et.begin();
262
263                 // Clean up the DB
264                 em.createQuery("Delete from StateManagementEntity").executeUpdate();
265                 em.createQuery("Delete from ForwardProgressEntity").executeUpdate();
266                 em.createQuery("Delete from ResourceRegistrationEntity").executeUpdate();
267
268                 // commit transaction
269                 et.commit();
270                 em.close();
271                 
272                 logger.debug("initializeDb: Exiting");
273         }       
274 }