e24a9a3f3314ec5f62df34c3dd9daa6b0da30713
[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.*;
24 import org.junit.rules.TemporaryFolder;
25 import org.junit.runner.RunWith;
26
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;
32
33 import javax.persistence.EntityManager;
34 import javax.persistence.EntityManagerFactory;
35 import javax.persistence.Persistence;
36 import java.io.File;
37 import java.io.FileWriter;
38 import java.io.IOException;
39 import java.util.Map;
40
41
42 import static org.junit.Assert.assertTrue;
43
44 import org.junit.Test;
45
46 import java.util.HashMap;
47
48
49 @RunWith(PowerMockRunner.class)
50 @SuppressStaticInitializationFor("org.onap.dmaap.datarouter.provisioning.beans.Parameters")
51 public class LogfileLoaderTest {
52
53     private static EntityManagerFactory emf;
54     private static EntityManager em;
55     private LogfileLoader lfl = LogfileLoader.getLoader();
56
57
58     @Rule
59     public TemporaryFolder folder = new TemporaryFolder();
60
61
62     @BeforeClass
63     public static void init() {
64         emf = Persistence.createEntityManagerFactory("dr-unit-tests");
65         em = emf.createEntityManager();
66         System.setProperty(
67                 "org.onap.dmaap.datarouter.provserver.properties",
68                 "src/test/resources/h2Database.properties");
69         InternalServlet internalServlet = new InternalServlet();
70     }
71
72
73     @AfterClass
74     public static void tearDownClass() {
75         em.clear();
76         em.close();
77         emf.close();
78     }
79
80
81     @Test
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);
87     }
88
89
90     @Test
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);
96     }
97
98
99     @Test
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);
106     }
107
108
109     @Test
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));
118     }
119
120
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());
127         }
128         return file1;
129     }
130 }