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