Update 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.AfterClass;
24 import org.junit.Assert;
25 import org.junit.BeforeClass;
26 import org.junit.Rule;
27 import org.junit.rules.TemporaryFolder;
28 import org.junit.runner.RunWith;
29
30 import org.onap.dmaap.datarouter.provisioning.InternalServlet;
31 import org.onap.dmaap.datarouter.provisioning.beans.Parameters;
32 import org.powermock.api.mockito.PowerMockito;
33 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
34 import org.powermock.modules.junit4.PowerMockRunner;
35
36 import javax.persistence.EntityManager;
37 import javax.persistence.EntityManagerFactory;
38 import javax.persistence.Persistence;
39 import java.io.File;
40 import java.io.FileWriter;
41 import java.io.IOException;
42
43 import static org.junit.Assert.assertFalse;
44
45 import org.junit.Test;
46
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     @Rule
58     public TemporaryFolder folder = new TemporaryFolder();
59
60
61     @BeforeClass
62     public static void init() {
63         emf = Persistence.createEntityManagerFactory("dr-unit-tests");
64         em = emf.createEntityManager();
65         System.setProperty(
66                 "org.onap.dmaap.datarouter.provserver.properties",
67                 "src/test/resources/h2Database.properties");
68         InternalServlet internalServlet = new InternalServlet();
69     }
70
71
72     @AfterClass
73     public static void tearDownClass() {
74         em.clear();
75         em.close();
76         emf.close();
77     }
78
79
80     @Test
81     public void Verify_File_Processing_when_Req_Type_LOG() throws IOException {
82         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";
83         int[] actual = lfl.process(prepFile(fileContent, "file1"));
84         int[] expect = {0, 1};
85         Assert.assertArrayEquals(expect, actual);
86     }
87
88
89     @Test
90     public void Verify_File_Processing_when_Req_Type_EXP() throws IOException{
91         String fileContent = "2018-08-29-10-10-10-543.|EXP|1|1|1|'url/file123'|method|ctype|3|other|4";
92         int[] actual = lfl.process(prepFile(fileContent, "file2"));
93         int[] expect = {0, 1};
94         Assert.assertArrayEquals(expect, actual);
95     }
96
97
98     @Test
99     public void Verify_Records_Prune_When_Record_Count_Is_Less_Then_Threshold() throws IOException{
100         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";
101         lfl.process(prepFile(fileContent, "file3"));
102         PowerMockito.mockStatic(Parameters.class);
103         PowerMockito.when(Parameters.getParameter(Parameters.PROV_LOG_RETENTION)).thenReturn(new Parameters(Parameters.PROV_LOG_RETENTION, "0"));
104         assertFalse(lfl.pruneRecords());
105     }
106
107
108     private File prepFile(String content, String fileName) throws IOException{
109         File file1 = folder.newFile(fileName);
110         try (FileWriter fileWriter = new FileWriter(file1)) {
111             fileWriter.write(content);
112         }catch (IOException e){
113             System.out.println(e.getMessage());
114         }
115         return file1;
116     }
117 }