summaryrefslogtreecommitdiffstats
path: root/integrity-monitor/src/test/java/org/onap/policy/common/im/test/AllSeemsWellTest.java
blob: 6fa8114d074139fd15ebcaeeeb21f56dcec4253a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
/*-
 * ============LICENSE_START=======================================================
 * Integrity Monitor
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.common.im.test;

import static org.junit.Assert.*;

import java.util.Map;
import java.util.Properties;

import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Persistence;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;
import org.onap.policy.common.im.IntegrityMonitor;
import org.onap.policy.common.im.IntegrityMonitorProperties;
import org.onap.policy.common.im.StateManagement;
import org.onap.policy.common.im.jpa.ForwardProgressEntity;
import org.onap.policy.common.im.jpa.StateManagementEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class AllSeemsWellTest {
	private static Logger logger = LoggerFactory.getLogger(AllSeemsWellTest.class);
	private static Properties myProp;
	private static EntityManagerFactory emf;
	private static EntityManager em;
	private static EntityTransaction et;
	private static String resourceName;
	private static Properties systemProps;
	
	private static final String DEFAULT_DB_DRIVER = "org.h2.Driver";
	private static final String DEFAULT_DB_URL = "jdbc:h2:file:./sql/imTest";
	private static final String DEFAULT_DB_USER = "sa";
	private static final String DEFAULT_DB_PWD = "";
	
	@BeforeClass
	public static void setUpClass() throws Exception {

	}

	@AfterClass
	public static void tearDownClass() throws Exception {
	}

	@Before
	public void setUp() throws Exception {
		IntegrityMonitor.setUnitTesting(true);
		
		myProp = new Properties();
		myProp.put(IntegrityMonitorProperties.DB_DRIVER, AllSeemsWellTest.DEFAULT_DB_DRIVER);
		myProp.put(IntegrityMonitorProperties.DB_URL, AllSeemsWellTest.DEFAULT_DB_URL);
		myProp.put(IntegrityMonitorProperties.DB_USER, AllSeemsWellTest.DEFAULT_DB_USER);
		myProp.put(IntegrityMonitorProperties.DB_PWD, AllSeemsWellTest.DEFAULT_DB_PWD);
		myProp.put(IntegrityMonitorProperties.SITE_NAME, "SiteA");
		myProp.put(IntegrityMonitorProperties.NODE_TYPE, "pap");
		
		// set JMX remote port in system properties
		systemProps = System.getProperties();
		systemProps.put("com.sun.management.jmxremote.port", "9797");
		
		resourceName = "siteA.pap1";
		
		//Create the data schema and entity manager factory
		emf = Persistence.createEntityManagerFactory("schemaPU", myProp);

		// Create an entity manager to use the DB
		em = emf.createEntityManager();

	}
	

	@After
	public void tearDown() throws Exception {
		// clear jmx remote port setting
		systemProps.remove("com.sun.management.jmxremote.port");
	}

	//Ignore
	@Test
	public void testAllSeemsWell() throws Exception {
		logger.debug("\nIntegrityMonitorTest: Entering testAllSeemsWell\n\n");
		
		// parameters are passed via a properties file
		myProp.put(IntegrityMonitorProperties.DEPENDENCY_GROUPS, "");
		myProp.put(IntegrityMonitorProperties.TEST_VIA_JMX, "false");
		myProp.put(IntegrityMonitorProperties.REFRESH_STATE_AUDIT_INTERVAL_MS, "-1");
		myProp.put(IntegrityMonitorProperties.STATE_AUDIT_INTERVAL_MS, "-1");
		myProp.put(IntegrityMonitorProperties.FAILED_COUNTER_THRESHOLD, "1");
		myProp.put(IntegrityMonitorProperties.FP_MONITOR_INTERVAL, "5");
		myProp.put(IntegrityMonitorProperties.TEST_TRANS_INTERVAL, "1");
		myProp.put(IntegrityMonitorProperties.WRITE_FPC_INTERVAL, "1");

		IntegrityMonitor.updateProperties(myProp);
		/*
		 *  The monitorInterval is 5 and the failedCounterThreshold is 1
		 *  A forward progress will be stale after 5 seconds.
		 */
		
		et = em.getTransaction();
		et.begin();

		// Make sure we start with the DB clean
		em.createQuery("DELETE FROM StateManagementEntity").executeUpdate();
		em.createQuery("DELETE FROM ResourceRegistrationEntity").executeUpdate();
		em.createQuery("DELETE FROM ForwardProgressEntity").executeUpdate();

		em.flush();
		et.commit();

		IntegrityMonitor.deleteInstance();
		
		IntegrityMonitor im = IntegrityMonitor.getInstance(resourceName, myProp);

		StateManagement sm = im.getStateManager();
		
		//Give it time to set the states in the DB
		Thread.sleep(15000);

		//Check the state
		logger.debug("\n\ntestAllSeemsWell starting im state \nAdminState = {}\nOpState() = {}\nAvailStatus = {}\nStandbyStatus = {}\n",
				sm.getAdminState(),
				sm.getOpState(),
				sm.getAvailStatus(),
				sm.getStandbyStatus());
		
		assertTrue(sm.getOpState().equals(StateManagement.ENABLED));
		
		//Indicate a failure
		im.allSeemsWell(this.getClass().getName(), IntegrityMonitorProperties.ALLNOTWELL, 
				"'AllSeemsWellTest - ALLNOTWELL'");
		
		//Wait for the state to change due to ALLNOTWELL
		Thread.sleep(15000);
		//Check the state
		logger.debug("\n\ntestAllSeemsWell after ALLNOTWELL: im state \nAdminState = {}\nOpState() = {}\nAvailStatus = {}\nStandbyStatus = {}\n",
				sm.getAdminState(),
				sm.getOpState(),
				sm.getAvailStatus(),
				sm.getStandbyStatus());
		
		//assertTrue(sm.getOpState().equals(StateManagement.DISABLED));
		
		Map<String, String> allNotWellMap = im.getAllNotWellMap();
		for(String key: allNotWellMap.keySet()){
			logger.debug("AllSeemsWellTest: allNotWellMap: key = {}  msg = {}", key, allNotWellMap.get(key));							
		}
		//assertTrue(allNotWellMap.size() == 1);

		Map<String,String> allSeemsWellMap = im.getAllSeemsWellMap();
		//assertTrue(allSeemsWellMap.isEmpty());
		
		//Return to normal
		im.allSeemsWell(this.getClass().getName(), IntegrityMonitorProperties.ALLSEEMSWELL, 
				"'AllSeemsWellTest - ALLSEEMSWELL'");
		
		//Wait for the state to change due to ALLNOTWELL
		Thread.sleep(15000);
		//Check the state
		logger.debug("\n\ntestAllSeemsWell after ALLSEEMSWELL: im state \nAdminState = {}\nOpState() = {}\nAvailStatus = {}\nStandbyStatus = {}\n",
				sm.getAdminState(),
				sm.getOpState(),
				sm.getAvailStatus(),
				sm.getStandbyStatus());
		
		//assertTrue(sm.getOpState().equals(StateManagement.ENABLED));
		
		allNotWellMap = im.getAllNotWellMap();
		assertTrue(allNotWellMap.isEmpty());
		
		allSeemsWellMap = im.getAllSeemsWellMap();
		assertTrue(allSeemsWellMap.size() == 1);
		for(String key: allSeemsWellMap.keySet()){
			logger.debug("AllSeemsWellTest: allSeemsWellMap: key = {}  msg = {}", key, allSeemsWellMap.get(key));							
		}
		
		//Check for null parameters
		try{
			im.allSeemsWell(null, IntegrityMonitorProperties.ALLSEEMSWELL, 
					"'AllSeemsWellTest - ALLSEEMSWELL'");
			assertTrue(false);
		}catch (IllegalArgumentException e) {
			assertTrue(true);
		}
		
		try{
			im.allSeemsWell("", IntegrityMonitorProperties.ALLSEEMSWELL, 
					"'AllSeemsWellTest - ALLSEEMSWELL'");
			assertTrue(false);
		}catch (IllegalArgumentException e) {
			assertTrue(true);
		}
		
		try{
			im.allSeemsWell(this.getClass().getName(), null, 
					"'AllSeemsWellTest - ALLSEEMSWELL'");
			assertTrue(false);
		}catch (IllegalArgumentException e) {
			assertTrue(true);
		}
		
		try{
			im.allSeemsWell(this.getClass().getName(), IntegrityMonitorProperties.ALLSEEMSWELL, 
					null);
			assertTrue(false);
		}catch (IllegalArgumentException e) {
			assertTrue(true);
		}
		
		try{
			im.allSeemsWell(this.getClass().getName(), IntegrityMonitorProperties.ALLSEEMSWELL, 
					"");
			assertTrue(false);
		}catch (IllegalArgumentException e) {
			assertTrue(true);
		}
		
		// undo settings
		myProp.put(IntegrityMonitorProperties.FAILED_COUNTER_THRESHOLD, Integer.toString(IntegrityMonitorProperties.DEFAULT_FAILED_COUNTER_THRESHOLD));
		myProp.put(IntegrityMonitorProperties.FP_MONITOR_INTERVAL, Integer.toString(IntegrityMonitorProperties.DEFAULT_MONITOR_INTERVAL));
		myProp.put(IntegrityMonitorProperties.FAILED_COUNTER_THRESHOLD, Integer.toString(IntegrityMonitorProperties.DEFAULT_FAILED_COUNTER_THRESHOLD));
		myProp.put(IntegrityMonitorProperties.FP_MONITOR_INTERVAL, Integer.toString(IntegrityMonitorProperties.DEFAULT_MONITOR_INTERVAL));
		myProp.put(IntegrityMonitorProperties.TEST_TRANS_INTERVAL, Integer.toString(IntegrityMonitorProperties.DEFAULT_TEST_INTERVAL));
		myProp.put(IntegrityMonitorProperties.WRITE_FPC_INTERVAL, Integer.toString(IntegrityMonitorProperties.DEFAULT_WRITE_FPC_INTERVAL));
		IntegrityMonitor.updateProperties(myProp);

		et = em.getTransaction();
		
		et.begin();
		// Make sure we leave the DB clean
		em.createQuery("DELETE FROM StateManagementEntity").executeUpdate();
		em.createQuery("DELETE FROM ResourceRegistrationEntity").executeUpdate();
		em.createQuery("DELETE FROM ForwardProgressEntity").executeUpdate();

		em.flush();
		et.commit();
		
		logger.debug("\n\ntestAllSeemsWell: Exit\n\n");
	}

}