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;
24 import org.junit.rules.TemporaryFolder;
25 import org.junit.runner.RunWith;
27 import org.onap.dmaap.datarouter.provisioning.InternalServlet;
28 import org.onap.dmaap.datarouter.provisioning.beans.Parameters;
29 import org.powermock.api.mockito.PowerMockito;
30 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
31 import org.powermock.modules.junit4.PowerMockRunner;
33 import javax.persistence.EntityManager;
34 import javax.persistence.EntityManagerFactory;
35 import javax.persistence.Persistence;
37 import java.io.FileWriter;
38 import java.io.IOException;
42 import static org.junit.Assert.assertTrue;
44 import org.junit.Test;
46 import java.util.HashMap;
49 @RunWith(PowerMockRunner.class)
50 @SuppressStaticInitializationFor("org.onap.dmaap.datarouter.provisioning.beans.Parameters")
51 public class LogfileLoaderTest {
53 private static EntityManagerFactory emf;
54 private static EntityManager em;
55 private LogfileLoader lfl = LogfileLoader.getLoader();
59 public TemporaryFolder folder = new TemporaryFolder();
63 public static void init() {
64 emf = Persistence.createEntityManagerFactory("dr-unit-tests");
65 em = emf.createEntityManager();
67 "org.onap.dmaap.datarouter.provserver.properties",
68 "src/test/resources/h2Database.properties");
69 InternalServlet internalServlet = new InternalServlet();
74 public static void tearDownClass() {
82 public void Verify_File_Processing_when_Req_Type_LOG() throws IOException {
83 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";
84 int[] actual = lfl.process(prepFile(fileContent));
85 int[] expect = {0, 1};
86 Assert.assertArrayEquals(expect, actual);
91 public void Verify_File_Processing_when_Req_Type_EXP() throws IOException {
92 String fileContent = "2018-08-29-10-10-10-543.|EXP|1|1|1|'url/file123'|method|ctype|3|other|4";
93 int[] actual = lfl.process(prepFile(fileContent));
94 int[] expect = {0, 1};
95 Assert.assertArrayEquals(expect, actual);
100 public void Verify_Records_Prune_When_Record_Count_Is_Less_Then_Threshold() throws IOException{
101 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";
102 lfl.process(prepFile(fileContent));
103 PowerMockito.mockStatic(Parameters.class);
104 PowerMockito.when(Parameters.getParameter(Parameters.PROV_LOG_RETENTION)).thenReturn(new Parameters(Parameters.PROV_LOG_RETENTION, "0"));
105 Assert.assertEquals(lfl.pruneRecords(), false);
110 public void Verify_Histogram_When_Request_Type_Post() throws Exception {
111 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";
112 lfl.process(prepFile(fileContent));
113 Map<Long, Long> expect = new HashMap<>();
114 expect.put(17772L,2L);
115 expect.put(29353L,1L);
116 Map<Long, Long> actual = lfl.getHistogram();
117 assertTrue(actual.equals(expect));
121 private File prepFile(String content) throws IOException{
122 File file1 = folder.newFile("myfile1.txt");
123 try (FileWriter fileWriter = new FileWriter(file1)) {
124 fileWriter.write(content);
125 }catch (IOException e){
126 System.out.println(e.getMessage());