Updates for LogfileLoaderTest.java
[dmaap/datarouter.git] / datarouter-prov / src / test / java / org / onap / dmaap / datarouter / provisioning / utils / LogfileLoaderTest.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.dmaap.datarouter.provisioning.utils;
22
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;
28
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;
34
35 import javax.persistence.EntityManager;
36 import javax.persistence.EntityManagerFactory;
37 import javax.persistence.Persistence;
38 import java.io.File;
39 import java.io.FileWriter;
40 import java.io.IOException;
41 import java.util.Map;
42
43
44 import static org.junit.Assert.assertEquals;
45 import static org.junit.Assert.assertFalse;
46
47 import org.junit.Test;
48 import java.util.HashMap;
49
50
51 @RunWith(PowerMockRunner.class)
52 @SuppressStaticInitializationFor("org.onap.dmaap.datarouter.provisioning.beans.Parameters")
53 public class LogfileLoaderTest {
54
55     private static EntityManagerFactory emf;
56     private static EntityManager em;
57     private LogfileLoader lfl = LogfileLoader.getLoader();
58
59
60     @BeforeClass
61     public static void init() {
62         emf = Persistence.createEntityManagerFactory("dr-unit-tests");
63         em = emf.createEntityManager();
64         System.setProperty(
65                 "org.onap.dmaap.datarouter.provserver.properties",
66                 "src/test/resources/h2Database.properties");
67         InternalServlet internalServlet = new InternalServlet();
68     }
69
70
71     @AfterClass
72     public static void tearDownClass() {
73         em.clear();
74         em.close();
75         emf.close();
76     }
77
78
79     @After
80     public void tearDown() {
81         try {
82             new FileWriter("unit-test-logs/testFile1.txt").close();
83         }catch (IOException ioe){
84             System.out.println(ioe.getMessage());
85         }
86     }
87
88
89     @Test
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);
98     }
99
100
101     @Test
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);
107     }
108
109
110     @Test
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);
116     }
117
118
119     @Test
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());
126     }
127
128
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());
135         }
136         return file1;
137     }
138 }