bd0ebdcbf55af381721d3a64c4069fa1868b7327
[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 org.junit.*;
24 import org.onap.dmaap.datarouter.provisioning.utils.DB;
25 import org.onap.dmaap.datarouter.provisioning.utils.RLEBitSet;
26
27 import javax.persistence.EntityManager;
28 import javax.persistence.EntityManagerFactory;
29 import javax.persistence.Persistence;
30 import java.io.ByteArrayOutputStream;
31 import java.io.IOException;
32 import java.io.PrintStream;
33 import java.sql.Connection;
34 import java.sql.PreparedStatement;
35 import java.sql.SQLException;
36 import java.text.ParseException;
37
38 public class LogRecordTest {
39
40     private LogRecord logRecord;
41     private static EntityManagerFactory emf;
42     private static EntityManager em;
43     private DB db;
44     private static final String INSERT_SQL = "insert into LOG_RECORDS values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
45     private PreparedStatement ps;
46
47     @BeforeClass
48     public static void init() {
49         emf = Persistence.createEntityManagerFactory("dr-unit-tests");
50         em = emf.createEntityManager();
51         System.setProperty(
52                 "org.onap.dmaap.datarouter.provserver.properties",
53                 "src/test/resources/h2Database.properties");
54     }
55
56     @AfterClass
57     public static void tearDownClass() {
58         em.clear();
59         em.close();
60         emf.close();
61     }
62
63     @Before
64     public void setUp() throws ParseException, SQLException {
65         db = new DB();
66         Connection conn = db.getConnection();
67         ps = conn.prepareStatement(INSERT_SQL);
68     }
69
70     @Test
71     public void Validate_Load_For_Pub_Type_Sets_Correct_Values() throws SQLException, ParseException {
72         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'}");
73     }
74
75     @Test
76     public void Validate_Load_For_Del_Type_Sets_Correct_Values() throws SQLException, ParseException {
77         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'}");
78     }
79
80     @Test
81     public void Validate_Load_For_Exp_Type_Sets_Correct_Values() throws SQLException, ParseException {
82         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'}");
83     }
84
85     @Test
86     public void Validate_Load_For_Pbf_Type_Sets_Correct_Values() throws SQLException, ParseException {
87         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'}");
88     }
89
90     @Test
91     public void Validate_Load_For_Dlx_Type_Sets_Correct_Values() throws SQLException, ParseException {
92         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'}");
93     }
94
95     @Test
96     public void Validate_printLogRecords_Prints_Correct_Values() throws IOException {
97         ByteArrayOutputStream outContent = new ByteArrayOutputStream();
98         System.setOut(new PrintStream(outContent));
99         String[] rlebitset = {"0-1,2-2"};
100         LogRecord.printLogRecords(System.out, new RLEBitSet(rlebitset[0]));
101         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");
102     }
103
104     private void setArgsLoadAndAssertEquals(String type, String s) throws ParseException, SQLException {
105         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"};
106         logRecord = new LogRecord(args);
107         logRecord.load(ps);
108         String compare_string = ps.toString().substring(ps.toString().indexOf("{1:"), ps.toString().indexOf("2:")) + ps.toString().substring(ps.toString().indexOf("3:"));
109         Assert.assertEquals(compare_string, s);
110     }
111 }