Test coverage in transaction-recorder
[appc.git] / appc-dispatcher / appc-dispatcher-common / transaction-recorder / src / test / java / org / onap / appc / transactionrecorder / impl / TransactionRecorderImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : APPC
4  * ================================================================================
5  * Copyright (C) 2017-2018 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Copyright (C) 2017 Amdocs
8  * ================================================================================
9  * Modifications Copyright (C) 2019 Ericsson
10  * =============================================================================
11  * Licensed under the Apache License, Version 2.0 (the "License");
12  * you may not use this file except in compliance with the License.
13  * You may obtain a copy of the License at
14  *
15  *      http://www.apache.org/licenses/LICENSE-2.0
16  *
17  * Unless required by applicable law or agreed to in writing, software
18  * distributed under the License is distributed on an "AS IS" BASIS,
19  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
20  * See the License for the specific language governing permissions and
21  * limitations under the License.
22  *
23  * ============LICENSE_END=========================================================
24  */
25
26 package org.onap.appc.transactionrecorder.impl;
27
28 import static org.mockito.Matchers.anyObject;
29 import static org.mockito.Matchers.anyString;
30 import static org.hamcrest.CoreMatchers.isA;
31 import java.sql.Connection;
32 import java.sql.PreparedStatement;
33 import java.sql.ResultSet;
34 import java.sql.SQLException;
35 import java.sql.Statement;
36 import java.text.ParseException;
37 import java.time.Instant;
38 import java.time.ZoneOffset;
39 import java.time.format.DateTimeFormatter;
40 import java.time.temporal.ChronoUnit;
41 import java.util.ArrayList;
42 import java.util.HashMap;
43 import java.util.List;
44 import java.util.Map;
45 import java.util.UUID;
46 import javax.sql.rowset.CachedRowSet;
47 import org.junit.After;
48 import org.junit.Assert;
49 import org.junit.Before;
50 import org.junit.Rule;
51 import org.junit.Test;
52 import org.junit.rules.ExpectedException;
53 import org.mockito.Mockito;
54 import org.onap.appc.dao.util.dbcp.DBConnectionPool;
55 import org.onap.appc.dao.util.helper.DBHelper;
56 import org.onap.appc.domainmodel.lcm.Flags;
57 import org.onap.appc.domainmodel.lcm.RequestStatus;
58 import org.onap.appc.domainmodel.lcm.TransactionRecord;
59 import org.onap.appc.domainmodel.lcm.VNFOperation;
60 import org.onap.appc.exceptions.APPCException;
61 import org.onap.appc.transactionrecorder.objects.TransactionConstants;
62 import org.onap.appc.transactionrecorder.objects.TransactionConstants.TRANSACTION_ATTRIBUTES;
63 import org.onap.ccsdk.sli.core.dblib.DbLibService;
64 import com.sun.rowset.CachedRowSetImpl;
65
66 /**
67  * Test class for TransactionRecorder
68  */
69 public class TransactionRecorderImplTest {
70
71     private String dbUrl = "jdbc:h2:mem:test;MODE=MYSQL;DB_CLOSE_DELAY=-1";
72     private String username = "sa";
73     private String password = "sa";
74     private String driver = "org.h2.Driver";
75
76     private TransactionRecorderImpl transactionRecorderImpl;
77     private DbLibService dbLibService;
78
79     private DBConnectionPool dbConnectionPool;
80
81
82     /**
83      * Ideally JUnit should grab the SQL to create the transaction table from the same source used in deployments;
84      * however, at the time of writing this that was not possible.  Should it become possible in the future please
85      * update this JUnit test to use the deployment source.
86      * <p>
87      * Please ensure this table create script is identical to the source script used in a deployment.
88      */
89     private String TRANSACTION_CREATE_TABLE = "CREATE TABLE TRANSACTIONS (" +
90             "  TRANSACTION_ID VARCHAR(75) NOT NULL PRIMARY KEY," +
91             "  ORIGIN_TIMESTAMP DATETIME(3) NOT NULL," +
92             "  REQUEST_ID VARCHAR(256) NOT NULL," +
93             "  SUBREQUEST_ID VARCHAR(256) DEFAULT NULL," +
94             "  ORIGINATOR_ID VARCHAR(256) DEFAULT NULL," +
95             "  START_TIME DATETIME(3) NOT NULL," +
96             "  END_TIME DATETIME(3) DEFAULT NULL," +
97             "  TARGET_ID VARCHAR(256) NOT NULL," +
98             "  TARGET_TYPE VARCHAR(256) DEFAULT NULL," +
99             "  OPERATION VARCHAR(256) NOT NULL," +
100             "  RESULT_CODE INT(11) DEFAULT NULL," +
101             "  DESCRIPTION TEXT," +
102             "  STATE VARCHAR(50) NOT NULL," +
103             "  SERVICE_INSTANCE_ID VARCHAR(256) DEFAULT NULL," +
104             "  VNFC_NAME VARCHAR(256) DEFAULT NULL," +
105             "  VSERVER_ID VARCHAR(256) DEFAULT NULL," +
106             "  VF_MODULE_ID VARCHAR(256) DEFAULT NULL," +
107             "  MODE VARCHAR(50) NOT NULL," +
108             ")";
109     private String TRANSACTION_DROP_TABLE = "DROP TABLE IF EXISTS TRANSACTIONS";
110
111     @Rule
112     public ExpectedException expectedEx = ExpectedException.none();
113
114     @Before
115     public void setUp() throws Exception {
116         transactionRecorderImpl = new TransactionRecorderImpl();
117         transactionRecorderImpl.setAppcInstanceId("123");
118         dbLibService = Mockito.mock(DbLibService.class);
119         transactionRecorderImpl.setDbLibService(dbLibService);
120         dbConnectionPool = new DBConnectionPool(dbUrl, username, password, driver);
121         executeUpdate(TRANSACTION_CREATE_TABLE);
122     }
123
124
125     @After
126     public void shutdown() {
127         if (dbConnectionPool != null) {
128             executeUpdate(TRANSACTION_DROP_TABLE);
129             dbConnectionPool.shutdown();
130         }
131     }
132
133     private void executeUpdate(String updateSQL) {
134         Connection connection = null;
135         Statement stmt = null;
136         try {
137             connection = dbConnectionPool.getConnection();
138             stmt = connection.createStatement();
139             stmt.executeUpdate(updateSQL);
140         } catch (SQLException e) {
141             throw new RuntimeException(e);
142         } finally {
143             DBHelper.close(null, stmt, connection);
144         }
145     }
146
147     /**
148      * Verify the transactionRecorderImpl.sore() store the TransactionRecord correctly in the database.
149      */
150     @Test
151     public void testStore() throws Exception {
152
153         TransactionRecord input = prepareTransactionsInput();
154         Mockito.when(dbLibService.writeData(anyString(), anyObject(), anyString())).thenAnswer(invocation ->
155                 testStoreInMemory(invocation.getArguments()));
156         transactionRecorderImpl.store(input);
157
158     }
159
160     @Test
161     public void testStoreExceptionFlow() throws SQLException, APPCException {
162
163         TransactionRecord input = prepareTransactionsInput();
164         Mockito.when(dbLibService.writeData(anyString(), anyObject(), anyString())).thenThrow(new SQLException());
165         expectedEx.expect(APPCException.class);
166         expectedEx.expectMessage(TransactionConstants.ERROR_ACCESSING_DATABASE);
167         expectedEx.expectCause(isA(SQLException.class));
168         transactionRecorderImpl.store(input);
169
170     }
171
172     @Test
173     public void testGetInProgressRequests() throws SQLException, APPCException {
174         TransactionRecord record1 = prepareTransactionsInput();
175         insertRecord(record1);
176         TransactionRecord input = prepareTransactionsInput();
177         input.setStartTime(Instant.now());
178         Mockito.when(dbLibService.getData(anyString(), anyObject(), anyString())).thenAnswer(invocation ->
179                 inMemoryExecutionWithResultSet(invocation.getArguments()));
180         Assert.assertEquals(1, transactionRecorderImpl.getInProgressRequests(input, 0).size());
181
182     }
183
184     @Test
185     public void testGetInProgressRequestsSqlException() throws SQLException, APPCException {
186         TransactionRecord record1 = prepareTransactionsInput();
187         insertRecord(record1);
188         TransactionRecord input = prepareTransactionsInput();
189         input.setStartTime(Instant.now());
190         Mockito.when(dbLibService.getData(anyString(), anyObject(), anyString())).thenThrow(new SQLException());
191         expectedEx.expect(APPCException.class);
192         expectedEx.expectMessage(TransactionConstants.ERROR_ACCESSING_DATABASE);
193         expectedEx.expectCause(isA(SQLException.class));
194         transactionRecorderImpl.getInProgressRequests(input, 0);
195     }
196
197     @Test
198     public void testGetInProgressRequestsWithinTimeInterval() throws SQLException, APPCException {
199         TransactionRecord record1 = prepareTransactionsInput();
200         record1.setStartTime(Instant.now().minus(4,ChronoUnit.HOURS));
201         insertRecord(record1);
202         TransactionRecord input = prepareTransactionsInput();
203         input.setStartTime(Instant.now());
204         Mockito.when(dbLibService.getData(anyString(), anyObject(), anyString())).thenAnswer(invocation ->
205                 inMemoryExecutionWithResultSet(invocation.getArguments()));
206         List<TransactionRecord> aList= transactionRecorderImpl.getInProgressRequests(input,12);
207         Assert.assertEquals(1, transactionRecorderImpl.getInProgressRequests(input,12).size());
208
209     }
210
211     @Test
212     public void testIsTransactionDuplicate() throws SQLException, APPCException {
213         TransactionRecord input = prepareTransactionsInput();
214         Mockito.when(dbLibService.getData(anyString(), anyObject(), anyString())).thenAnswer(invocation ->
215                 inMemoryExecutionWithResultSet(invocation.getArguments()));
216         Assert.assertFalse(transactionRecorderImpl.isTransactionDuplicate(input));
217
218     }
219
220     @Test
221     public void testIsTransactionDuplicateExceptionFlow() throws SQLException, APPCException {
222         TransactionRecord input = prepareTransactionsInput();
223         Mockito.when(dbLibService.getData(anyString(), anyObject(), anyString())).thenThrow(new SQLException());
224         expectedEx.expect(APPCException.class);
225         expectedEx.expectMessage(TransactionConstants.ERROR_ACCESSING_DATABASE);
226         expectedEx.expectCause(isA(SQLException.class));
227         transactionRecorderImpl.isTransactionDuplicate(input);
228     }
229
230     @Test
231     public void testIsTransactionDuplicateAlternativeFlow() throws SQLException, APPCException {
232         TransactionRecord input = prepareTransactionsInput();
233         input.setSubRequestId(null);
234         input.setOriginatorId(null);
235         CachedRowSetImpl rowset = Mockito.mock(CachedRowSetImpl.class);
236         Mockito.when(rowset.first()).thenReturn(true);
237         Mockito.when(rowset.getString(TransactionConstants.TRANSACTION_ATTRIBUTES.TRANSACTION_ID.getColumnName()))
238             .thenReturn(null);
239         Mockito.when(dbLibService.getData(anyString(), anyObject(), anyString())).thenReturn(rowset);
240         Assert.assertTrue(transactionRecorderImpl.isTransactionDuplicate(input));
241     }
242
243     @Test
244     public void testGetInProgressRequestsCount() throws SQLException, APPCException {
245         TransactionRecord input = prepareTransactionsInput();
246         Mockito.when(dbLibService.getData(anyString(), anyObject(), anyString())).thenAnswer(invocation ->
247                 inMemoryExecutionWithResultSet(invocation.getArguments()));
248         Assert.assertEquals(0, transactionRecorderImpl.getInProgressRequestsCount().intValue());
249     }
250
251     @Test
252     public void testGetInProgressRequestsCountSqlException() throws SQLException, APPCException {
253         TransactionRecord input = prepareTransactionsInput();
254         Mockito.when(dbLibService.getData(anyString(), anyObject(), anyString())).thenThrow(new SQLException());
255         expectedEx.expect(APPCException.class);
256         expectedEx.expectMessage(TransactionConstants.ERROR_ACCESSING_DATABASE);
257         expectedEx.expectCause(isA(SQLException.class));
258         transactionRecorderImpl.getInProgressRequestsCount();
259     }
260
261     @Test
262     public void testGetInProgressRequestsCountNoRecords() throws SQLException, APPCException {
263         CachedRowSetImpl rowset = Mockito.mock(CachedRowSetImpl.class);
264         Mockito.when(rowset.first()).thenReturn(false);
265         Mockito.when(dbLibService.getData(anyString(), anyObject(), anyString())).thenReturn(rowset);
266         expectedEx.expect(APPCException.class);
267         expectedEx.expectMessage(TransactionConstants.ERROR_ACCESSING_DATABASE);
268         transactionRecorderImpl.getInProgressRequestsCount();
269     }
270
271     @Test
272     public void testUpdate() throws APPCException, SQLException {
273         TransactionRecord input = prepareTransactionsInput();
274         insertRecord(input);
275         Map<TransactionConstants.TRANSACTION_ATTRIBUTES, String> updateColumns = new HashMap<>();
276         updateColumns.put(TransactionConstants.TRANSACTION_ATTRIBUTES.TARGET_TYPE, "Firewall");
277         Mockito.when(dbLibService.writeData(anyString(), anyObject(), anyString())).thenAnswer(invocation ->
278                 testUpdateInMemory(invocation.getArguments()));
279         transactionRecorderImpl.update(input.getTransactionId(), updateColumns);
280     }
281
282     @Test
283     public void testUpdateExceptionFlow() throws APPCException, SQLException {
284         TransactionRecord input = prepareTransactionsInput();
285         insertRecord(input);
286         Map<TransactionConstants.TRANSACTION_ATTRIBUTES, String> updateColumns = new HashMap<>();
287         updateColumns.put(TransactionConstants.TRANSACTION_ATTRIBUTES.TARGET_TYPE, "Firewall");
288         Mockito.when(dbLibService.writeData(anyString(), anyObject(), anyString())).thenThrow(new SQLException());
289         expectedEx.expect(APPCException.class);
290         expectedEx.expectMessage(TransactionConstants.ERROR_ACCESSING_DATABASE);
291         expectedEx.expectCause(isA(SQLException.class));
292         transactionRecorderImpl.update(input.getTransactionId(), updateColumns);
293     }
294
295     @Test
296     public void testMarkTransactionsAborted() throws SQLException {
297         TransactionRecord input = prepareTransactionsInput();
298         insertRecord(input);
299         Mockito.when(dbLibService.writeData(anyString(), anyObject(), anyString())).thenAnswer(invocation ->
300                 testMarkAbortedInMemory(invocation.getArguments()));
301         transactionRecorderImpl.markTransactionsAborted("123~");
302     }
303
304     @Test
305     public void testMarkTransactionsAbortedExceptionFlow() throws SQLException {
306         TransactionRecord input = prepareTransactionsInput();
307         insertRecord(input);
308         Mockito.when(dbLibService.writeData(anyString(), anyObject(), anyString())).thenThrow(new SQLException());
309         expectedEx.expect(RuntimeException.class);
310         expectedEx.expectMessage("In progress transactions couldn't be marked aborted on server start up");
311         expectedEx.expectCause(isA(SQLException.class));
312         transactionRecorderImpl.markTransactionsAborted("123~");
313     }
314
315     @Test
316     public void testGetRecords() throws SQLException, APPCException {
317         CachedRowSetImpl rowset = Mockito.mock(CachedRowSetImpl.class);
318         Mockito.when(rowset.next()).thenReturn(true).thenReturn(false);
319         Mockito.when(rowset.getString(TRANSACTION_ATTRIBUTES.STATE.getColumnName())).thenReturn("NAME");
320         Mockito.when(dbLibService.getData(anyString(), anyObject(), anyString())).thenReturn(rowset);
321         Assert.assertEquals(RequestStatus.UNKNOWN,
322                 transactionRecorderImpl.getRecords(null, "SUBREQUEST_ID", "ORIGINATOR_ID", null).get(0));
323     }
324
325     @Test
326     public void testGetRecordsSqlException() throws SQLException, APPCException {
327         Mockito.when(dbLibService.getData(anyString(), anyObject(), anyString())).thenThrow(new SQLException());
328         expectedEx.expect(APPCException.class);
329         expectedEx.expectMessage("Error retrieving record for requestID null and vnfId null");
330         expectedEx.expectCause(isA(SQLException.class));
331         transactionRecorderImpl.getRecords(null, null, null, null);
332     }
333
334     private ResultSet inMemoryExecutionWithResultSet(Object[] obj) throws Exception {
335         String query = (String) obj[0];
336         ArrayList<String> args = (ArrayList<String>) obj[1];
337         Connection con = dbConnectionPool.getConnection();
338         PreparedStatement ps = con.prepareStatement(query);
339         for (int i = 1; i <= args.size(); i++) {
340             ps.setString(i, args.get(i - 1));
341         }
342         CachedRowSet rowSet = new CachedRowSetImpl();
343         rowSet.populate(ps.executeQuery());
344         return rowSet;
345     }
346
347     private boolean testMarkAbortedInMemory(Object[] obj) throws Exception {
348         String query = (String) obj[0];
349         ArrayList<String> args = (ArrayList<String>) obj[1];
350         Connection con = dbConnectionPool.getConnection();
351         PreparedStatement ps = con.prepareStatement(query);
352         for (int i = 1; i <= args.size(); i++) {
353             ps.setString(i, args.get(i - 1));
354         }
355         ps.execute();
356         return isTransactionAborted();
357     }
358
359     private boolean isTransactionAborted() throws Exception {
360         String query = "SELECT COUNT(*) FROM  TRANSACTIONS WHERE STATE = ?";
361         Connection con = dbConnectionPool.getConnection();
362         PreparedStatement ps = con.prepareStatement(query);
363         ps.setString(1, RequestStatus.ABORTED.toString());
364         ResultSet rs = ps.executeQuery();
365         while (rs.next()) {
366             int value = rs.getInt(1);
367             if (value == 1) {
368                 System.out.println("Non terminal Transactions are aborted");
369                 return true;
370             }
371         }
372         throw new Exception("Transactions are not aborted");
373     }
374
375     private boolean testUpdateInMemory(Object[] obj) throws Exception {
376         String query = (String) obj[0];
377         ArrayList<String> args = (ArrayList<String>) obj[1];
378         Connection con = dbConnectionPool.getConnection();
379         PreparedStatement ps = con.prepareStatement(query);
380         for (int i = 1; i <= args.size(); i++) {
381             ps.setString(i, args.get(i - 1));
382         }
383         ps.execute();
384         String updatedValue = checkIfValueIsUpdated(args.get(1));
385         System.out.println("updated Value is " + updatedValue);
386         if (updatedValue.equals("Firewall")) {
387             return true;
388         }
389         throw new Exception("Not Updated");
390     }
391
392     private boolean testStoreInMemory(Object[] obj) throws Exception {
393         String query = (String) obj[0];
394         ArrayList<String> args = (ArrayList<String>) obj[1];
395         Connection con = dbConnectionPool.getConnection();
396         PreparedStatement ps = con.prepareStatement(query);
397         for (int i = 1; i <= args.size(); i++) {
398             ps.setString(i, args.get(i - 1));
399         }
400         ps.execute();
401         if (checkIfRowIsPresent(args.get(0))) {
402             return true;
403         }
404         throw new Exception("Failed to update");
405     }
406
407     private TransactionRecord prepareTransactionsInput() {
408         TransactionRecord input = new TransactionRecord();
409         input.setTransactionId(UUID.randomUUID().toString());
410         input.setOriginTimestamp(Instant.parse("2017-09-11T00:00:01.00Z"));
411         input.setRequestId("REQUEST_ID");
412         input.setSubRequestId("SUB_REQUEST_ID");
413         input.setOriginatorId("ORIGINATOR_ID");
414         input.setStartTime(Instant.parse("2017-09-11T00:00:02.00Z"));
415         input.setTargetId("TARGET_ID");
416         input.setTargetType("TARGET_TYPE");
417         input.setServiceInstanceId("SERVICE_INSTANCE_ID");
418         input.setOperation(VNFOperation.ActionStatus);
419         input.setResultCode(200);
420         input.setRequestState(RequestStatus.ACCEPTED);
421         input.setDescription("DESCRIPTION");
422         input.setMode(Flags.Mode.EXCLUSIVE);
423         return input;
424     }
425
426     private void insertRecord(TransactionRecord input) throws SQLException {
427         final String STORE_DATE_QUERY = TransactionConstants.INSERT_INTO + TransactionConstants.TRANSACTIONS +
428                 " values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
429         Connection con = dbConnectionPool.getConnection();
430         PreparedStatement ps = con.prepareStatement(STORE_DATE_QUERY);
431         ArrayList<String> args = prepareArguments(input);
432         args.remove(0);
433         args.add(0, "123~" + input.getTransactionId());
434         for (int i = 1; i <= 18; i++) {
435             ps.setString(i, args.get(i - 1));
436         }
437         ps.execute();
438         if (checkIfRowIsPresent(args.get(0))) {
439             System.out.println("RECORD INSERTED " + args.get(0));
440         }
441
442     }
443
444     private ArrayList<String> prepareArguments(TransactionRecord input) {
445         ArrayList<String> arguments = new ArrayList<>();
446         arguments.add(input.getTransactionId());
447         arguments.add(dateToStringConverterMillis(input.getOriginTimestamp()));
448         arguments.add(input.getRequestId());
449         arguments.add(input.getSubRequestId());
450         arguments.add(input.getOriginatorId());
451         arguments.add(dateToStringConverterMillis(input.getStartTime()));
452         arguments.add(dateToStringConverterMillis(input.getEndTime()));
453         arguments.add(input.getTargetId());
454         arguments.add(input.getTargetType());
455         arguments.add(input.getOperation().name());
456         arguments.add(String.valueOf(input.getResultCode()));
457         arguments.add(input.getDescription());
458         arguments.add(input.getRequestState());
459         arguments.add(input.getServiceInstanceId());
460         arguments.add(input.getVnfcName());
461         arguments.add(input.getVserverId());
462         arguments.add(input.getVfModuleId());
463         arguments.add(input.getMode());
464
465         return arguments;
466     }
467
468     private static String dateToStringConverterMillis(Instant date) {
469         if (date == null) {
470             return null;
471         }
472         DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS").withZone(ZoneOffset.UTC);
473         return formatter.format(date);
474     }
475
476     private boolean checkIfRowIsPresent(String key) {
477         Connection con = null;
478         ResultSet rs = null;
479         PreparedStatement ps = null;
480         try {
481             con = dbConnectionPool.getConnection();
482             ps = con.prepareStatement("SELECT COUNT(*) FROM  TRANSACTIONS WHERE TRANSACTION_ID = ?");
483             ps.setString(1, key);
484             rs = ps.executeQuery();
485             while (rs.next()) {
486                 int value = rs.getInt(1);
487                 System.out.println("KEY checked is " + key + " COUNT RETURNED IS " + value);
488                 if (value == 1) {
489                     return true;
490                 }
491             }
492         } catch (SQLException e) {
493             e.printStackTrace();
494         } finally {
495             DBHelper.close(rs, ps, con);
496         }
497         return false;
498     }
499
500     private String checkIfValueIsUpdated(String key) throws Exception {
501         Connection con = dbConnectionPool.getConnection();
502         PreparedStatement ps = con.prepareStatement("SELECT TARGET_TYPE FROM  TRANSACTIONS WHERE TRANSACTION_ID = ?");
503         ps.setString(1, key);
504         ResultSet rs = ps.executeQuery();
505         while (rs.next()) {
506             String value = rs.getString("TARGET_TYPE");
507             return value;
508         }
509         throw new Exception("Value not found");
510     }
511
512
513     /**
514      * Verify the transactionRecorderImpl. getRecords () can be fetch with each of the parameter combinations
515      * @throws Exception
516      *//*
517     @Test
518     public void test_api_getRecords() throws Exception {
519
520
521         final int requestId = 0;
522         final int subrequestId = 1;
523         final int originatorId = 2;
524         final int vnfId = 3;
525         final int requestStatus = 4;
526
527
528         String[][] trCreateMatrix = {
529             {"request1", "subrequestId1", "originatorId1", "vnfId1", RequestStatus.UNKNOWN.name()},
530             {"request1", "subrequestId2", "originatorId1", "vnfId1", RequestStatus.RECEIVED.name()},
531             {"request2", "subrequestId1", "originatorId1", "vnfId1", RequestStatus.ACCEPTED.name()},
532             {"request2", "subrequestId2", "originatorId1", "vnfId1", RequestStatus.REJECTED.name()},
533             {"request1", "subrequestId1", "originatorId1", "vnfId2", RequestStatus.SUCCESSFUL.name()},
534             {"request1", "subrequestId2", "originatorId1", "vnfId2", RequestStatus.FAILED.name()},
535             {"request2", "subrequestId1", "originatorId1", "vnfId2", RequestStatus.TIMEOUT.name()},
536             {"request2", "subrequestId2", "originatorId1", "vnfId2", RequestStatus.ABORTED.name()},
537             {"request1", "subrequestId1", "originatorId2", "vnfId1", RequestStatus.UNKNOWN.name()},
538             {"request1", "subrequestId2", "originatorId2", "vnfId1", RequestStatus.RECEIVED.name()},
539             {"request2", "subrequestId1", "originatorId2", "vnfId1", RequestStatus.ACCEPTED.name()},
540             {"request2", "subrequestId2", "originatorId2", "vnfId1", RequestStatus.REJECTED.name()},
541             {"request1", "subrequestId1", "originatorId2", "vnfId2", RequestStatus.SUCCESSFUL.name()},
542             {"request1", "subrequestId2", "originatorId2", "vnfId2", RequestStatus.FAILED.name()},
543             {"request2", "subrequestId1", "originatorId2", "vnfId2", RequestStatus.TIMEOUT.name()},
544             {"request2", "subrequestId2", "originatorId2", "vnfId2", RequestStatus.ABORTED.name()},
545         };
546
547
548         TransactionRecord tr = new TransactionRecord();
549         tr.setTimeStamp(Instant.parse("2017-09-11T00:00:01.00Z"));
550         tr.setStartTime(Instant.parse("2017-09-11T00:00:02.00Z"));
551         tr.setEndTime(Instant.parse("2017-09-11T00:00:03.00Z"));
552         tr.setTargetType("TARGET_TYPE");
553         tr.setSubComponent("SUB_COMPONENT");
554         tr.setOperation(VNFOperation.ActionStatus);
555         tr.setResultCode("RESULT_CODE");
556         tr.setDescription("DESCRIPTION");
557
558         for (int row = 0; row < trCreateMatrix.length; row++) {
559             tr.setRequestID(trCreateMatrix[row][requestId]);
560             tr.setSubRequestID(trCreateMatrix[row][subrequestId]);
561             tr.setOriginatorId(trCreateMatrix[row][originatorId]);
562             tr.setTargetID(trCreateMatrix[row][vnfId]);
563             tr.setRequestStatus(RequestStatus.valueOf(trCreateMatrix[row][requestStatus]));
564             transactionRecorderImpl.store(tr);
565         }
566
567
568         String[][] trSearchMatrix = {
569             {"request1", null, null, "vnfId1"},
570             {"request2", "subrequestId1", null, "vnfId1"},
571             {"request1", null, "originatorId1", "vnfId1"},
572             {"request2", "subrequestId2", "originatorId1", "vnfId1"},
573         };
574
575
576         for (int i = 0; i < trSearchMatrix.length; i++) {
577             final int row = i;
578             List<RequestStatus> actualList = transactionRecorderImpl
579                 .getRecords(trSearchMatrix[row][requestId], trSearchMatrix[row][subrequestId],
580                     trSearchMatrix[row][originatorId], trSearchMatrix[row][vnfId])
581                 .stream()
582                 .sorted()
583                 .collect(Collectors.toList());
584
585             List<RequestStatus> expectedList = Arrays.stream(trCreateMatrix)
586                 .filter(entry -> entry[requestId].equals(trSearchMatrix[row][requestId]))
587                 .filter(entry -> trSearchMatrix[row][subrequestId] == null || entry[subrequestId].equals
588                     (trSearchMatrix[row][subrequestId]))
589                 .filter(entry -> trSearchMatrix[row][originatorId] == null || entry[originatorId].equals
590                     (trSearchMatrix[row][originatorId]))
591                 .filter(entry -> entry[vnfId].equals(trSearchMatrix[row][vnfId]))
592                 .map(entry -> RequestStatus.valueOf(entry[requestStatus]))
593                 .sorted()
594                 .collect(Collectors.toList());
595             System.out.println(expectedList);
596             System.out.println(actualList);
597             Assert.assertEquals("Unexpected results: ", expectedList, actualList);
598
599         }
600
601
602     }*/
603 }