aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/org/onap/aai/audit/AuditGraphson2SqlTest.java
blob: 2bc6250fafc7332dc224f2a3bfc4baf04418bb45 (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
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017-2018 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.aai.audit;

import com.google.gson.JsonObject;
import org.junit.Before;
import org.junit.FixMethodOrder;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runners.MethodSorters;
import org.mockito.Mock;

import static org.mockito.Matchers.*;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
import org.onap.aai.AAISetup;
import org.onap.aai.edges.EdgeIngestor;
import org.onap.aai.edges.exceptions.EdgeRuleNotFoundException;
import org.onap.aai.exceptions.AAIException;
import org.onap.aai.rest.client.ApertureService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.TestPropertySource;

import java.io.IOException;
import java.util.HashMap;

import static org.junit.Assert.*;

public class AuditGraphson2SqlTest extends AAISetup {

	
	private AuditGraphson2Sql auditG2S;
	
	@Autowired 
	private EdgeIngestor ei;

	@Mock
	private ApertureService apertureServiceMock;

	@Before
	public void setUp() {
		
		auditG2S = new AuditGraphson2Sql(ei, schemaVersions, loaderFactory, apertureServiceMock);
		    
	}

    @Test
    public void testCompareGood() throws IOException, EdgeRuleNotFoundException {
    	String outDir = "logs/data/audit";
    	Boolean resultOk = true;
    	HashMap <String,Integer> gHash = new HashMap <String,Integer> ();
    	gHash.put("tbl2", 5);
    	gHash.put("tbl-x", 6);
    	gHash.put("edge__tbly__tblX", 7);
    	gHash.put("edge__tblZ__tblX", 8);
    	gHash.put("tbl-only-in-g", 88);
    	
    	HashMap <String,Integer> sEdgeHash = new HashMap <String,Integer> ();
    	sEdgeHash.put("edge__tbly__tblX", 7);
    	sEdgeHash.put("edge__tblZ__tblX", 8);
    	
    	HashMap <String,Integer> sNodeHash = new HashMap <String,Integer> ();
    	sNodeHash.put("tbl2", 5);
    	sNodeHash.put("tbl-x", 6);
    	sNodeHash.put("tbl-only-in-sql", 89);
    	
    	String titleInfo = "Comparing data from GraphSon file: " +
        		"fileXYZ.202001223344, and Aperture data using timeStamp = [987654321]"; 
        	
    	try {
    		auditG2S.compareResults(gHash,sNodeHash,sEdgeHash,outDir,titleInfo);
    	}
    	catch ( Exception e ) {
    		System.out.println("ERROR - got this exception: " + e.getMessage());
    		resultOk = false;
    	}   	
    	assertTrue(resultOk);  	
    }
	
    @Test
    public void testCompareResMissingRecords() throws IOException, EdgeRuleNotFoundException {
    	
    	String outDir = "logs/data/audit";
    	Boolean resultOk = true;
    	HashMap <String,Integer> gHash = new HashMap <String,Integer> ();
    	gHash.put("tbl2", 5);
    	gHash.put("tbl-x", 6);
    	gHash.put("edge__tblZ__tblX", 7);
    	gHash.put("edge__tblZ__tblX", 8);
    	gHash.put("tbl-only-in-g", 88);
    	HashMap <String,Integer> sNodeHash = new HashMap <String,Integer> ();
    	HashMap <String,Integer> sEdgeHash = new HashMap <String,Integer> ();
    	
    	String titleInfo = "Comparing data from GraphSon file: " +
        		"fileXYZ.202001223344, and Aperture data using timeStamp = [987654321]"; 
        		  	
    	try {
    		auditG2S.compareResults(gHash,sNodeHash,sEdgeHash,outDir,titleInfo);
    	}
    	catch ( Exception e ) {
    		System.out.println("ERROR - got this exception: " + e.getMessage());
    		resultOk = false;
    	}
    	
    	assertTrue(resultOk);   	
    }
    
    @Ignore
    @Test
    public void testGetDateTimeStamp() {
    	long dts = 0;
    	String fName = "xxxxx.yyyy.202003220000";
    	try {
    		dts = auditG2S.getTimestampFromFileName(fName); 
    	} catch (Exception e) {
    		System.out.println(" threw Exception e = " + e.getMessage());
    	}

    	assertEquals(1577595600000L, dts);
    }
    
    @Test
    public void testTimePieceGood() {
    	String dtPiece = "";
    	String fName = "xxxxx.yyyy.222233445566";
    	try {
    		dtPiece = auditG2S.getDateTimePieceOfFileName(fName); 
    	} catch (Exception e) {
    		System.out.println(" threw Exception e = " + e.getMessage());
    	}
    	assertEquals( "222233445566", dtPiece);
    }
    
    @Test
    public void testTimePieceGoodStill() {
    	String dtPiece = "";
    	String fName = "x.222233445566";
    	try {
    		dtPiece = auditG2S.getDateTimePieceOfFileName(fName); 
    	} catch (Exception e) {
    		System.out.println(" threw Exception e = " + e.getMessage());
    	}
    	assertEquals(dtPiece, "222233445566");
    }
    
    @Test
    public void testTimePieceNotime() {
    	String fName = "xxxxx.yyyy";
    	Boolean resultOk = true;
    	try {
    		auditG2S.getDateTimePieceOfFileName(fName); 
    	} catch (Exception e) {
    		System.out.println(" threw Exception e = " + e.getMessage());
    		resultOk = false;
    	}
    	assertFalse(resultOk);
    }
    
    @Test
    public void testTimePieceTooShort() {
    	String fName = "xxxxx.yyyy.22223";
    	Boolean resultOk = true;
    	try {
    		auditG2S.getDateTimePieceOfFileName(fName); 
    	} catch (Exception e) {
    		System.out.println(" threw Exception e = " + e.getMessage());
    		resultOk = false;
    	}
    	assertFalse(resultOk);
    }
    

    @Ignore
    @Test
    public void testGetCounts() throws IOException, EdgeRuleNotFoundException {
    	Boolean resultOk = true;
    	String dbName = "aai";
    	String fileNamePart = "dataSnapshot.graphSON.201908151845";
    	String srcDir = "src/test/resources/audit/";
    	HashMap <String,Integer> resHash = new HashMap <String,Integer> ();
    	try {
    		resHash = auditG2S.getCountsFromGraphsonSnapshot(dbName, fileNamePart, srcDir);
    	}
    	catch ( Exception e ) {
    		System.out.println("ERROR - got this exception: " + e.getMessage());
    		resultOk = false;
    	}
    	
    	assertTrue(resultOk);   	
    }
    

    @Ignore
    @Test
    public void testGoodRun() throws IOException, EdgeRuleNotFoundException {
    	
    	String [] argVals = {};

    	// this is the tStamp that would go
		//     with this file name: "dataSnapshot.graphSON.201908151845"
		Long tStamp = 1565842725000L;


		JsonObject jVal = new JsonObject();
		jVal.addProperty("autonomous-system",5);
		jVal.addProperty("pnf",7);


		String dbn = "aai_relational";
		String resStr = "";
    	try {
			when(apertureServiceMock.runAudit(anyLong(), anyString())).thenReturn(jVal);
    		resStr = auditG2S.runAudit("aai",
					"dataSnapshot.graphSON.201908151845",
					"src/test/resources/audit/");
    	}
    	catch ( Exception e ) {
    		System.out.println("ERROR - got this exception: " + e.getMessage());
			resStr = "Error";
    	}

    	assertTrue( resStr.startsWith("Audit ran and logged") );

    }
    
    @Test
    public void testRunWithBadParam() throws IOException, EdgeRuleNotFoundException {
    	
    	String resStr = "";
    	try {
    		resStr = auditG2S.runAudit("aai",
					"bogusFileName",
					"src/test/resources/audit/");
    	}
    	catch ( Exception e ) {
    		System.out.println("ERROR - got this exception: " + e.getMessage());
    		resStr = "Error";
    	}

		assertTrue( resStr.equals("Error") );
    }


	@Test
	public void testGetCountsFromJson() throws IOException, EdgeRuleNotFoundException {

		JsonObject sqlJsonData = new JsonObject ();
		sqlJsonData.addProperty("tableName1", 4);
		sqlJsonData.addProperty("tableName2", 5);
		sqlJsonData.addProperty("tableName3", 6);
		sqlJsonData.addProperty("tableName4", 7);

		HashMap <String,Integer> results1 = new HashMap <String,Integer> ();
		HashMap <String,Integer> results2 = new HashMap <String,Integer> ();
		try {
			results1 = auditG2S.getNodeCountsFromSQLRes(sqlJsonData);
			results2 = auditG2S.getNodeCountsFromSQLRes(sqlJsonData);
		}
		catch ( Exception e ) {
			System.out.println("ERROR - got this exception: " + e.getMessage());
		}

		assertEquals(4, results1.size());
		assertTrue( results1.containsKey("tableName3") );
		assertTrue( results1.get("tableName4") == 7);
		assertEquals(4, results2.size());
	}
    
}