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.SuppressStaticInitializationFor;
44 import org.powermock.modules.junit4.PowerMockRunner;
46 @RunWith(PowerMockRunner.class)
47 @SuppressStaticInitializationFor("org.onap.dmaap.datarouter.provisioning.beans.Parameters")
48 public class LogfileLoaderTest {
50 private static EntityManagerFactory emf;
51 private static EntityManager em;
52 private LogfileLoader lfl = LogfileLoader.getLoader();
56 public void setUp() throws Exception {
57 testLog = new File(System.getProperty("user.dir") + "/src/test/resources/IN.test_prov_logs");
62 public void tearDown() throws IOException {
63 Files.deleteIfExists(testLog.toPath());
67 public static void init() {
68 emf = Persistence.createEntityManagerFactory("dr-unit-tests");
69 em = emf.createEntityManager();
71 "org.onap.dmaap.datarouter.provserver.properties",
72 "src/test/resources/h2Database.properties");
73 InternalServlet internalServlet = new InternalServlet();
77 public static void tearDownClass() {
84 public void Verify_File_Processing_Returns_Expected_Array() {
85 int[] actual = lfl.process(testLog);
86 int[] expect = {5, 7};
87 Assert.assertArrayEquals(expect, actual);
88 Assert.assertNotNull(lfl.getBitSet());
89 Assert.assertTrue(lfl.isIdle());
93 public void Verify_Records_Prune_When_Record_Count_Is_Less_Then_Threshold() {
95 PowerMockito.mockStatic(Parameters.class);
96 PowerMockito.when(Parameters.getParameter(Parameters.PROV_LOG_RETENTION)).thenReturn(new Parameters(Parameters.PROV_LOG_RETENTION, "0"));
97 PowerMockito.when(Parameters.getParameter(Parameters.DEFAULT_LOG_RETENTION)).thenReturn(new Parameters(Parameters.DEFAULT_LOG_RETENTION, "1000000"));
98 assertFalse(lfl.pruneRecords());
102 public void Verify_Records_Prune_When_Record_Count_Is_Greater_Then_Threshold() {
103 lfl.process(testLog);
104 PowerMockito.mockStatic(Parameters.class);
105 PowerMockito.when(Parameters.getParameter(Parameters.PROV_LOG_RETENTION)).thenReturn(new Parameters(Parameters.PROV_LOG_RETENTION, "0"));
106 PowerMockito.when(Parameters.getParameter(Parameters.DEFAULT_LOG_RETENTION)).thenReturn(new Parameters(Parameters.DEFAULT_LOG_RETENTION, "1"));
107 assertTrue(lfl.pruneRecords());
111 private void prepFile(File logFile) {
112 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"
113 + "2018-08-29-10-10-10-543.|EXP|1|1|1|'url/file123'|PUT|null|3|new reason|4\n"
114 + "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"
115 + "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"
116 + "2018-08-29-10-10-10-543.|DLX|1|1|1|100|100\n"
117 + "2018-08-29-10-10-10-543.|Bad Record|||\n"
118 + "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";
119 try (FileWriter fileWriter = new FileWriter(logFile)) {
120 fileWriter.write(testLogs);
122 catch (IOException e){
123 System.out.println(e.getMessage());