Updates for LogfileLoader unit test
[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_File_Processing_when_Req_Type_LOG() {
91         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";
92         int[] actual = lfl.process(prepFile(fileContent));
93         int[] expect = {0, 1};
94         Assert.assertArrayEquals(expect, actual);
95     }
96
97
98     @Test
99     public void Verify_File_Processing_when_Req_Type_EXP() {
100         String fileContent = "2018-08-29-10-10-10-543.|EXP|1|1|1|'url/file123'|method|ctype|3|other|4";
101         int[] actual = lfl.process(prepFile(fileContent));
102         int[] expect = {0, 1};
103         Assert.assertArrayEquals(expect, actual);
104     }
105
106
107     @Test
108     public void Verify_Records_Prune_When_Record_Count_Is_Less_Then_Threshold() {
109         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";
110         lfl.process(prepFile(fileContent));
111         PowerMockito.mockStatic(Parameters.class);
112         PowerMockito.when(Parameters.getParameter(Parameters.PROV_LOG_RETENTION)).thenReturn(new Parameters(Parameters.PROV_LOG_RETENTION, "0"));
113         assertFalse(lfl.pruneRecords());
114     }
115
116
117     private File prepFile(String content){
118         File file1 = new File("unit-test-logs/testFile1.txt");
119         try (FileWriter fileWriter = new FileWriter(file1)) {
120             fileWriter.write(content);
121         }catch (IOException e){
122             System.out.println(e.getMessage());
123         }
124         return file1;
125     }
126 }