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 org.junit.After;
24 import org.junit.AfterClass;
25 import org.junit.Assert;
26 import org.junit.BeforeClass;
27 import org.junit.runner.RunWith;
29 import org.onap.dmaap.datarouter.provisioning.InternalServlet;
30 import org.onap.dmaap.datarouter.provisioning.beans.Parameters;
31 import org.powermock.api.mockito.PowerMockito;
32 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
33 import org.powermock.modules.junit4.PowerMockRunner;
35 import javax.persistence.EntityManager;
36 import javax.persistence.EntityManagerFactory;
37 import javax.persistence.Persistence;
39 import java.io.FileWriter;
40 import java.io.IOException;
44 import static org.junit.Assert.assertEquals;
45 import static org.junit.Assert.assertFalse;
47 import org.junit.Test;
48 import java.util.HashMap;
51 @RunWith(PowerMockRunner.class)
52 @SuppressStaticInitializationFor("org.onap.dmaap.datarouter.provisioning.beans.Parameters")
53 public class LogfileLoaderTest {
55 private static EntityManagerFactory emf;
56 private static EntityManager em;
57 private LogfileLoader lfl = LogfileLoader.getLoader();
61 public static void init() {
62 emf = Persistence.createEntityManagerFactory("dr-unit-tests");
63 em = emf.createEntityManager();
65 "org.onap.dmaap.datarouter.provserver.properties",
66 "src/test/resources/h2Database.properties");
67 InternalServlet internalServlet = new InternalServlet();
72 public static void tearDownClass() {
80 public void tearDown() {
82 new FileWriter("unit-test-logs/testFile1.txt").close();
83 }catch (IOException ioe){
84 System.out.println(ioe.getMessage());
90 public void Verify_Histogram_When_Request_Type_Post() {
91 String fileContent = "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";
92 lfl.process(prepFile(fileContent));
93 Map<Long, Long> expect = new HashMap<>();
94 expect.put(17772L,1L);
95 expect.put(29353L,1L);
96 Map<Long, Long> actual = lfl.getHistogram();
97 assertEquals(actual,expect);
102 public void Verify_File_Processing_when_Req_Type_LOG() {
103 String fileContent = "2018-08-29-10-10-10-543.|LOG|1|1|url/file123|method|1|1|type|1|128.0.0.9|user123|2|1|1|1|other|1";
104 int[] actual = lfl.process(prepFile(fileContent));
105 int[] expect = {0, 1};
106 Assert.assertArrayEquals(expect, actual);
111 public void Verify_File_Processing_when_Req_Type_EXP() {
112 String fileContent = "2018-08-29-10-10-10-543.|EXP|1|1|1|'url/file123'|method|ctype|3|other|4";
113 int[] actual = lfl.process(prepFile(fileContent));
114 int[] expect = {0, 1};
115 Assert.assertArrayEquals(expect, actual);
120 public void Verify_Records_Prune_When_Record_Count_Is_Less_Then_Threshold() {
121 String fileContent = "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";
122 lfl.process(prepFile(fileContent));
123 PowerMockito.mockStatic(Parameters.class);
124 PowerMockito.when(Parameters.getParameter(Parameters.PROV_LOG_RETENTION)).thenReturn(new Parameters(Parameters.PROV_LOG_RETENTION, "0"));
125 assertFalse(lfl.pruneRecords());
129 private File prepFile(String content){
130 File file1 = new File("unit-test-logs/testFile1.txt");
131 try (FileWriter fileWriter = new FileWriter(file1)) {
132 fileWriter.write(content);
133 }catch (IOException e){
134 System.out.println(e.getMessage());