e458dceaff23d97b9e61cda3772f3b8aea3cfdf1
[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.util.Properties;
28
29 import javax.persistence.EntityManager;
30 import javax.persistence.EntityManagerFactory;
31 import javax.persistence.EntityTransaction;
32 import javax.persistence.Persistence;
33
34 import org.junit.After;
35 import org.junit.AfterClass;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Ignore;
39 import org.junit.Test;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.onap.policy.common.im.StateManagement;
43 import org.onap.policy.drools.core.PolicySessionFeatureAPI;
44 import org.onap.policy.drools.statemanagement.StateManagementFeatureAPI;
45 import org.onap.policy.drools.statemanagement.StateManagementProperties;
46
47 public class StateManagementTest {
48                 
49         // get an instance of logger 
50         private static Logger  logger = LoggerFactory.getLogger(StateManagementTest.class);     
51         
52         /*
53          * Sleep 5 seconds after each test to allow interrupt (shutdown) recovery.
54          */
55          
56         private long interruptRecoveryTime = 1000;
57         
58         StateManagementFeatureAPI stateManagementFeature;
59         
60         /*
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         
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         /*
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                 Properties fsmProperties = new Properties();
111                 fsmProperties.load(new FileInputStream(new File(
112                                 configDir + "/feature-state-management.properties")));
113                 String thisPdpId = fsmProperties
114                                 .getProperty(StateManagementProperties.NODE_NAME);
115
116                 StateManagementFeatureAPI stateManagementFeature = null;
117                 for (StateManagementFeatureAPI feature : StateManagementFeatureAPI.impl.getList())
118                 {
119                         ((PolicySessionFeatureAPI) feature).globalInit(null, configDir);
120                         stateManagementFeature = feature;
121                         logger.debug("testStateManagementOperation stateManagementFeature.getResourceName(): " + stateManagementFeature.getResourceName());
122                         break;
123                 }
124                 if(stateManagementFeature == null){
125                         String msg = "testStateManagementOperation failed to initialize.  "
126                                         + "Unable to get instance of StateManagementFeatureAPI "
127                                         + "with resourceID: " + thisPdpId;
128                         logger.error(msg);
129                         logger.debug(msg);
130                 }
131                 
132                 Thread.sleep(interruptRecoveryTime);
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                 System.out.println("admin = " + admin);
141                 logger.debug("oper = {}", oper);
142                 System.out.println("oper = " + oper);
143                 logger.debug("avail = {}", avail);
144                 System.out.println("avail = " + avail);
145                 logger.debug("standby = {}", standby);
146                 System.out.println("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                         System.out.println(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                 System.out.println("after disableFailed()");
168                 logger.debug("admin = {}", admin);
169                 System.out.println("admin = " + admin);
170                 logger.debug("oper = {}", oper);
171                 System.out.println("oper = " + oper);
172                 logger.debug("avail = {}", avail);
173                 System.out.println("avail = " + avail);
174                 logger.debug("standby = {}", standby);
175                 System.out.println("standby = " + standby);
176                 
177                 assertTrue("Operational state not disabled after disableFailed()", oper.equals(StateManagement.DISABLED));
178                 assertTrue("Availability status not failed after disableFailed()", avail.equals(StateManagement.FAILED));
179                 
180                 
181                 try{
182                         stateManagementFeature.promote();
183                 }catch(Exception e){
184                         logger.debug(e.getMessage());
185                         System.out.println(e.getMessage());
186                 }
187                 
188                 Thread.sleep(interruptRecoveryTime);
189                 
190                 admin = stateManagementFeature.getAdminState();
191                 oper = stateManagementFeature.getOpState();
192                 avail = stateManagementFeature.getAvailStatus();
193                 standby = stateManagementFeature.getStandbyStatus();
194                 
195                 logger.debug("after promote()");
196                 System.out.println("after promote()");
197                 logger.debug("admin = {}", admin);
198                 System.out.println("admin = " + admin);
199                 logger.debug("oper = {}", oper);
200                 System.out.println("oper = " + oper);
201                 logger.debug("avail = {}", avail);
202                 System.out.println("avail = " + avail);
203                 logger.debug("standby = {}", standby);
204                 System.out.println("standby = " + standby);
205
206                 assertTrue("Standby status not coldstandby after promote()", standby.equals(StateManagement.COLD_STANDBY));
207                                 
208                 logger.debug("\n\ntestStateManagementOperation: Exiting\n\n");
209         }       
210         
211     /*
212      * This method initializes and cleans the DB so that PDP-D will be able to 
213      * store fresh records in the DB.
214      */
215      
216         public static void initializeDb(){
217                 
218                 logger.debug("initializeDb: Entering");
219                 
220         Properties cleanProperties = new Properties();
221         cleanProperties.put(StateManagementProperties.DB_DRIVER,"org.h2.Driver");
222         cleanProperties.put(StateManagementProperties.DB_URL, "jdbc:h2:file:./sql/statemanagement");
223         cleanProperties.put(StateManagementProperties.DB_USER, "sa");
224         cleanProperties.put(StateManagementProperties.DB_PWD, "");
225
226         EntityManagerFactory emf = Persistence.createEntityManagerFactory("junitPU", cleanProperties);
227                 
228                 EntityManager em = emf.createEntityManager();
229                 // Start a transaction
230                 EntityTransaction et = em.getTransaction();
231
232                 et.begin();
233
234                 // Clean up the DB
235                 em.createQuery("Delete from StateManagementEntity").executeUpdate();
236                 em.createQuery("Delete from ForwardProgressEntity").executeUpdate();
237                 em.createQuery("Delete from ResourceRegistrationEntity").executeUpdate();
238
239                 // commit transaction
240                 et.commit();
241                 em.close();
242                 
243                 logger.debug("initializeDb: Exiting");
244         }       
245 }