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