summaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/dcae/vestest/TestConfigProcessor.java
blob: 2d6087e506ae65a8b55ce796f23654d5f08d5dd6 (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
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
/*-
 * ============LICENSE_START=======================================================
 * PROJECT
 * ================================================================================
 * 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.dcae.vestest;

import static org.junit.Assert.assertEquals;

import java.io.FileReader;
import java.io.IOException;
import org.json.JSONObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonObject;
import org.junit.Test;

import org.onap.dcae.commonFunction.ConfigProcessors;



public class TestConfigProcessor {
	public JSONObject getFileAsJsonObject()
	{
		JSONObject jsonObject = null;
		FileReader fr = null;
		final JsonParser parser = new JsonParser();
		String jsonfilepath="src/test/resources/event4xjson.txt";
		try{
			fr = new FileReader ( jsonfilepath );
			final JsonObject jo =  (JsonObject) parser.parse (fr);
			final String jsonText = jo.toString ();
			jsonObject = new JSONObject ( jsonText );
		}
		catch(Exception e){
			System.out.println("Exception while opening the file");
			e.printStackTrace();
		}
		finally {
			//close the file
			if (fr != null) {
	    		try {
	    				fr.close();
	    			} catch (IOException e) {
	    				System.out.println("Error closing file reader stream : " +e.toString());
	    			}
	    	}
		}
		return jsonObject;
	}
	@Test
	public void testAttrMap(){
		
		final JSONObject jsonObject = getFileAsJsonObject();
		final String functionRole = (jsonObject.getJSONObject("event")).getJSONObject("commonEventHeader").get("functionalRole").toString();
		System.out.println("event==" + jsonObject.toString());
		System.out.println("functionRole==" + functionRole);
		final JSONObject jsonArgs = new JSONObject ( "{\"field\": \"event.commonEventHeader.nfNamingCode\",\"oldField\": \"event.commonEventHeader.functionalRole\"}" );
		ConfigProcessors cpEvent = new ConfigProcessors(jsonObject);
	 	cpEvent.map(jsonArgs);
	 	final String responseData = cpEvent.getEventObjectVal("event.commonEventHeader.nfNamingCode").toString();
	 	System.out.println("modified event==" + jsonObject.toString());
	 	System.out.println("responseData==" + responseData); 
	 	assertEquals (functionRole, responseData); 
	}
	
	@Test
	public void testArrayMap(){
		
		final JSONObject jsonObject = getFileAsJsonObject();
		final String alarmAdditionalInformation = (jsonObject.getJSONObject("event")).getJSONObject("faultFields").get("alarmAdditionalInformation").toString();
		System.out.println("event==" + jsonObject.toString());
		System.out.println("alarmAdditionalInformation==" + alarmAdditionalInformation);
		final JSONObject jsonArgs = new JSONObject ( "{\"field\": \"event.faultFields.eventAdditionalInformation\",\"oldField\": \"event.faultFields.alarmAdditionalInformation\"}" );
		ConfigProcessors cpEvent = new ConfigProcessors(jsonObject);
	 	cpEvent.map(jsonArgs);
	 	final String responseData = cpEvent.getEventObjectVal("event.faultFields.eventAdditionalInformation").toString();
	 	System.out.println("modified event==" + jsonObject.toString());
	 	System.out.println("responseData==" + responseData); 
	 	assertEquals (alarmAdditionalInformation, responseData); 
	}
	@Test
	public void testJobjMaptoArray(){
		
		final JSONObject jsonObject = getFileAsJsonObject();
		//final String receiveDiscards = (((jsonObject.getJSONObject("event")).getJSONObject("faultFields")).get("errors")).get("receiveDiscards").toString();
		System.out.println("event==" + jsonObject.toString());
		//System.out.println("alarmAdditionalInformation==" + alarmAdditionalInformation);
		final JSONObject jsonArgs = new JSONObject ( "{\"field\": \"event.faultFields.vNicPerformanceArray[]\",\"oldField\": \"event.faultFields.errors\",\"attrMap\":{\"receiveDiscards\":\"receivedDiscardedPacketsAccumulated\"}}" );
		ConfigProcessors cpEvent = new ConfigProcessors(jsonObject);
		final String receiveDiscards = cpEvent.getEventObjectVal("event.faultFields.errors.receiveDiscards").toString();
		System.out.println("receiveDiscards==" + receiveDiscards);
	 	cpEvent.map(jsonArgs);
	 	final String responseData = cpEvent.getEventObjectVal("event.faultFields.vNicPerformanceArray[0].receivedDiscardedPacketsAccumulated").toString();
	 	System.out.println("modified event==" + jsonObject.toString());
	 	System.out.println("responseData==" + responseData); 
	 	assertEquals (receiveDiscards, responseData); 
	}
	@Test
	public void testAttrAdd(){
		
		final JSONObject jsonObject = getFileAsJsonObject();
		//final String functionRole = (jsonObject.getJSONObject("event")).getJSONObject("commonEventHeader").get("functionalRole").toString();
		System.out.println("event==" + jsonObject.toString());
		//System.out.println("functionRole==" + functionRole);
		final JSONObject jsonArgs = new JSONObject ( "{\"field\": \"event.faultFields.version\",\"value\": \"2.0\",\"fieldType\": \"number\"}" );
		ConfigProcessors cpEvent = new ConfigProcessors(jsonObject);
	 	cpEvent.addAttribute(jsonArgs);
	 	final String responseData = cpEvent.getEventObjectVal("event.faultFields.version").toString();
	 	System.out.println("modified event==" + jsonObject.toString());
	 	System.out.println("responseData==" + responseData); 
	 	assertEquals ("2.0", responseData); 
	}
	
	@Test
	public void testAttrUpdate(){
		
		final JSONObject jsonObject = getFileAsJsonObject();
		//final String functionRole = (jsonObject.getJSONObject("event")).getJSONObject("commonEventHeader").get("functionalRole").toString();
		System.out.println("event==" + jsonObject.toString());
		//System.out.println("functionRole==" + functionRole);
		final JSONObject jsonArgs = new JSONObject ( "{\"field\": \"event.faultFields.version\",\"value\": \"2.0\",\"fieldType\": \"number\"}" );
		ConfigProcessors cpEvent = new ConfigProcessors(jsonObject);
	 	cpEvent.updateAttribute(jsonArgs);
	 	final String responseData = cpEvent.getEventObjectVal("event.faultFields.version").toString();
	 	System.out.println("modified event==" + jsonObject.toString());
	 	System.out.println("responseData==" + responseData); 
	 	assertEquals ("2.0", responseData); 
	}
	
	@Test
	public void testAttrConcatenate(){
		
		final JSONObject jsonObject = getFileAsJsonObject();
		final String eventType = (jsonObject.getJSONObject("event")).getJSONObject("commonEventHeader").get("eventType").toString();
		final String domain = (jsonObject.getJSONObject("event")).getJSONObject("commonEventHeader").get("domain").toString();
		final String alarmCondition = (jsonObject.getJSONObject("event")).getJSONObject("faultFields").get("alarmCondition").toString();
		System.out.println("event==" + jsonObject.toString());
		final String eventName = domain + "_" + eventType + "_" + alarmCondition;
		System.out.println("eventName==" + eventName);
		final JSONObject jsonArgs = new JSONObject ( "{\"field\":\"event.commonEventHeader.eventName\",\"concatenate\": [\"$event.commonEventHeader.domain\",\"$event.commonEventHeader.eventType\",\"$event.faultFields.alarmCondition\"],\"delimiter\":\"_\"}");
		ConfigProcessors cpEvent = new ConfigProcessors(jsonObject);
	 	cpEvent.concatenateValue(jsonArgs);
	 	final String responseData = cpEvent.getEventObjectVal("event.commonEventHeader.eventName").toString();
	 	System.out.println("modified event==" + jsonObject.toString());
	 	System.out.println("responseData==" + responseData); 
	 	assertEquals (eventName, responseData); 
	}
	
	@Test
	public void testAttrSubtract(){
		
		final JSONObject jsonObject = getFileAsJsonObject();
		final String memoryConfigured = (jsonObject.getJSONObject("event")).getJSONObject("faultFields").get("memoryConfigured").toString();
		final String memoryUsed = (jsonObject.getJSONObject("event")).getJSONObject("faultFields").get("memoryUsed").toString();
		System.out.println("event==" + jsonObject.toString());
		System.out.println("memoryConfigured==" + memoryConfigured);
		System.out.println("memoryUsed==" + memoryUsed);
		final JSONObject jsonArgs = new JSONObject ( "{\"field\": \"event.faultFields.memoryFree\",\"subtract\": [\"$event.faultFields.memoryConfigured\",\"$event.faultFields.memoryUsed\"]}" );
		ConfigProcessors cpEvent = new ConfigProcessors(jsonObject);
	 	cpEvent.subtractValue(jsonArgs);
	 	final String responseData = cpEvent.getEventObjectVal("event.faultFields.memoryFree").toString();
	 	System.out.println("modified event==" + jsonObject.toString());
	 	System.out.println("responseData==" + responseData); 
	 	assertEquals ("1980.0", responseData); 
	}
	
	@Test
	public void testSetValue(){
		
		final JSONObject jsonObject = getFileAsJsonObject();
		System.out.println("event==" + jsonObject.toString());
		System.out.println("Testing SetValue");
		final JSONObject jsonArgs = new JSONObject ( "{\"field\": \"event.faultFields.version\",\"value\": \"2.0\",\"fieldType\": \"number\"}" );
		ConfigProcessors cpEvent = new ConfigProcessors(jsonObject);
	 	cpEvent.setValue(jsonArgs);
	 	final String responseData = cpEvent.getEventObjectVal("event.faultFields.version").toString();
	 	System.out.println("modified event==" + jsonObject.toString());
	 	System.out.println("responseData==" + responseData); 
	 	assertEquals ("2.0", responseData); 
	}
	
	@Test
	public void testSetEventObjectVal(){
		
		final JSONObject jsonObject = getFileAsJsonObject();
		System.out.println("event==" + jsonObject.toString());
		System.out.println("Testing SetEventObjectVal");
		//final JSONObject jsonArgs = new JSONObject ( "{\"field\": \"event.faultFields.version\",\"value\": \"2.0\",\"fieldType\": \"number\"}" );
		ConfigProcessors cpEvent = new ConfigProcessors(jsonObject);
	 	cpEvent.setEventObjectVal("event.faultFields.version", "2.0", "number");
	 	final String responseData = cpEvent.getEventObjectVal("event.faultFields.version").toString();
	 	System.out.println("modified event==" + jsonObject.toString());
	 	System.out.println("responseData==" + responseData); 
	 	assertEquals ("2.0", responseData); 
	}
	
	@Test
	public void testGetValue(){
		
		final JSONObject jsonObject = getFileAsJsonObject();
		System.out.println("event==" + jsonObject.toString());
		System.out.println("Testing GetValue");
		final JSONObject jsonArgs = new JSONObject ( "{\"field\": \"event.faultFields.eventSeverity\"}" );
		ConfigProcessors cpEvent = new ConfigProcessors(jsonObject);
	 	cpEvent.getValue(jsonArgs);
	 	final String responseData = cpEvent.getEventObjectVal("event.faultFields.eventSeverity").toString();
	 	System.out.println("modified event==" + jsonObject.toString());
	 	System.out.println("responseData==" + responseData); 
	 	assertEquals ("CRITICAL", responseData); 
	}
	
	@Test
	public void testGetEventObjectVal(){
		
		final JSONObject jsonObject = getFileAsJsonObject();
		System.out.println("event==" + jsonObject.toString());
		System.out.println("Testing GetEventObjectVal");
		//final JSONObject jsonArgs = new JSONObject ( "{\"field\": \"event.faultFields.eventSeverity\"}" );
		ConfigProcessors cpEvent = new ConfigProcessors(jsonObject);
	 	cpEvent.getEventObjectVal("event.faultFields.eventSeverity");
	 	final String responseData = cpEvent.getEventObjectVal("event.faultFields.eventSeverity").toString();
	 	System.out.println("modified event==" + jsonObject.toString());
	 	System.out.println("responseData==" + responseData); 
	 	assertEquals ("CRITICAL", responseData); 
	}
	
	@Test
	public void testRemoveAttribute(){
		
		final JSONObject jsonObject = getFileAsJsonObject();
		System.out.println("event==" + jsonObject.toString());
		System.out.println("Testing removeAttribute");
		final JSONObject jsonArgs = new JSONObject ( "{\"field\": \"event.faultFields.memoryUsed\"}" );
		ConfigProcessors cpEvent = new ConfigProcessors(jsonObject);
	 	cpEvent.removeAttribute(jsonArgs);
	 	final String responseData = cpEvent.getEventObjectVal("event.faultFields.memoryUsed").toString();
	 	System.out.println("modified event==" + jsonObject.toString());
	 	System.out.println("responseData==" + responseData); 
	 	assertEquals ("ObjectNotFound", responseData); 
	}
	
	@Test
	public void testIsFilterMet(){
		
		final JSONObject jsonObject = getFileAsJsonObject();
		System.out.println("event==" + jsonObject.toString());
		System.out.println("Testing isFilterMet");
		final JSONObject jsonArgs = new JSONObject ( "{\"event.faultFields.eventSeverity\":\"CRITICAL\"}" );
		ConfigProcessors cpEvent = new ConfigProcessors(jsonObject);
	 	
	 	final boolean response = cpEvent.isFilterMet(jsonArgs);
	 	String responseData = "CRITICAL";
	 	if (response == false)
	 		responseData = "notCRITICAL";
	 	
	 	System.out.println("responseData==" + responseData); 
	 	assertEquals ("CRITICAL", responseData); 
	}
	
	@Test
	public void testSuppressEvent(){
		
		final JSONObject jsonObject = getFileAsJsonObject();
		System.out.println("event==" + jsonObject.toString());
		System.out.println("Testing SuppressEvent");
		final JSONObject jsonArgs = new JSONObject ( "{\"event.faultFields.eventSeverity\":\"CRITICAL\"}" );
		ConfigProcessors cpEvent = new ConfigProcessors(jsonObject);
	 	
	 	cpEvent.suppressEvent(jsonArgs);
	 	String responseData = cpEvent.getEventObjectVal("suppressEvent").toString();
	 	
	 	System.out.println("responseData==" + responseData); 
	 	assertEquals ("true", responseData); 
	}
}