Refactor Prov DB handling
[dmaap/datarouter.git] / datarouter-prov / src / test / java / org / onap / dmaap / datarouter / provisioning / beans / LogRecordTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dmaap.datarouter.provisioning.beans;
22
23 import java.io.ByteArrayOutputStream;
24 import java.io.IOException;
25 import java.io.PrintStream;
26 import java.sql.Connection;
27 import java.sql.PreparedStatement;
28 import java.sql.SQLException;
29 import java.text.ParseException;
30 import javax.persistence.EntityManager;
31 import javax.persistence.EntityManagerFactory;
32 import javax.persistence.Persistence;
33 import org.junit.AfterClass;
34 import org.junit.Assert;
35 import org.junit.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.onap.dmaap.datarouter.provisioning.utils.ProvDbUtils;
39 import org.onap.dmaap.datarouter.provisioning.utils.RLEBitSet;
40
41 public class LogRecordTest {
42
43     private static EntityManagerFactory emf;
44     private static EntityManager em;
45
46     @BeforeClass
47     public static void init() {
48         emf = Persistence.createEntityManagerFactory("dr-unit-tests");
49         em = emf.createEntityManager();
50         System.setProperty(
51                 "org.onap.dmaap.datarouter.provserver.properties",
52                 "src/test/resources/h2Database.properties");
53     }
54
55     @AfterClass
56     public static void tearDownClass() {
57         em.clear();
58         em.close();
59         emf.close();
60     }
61
62     @Before
63     public void setUp() throws ParseException, SQLException {
64
65     }
66
67     @Test
68     public void Validate_Load_For_Pub_Type_Sets_Correct_Values() throws SQLException, ParseException {
69         setArgsLoadAndAssertEquals("pub", "{1: 'pub', 3: 'ID', 4: 1, 5: 'URL/file123', 6: 'PUT', 7: 'application/vnd.dmaap-.log-list; version=1.0', 8: 100, 9: '1', 10: '172.0.0.8', 11: 'user', 12: 204, 13: NULL, 14: NULL, 15: NULL, 16: NULL, 17: NULL, 18: 1, 19: NULL, 20: 'file123'}");
70     }
71
72     @Test
73     public void Validate_Load_For_Del_Type_Sets_Correct_Values() throws SQLException, ParseException {
74         setArgsLoadAndAssertEquals("del", "{1: 'del', 3: 'ID', 4: 1, 5: 'URL/file123', 6: 'PUT', 7: 'application/vnd.dmaap-.log-list; version=1.0', 8: 100, 9: NULL, 10: NULL, 11: 'user', 12: NULL, 13: 1, 14: '1', 15: 204, 16: NULL, 17: NULL, 18: 1, 19: NULL, 20: 'file123'}");
75     }
76
77     @Test
78     public void Validate_Load_For_Exp_Type_Sets_Correct_Values() throws SQLException, ParseException {
79         setArgsLoadAndAssertEquals("exp", "{1: 'exp', 3: 'ID', 4: 1, 5: 'URL/file123', 6: 'PUT', 7: 'application/vnd.dmaap-.log-list; version=1.0', 8: 100, 9: NULL, 10: NULL, 11: NULL, 12: NULL, 13: 1, 14: '1', 15: NULL, 16: 0, 17: 'other', 18: 1, 19: NULL, 20: 'file123'}");
80     }
81
82     @Test
83     public void Validate_Load_For_Pbf_Type_Sets_Correct_Values() throws SQLException, ParseException {
84         setArgsLoadAndAssertEquals("pbf", "{1: 'pbf', 3: 'ID', 4: 1, 5: 'URL/file123', 6: 'PUT', 7: 'application/vnd.dmaap-.log-list; version=1.0', 8: 100, 9: '1', 10: '172.0.0.8', 11: 'user', 12: NULL, 13: NULL, 14: NULL, 15: NULL, 16: NULL, 17: NULL, 18: 1, 19: 100, 20: 'file123'}");
85     }
86
87     @Test
88     public void Validate_Load_For_Dlx_Type_Sets_Correct_Values() throws SQLException, ParseException {
89         setArgsLoadAndAssertEquals("dlx", "{1: 'dlx', 3: 'ID', 4: 1, 5: 'URL/file123', 6: 'PUT', 7: 'application/vnd.dmaap-.log-list; version=1.0', 8: 100, 9: NULL, 10: NULL, 11: NULL, 12: NULL, 13: 1, 14: NULL, 15: NULL, 16: NULL, 17: NULL, 18: 1, 19: 100, 20: 'file123'}");
90     }
91
92     @Test
93     public void Validate_printLogRecords_Prints_Correct_Values() throws IOException {
94         ByteArrayOutputStream outContent = new ByteArrayOutputStream();
95         System.setOut(new PrintStream(outContent));
96         String[] rlebitset = {"0-1,2-2"};
97         LogRecord.printLogRecords(System.out, new RLEBitSet(rlebitset[0]));
98         Assert.assertEquals(outContent.toString().substring(25), "LOG|ID|1|URL/file123|PUT|application/vnd.dmaap-dr.log-list; version=1.0|100|pub|1|172.0.0.8|user|204|1|1|204|0|other|1|0\n");
99     }
100
101     private void setArgsLoadAndAssertEquals(String type, String s) throws ParseException, SQLException {
102         String[] args = {"2018-08-29-10-10-10-543.", "LOG", "ID", "1", "URL/file123", "PUT", "application/vnd.dmaap-.log-list; version=1.0", "100", type, "1", "172.0.0.8", "user", "204", "1", "1", "204", "0", "other", "1", "100", "file123"};
103         LogRecord logRecord = new LogRecord(args);
104         try (Connection conn = ProvDbUtils.getInstance().getConnection()) {
105             PreparedStatement ps = conn.prepareStatement(
106                 "insert into LOG_RECORDS values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");
107             logRecord.load(ps);
108             String compare_string = ps.toString().substring(ps.toString().indexOf("{1:"), ps.toString().indexOf("2:")) + ps
109                 .toString().substring(ps.toString().indexOf("3:"));
110             Assert.assertEquals(compare_string, s);
111         }
112     }
113 }