package org.onap.dmaap.datarouter.provisioning.beans;
-import org.junit.*;
-import org.onap.dmaap.datarouter.provisioning.utils.DB;
-import org.onap.dmaap.datarouter.provisioning.utils.RLEBitSet;
-
-import javax.persistence.EntityManager;
-import javax.persistence.EntityManagerFactory;
-import javax.persistence.Persistence;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.ParseException;
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.Persistence;
+import org.junit.AfterClass;
+import org.junit.Assert;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.onap.dmaap.datarouter.provisioning.utils.ProvDbUtils;
+import org.onap.dmaap.datarouter.provisioning.utils.RLEBitSet;
public class LogRecordTest {
- private LogRecord logRecord;
private static EntityManagerFactory emf;
private static EntityManager em;
- private DB db;
- private static final String INSERT_SQL = "insert into LOG_RECORDS values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
- private PreparedStatement ps;
+ private static ProvDbUtils provDbUtils;
@BeforeClass
public static void init() {
emf = Persistence.createEntityManagerFactory("dr-unit-tests");
em = emf.createEntityManager();
System.setProperty(
- "org.onap.dmaap.datarouter.provserver.properties",
- "src/test/resources/h2Database.properties");
+ "org.onap.dmaap.datarouter.provserver.properties",
+ "src/test/resources/h2Database.properties");
}
@AfterClass
@Before
public void setUp() throws ParseException, SQLException {
- db = new DB();
- Connection conn = db.getConnection();
- ps = conn.prepareStatement(INSERT_SQL);
+ provDbUtils = ProvDbUtils.getInstance();
}
@Test
System.setOut(new PrintStream(outContent));
String[] rlebitset = {"0-1,2-2"};
LogRecord.printLogRecords(System.out, new RLEBitSet(rlebitset[0]));
- 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");
+ Assert.assertEquals("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", outContent.toString().substring(25));
}
private void setArgsLoadAndAssertEquals(String type, String s) throws ParseException, SQLException {
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"};
- logRecord = new LogRecord(args);
- logRecord.load(ps);
- String compare_string = ps.toString().substring(ps.toString().indexOf("{1:"), ps.toString().indexOf("2:")) + ps.toString().substring(ps.toString().indexOf("3:"));
- Assert.assertEquals(compare_string, s);
+ LogRecord logRecord = new LogRecord(args);
+ String compare_string;
+ try (Connection conn = provDbUtils.getConnection(); PreparedStatement ps = conn.prepareStatement(
+ "insert into LOG_RECORDS values (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")) {
+ logRecord.load(ps);
+ compare_string = ps.toString().substring(ps.toString().indexOf("{1:"), ps.toString().indexOf(", 2:")) + ps.toString().substring(ps.toString().indexOf(", 3:"));
+ }
+ Assert.assertEquals(s, compare_string);
}
}