1 /*******************************************************************************
2 * ============LICENSE_START==================================================
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
11 * * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
20 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
22 ******************************************************************************/
24 package org.onap.dmaap.datarouter.provisioning.beans;
27 import org.onap.dmaap.datarouter.provisioning.utils.DB;
28 import org.onap.dmaap.datarouter.provisioning.utils.RLEBitSet;
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;
41 public class LogRecordTest {
43 private LogRecord logRecord;
44 private static EntityManagerFactory emf;
45 private static EntityManager em;
47 private static final String INSERT_SQL = "insert into LOG_RECORDS values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
48 private PreparedStatement ps;
51 public static void init() {
52 emf = Persistence.createEntityManagerFactory("dr-unit-tests");
53 em = emf.createEntityManager();
55 "org.onap.dmaap.datarouter.provserver.properties",
56 "src/test/resources/h2Database.properties");
60 public static void tearDownClass() {
67 public void setUp() throws ParseException, SQLException {
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);
76 public void Validate_Load_For_Pub_Type_Sets_Correct_Values() throws SQLException {
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'}");
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'}");
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'}");
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'}");
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'}");
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");
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);
114 Assert.assertEquals(ps.toString().substring(128), s);