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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.dmaap.datarouter.provisioning.utils;
23 import static junit.framework.TestCase.assertTrue;
24 import static org.junit.Assert.assertFalse;
27 import java.io.FileWriter;
28 import java.io.IOException;
29 import java.nio.file.Files;
30 import javax.persistence.EntityManager;
31 import javax.persistence.EntityManagerFactory;
32 import javax.persistence.Persistence;
33 import org.junit.After;
34 import org.junit.AfterClass;
35 import org.junit.Assert;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.onap.dmaap.datarouter.provisioning.InternalServlet;
41 import org.onap.dmaap.datarouter.provisioning.beans.Parameters;
42 import org.powermock.api.mockito.PowerMockito;
43 import org.powermock.core.classloader.annotations.PowerMockIgnore;
44 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
45 import org.powermock.modules.junit4.PowerMockRunner;
47 @RunWith(PowerMockRunner.class)
48 @SuppressStaticInitializationFor("org.onap.dmaap.datarouter.provisioning.beans.Parameters")
49 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})
50 public class LogfileLoaderTest {
52 private static EntityManagerFactory emf;
53 private static EntityManager em;
54 private LogfileLoader lfl = LogfileLoader.getLoader();
58 public void setUp() throws Exception {
59 testLog = new File(System.getProperty("user.dir") + "/src/test/resources/IN.test_prov_logs");
64 public void tearDown() throws IOException {
65 Files.deleteIfExists(testLog.toPath());
69 public static void init() {
70 emf = Persistence.createEntityManagerFactory("dr-unit-tests");
71 em = emf.createEntityManager();
73 "org.onap.dmaap.datarouter.provserver.properties",
74 "src/test/resources/h2Database.properties");
75 InternalServlet internalServlet = new InternalServlet();
79 public static void tearDownClass() {
86 public void Verify_File_Processing_Returns_Expected_Array() {
87 int[] actual = lfl.process(testLog);
88 int[] expect = {5, 7};
89 Assert.assertArrayEquals(expect, actual);
90 Assert.assertNotNull(lfl.getBitSet());
91 Assert.assertTrue(lfl.isIdle());
95 public void Verify_Records_Prune_When_Record_Count_Is_Less_Then_Threshold() {
97 PowerMockito.mockStatic(Parameters.class);
98 PowerMockito.when(Parameters.getParameter(Parameters.PROV_LOG_RETENTION)).thenReturn(new Parameters(Parameters.PROV_LOG_RETENTION, "0"));
99 PowerMockito.when(Parameters.getParameter(Parameters.DEFAULT_LOG_RETENTION)).thenReturn(new Parameters(Parameters.DEFAULT_LOG_RETENTION, "1000000"));
100 assertFalse(lfl.pruneRecords());
103 private void prepFile(File logFile) {
104 String testLogs = "2018-08-29-10-10-10-543.|LOG|1|1|https://dmaap-dr-prov:/url/file123|POST|application/vnd.att-dr.feed|100|mockType|file123|https://dmaap-dr-prov|user123|200|1|1|200|2|2\n"
105 + "2018-08-29-10-10-10-543.|EXP|1|1|1|'url/file123'|PUT|null|3|new reason|4\n"
106 + "2018-08-29-10-10-10-543.|PUB|1|1|https://dmaap-dr-prov:8443/publish/1/file123/|POST|application/vnd.att-dr.feed|2|128.0.0.9|user123|200\n"
107 + "2018-08-29-10-10-10-543.|PBF|1|1|https://dmaap-dr-prov:8443/publish/1/file123/|POST|application/vnd.att-dr.feed|100|100|128.0.0.9|user123|failed\n"
108 + "2018-08-29-10-10-10-543.|DLX|1|1|1|100|100\n"
109 + "2018-08-29-10-10-10-543.|Bad Record|||\n"
110 + "2018-08-29-10-10-10-543.|DEL|2|1|2|https://dmaap-dr-prov:8443/publish/1/file123/|PUT|application/vnd.att-dr.feed|100|user123|200|123456";
111 try (FileWriter fileWriter = new FileWriter(logFile)) {
112 fileWriter.write(testLogs);
114 catch (IOException e){
115 System.out.println(e.getMessage());