Even more unit test and code cleanup
[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 static junit.framework.TestCase.assertTrue;
24 import static org.junit.Assert.assertFalse;
25
26 import java.io.File;
27 import java.io.FileWriter;
28 import java.io.IOException;
29 import java.nio.file.Files;
30 import javax.persistence.EntityManager;
31 import javax.persistence.EntityManagerFactory;
32 import javax.persistence.Persistence;
33 import org.junit.After;
34 import org.junit.AfterClass;
35 import org.junit.Assert;
36 import org.junit.Before;
37 import org.junit.BeforeClass;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.onap.dmaap.datarouter.provisioning.InternalServlet;
41 import org.onap.dmaap.datarouter.provisioning.beans.Parameters;
42 import org.powermock.api.mockito.PowerMockito;
43 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
44 import org.powermock.modules.junit4.PowerMockRunner;
45
46 @RunWith(PowerMockRunner.class)
47 @SuppressStaticInitializationFor("org.onap.dmaap.datarouter.provisioning.beans.Parameters")
48 public class LogfileLoaderTest {
49
50     private static EntityManagerFactory emf;
51     private static EntityManager em;
52     private LogfileLoader lfl = LogfileLoader.getLoader();
53     private File testLog;
54
55     @Before
56     public void setUp() throws Exception {
57         testLog = new File(System.getProperty("user.dir") + "/src/test/resources/IN.test_prov_logs");
58         prepFile(testLog);
59     }
60
61     @After
62     public void tearDown() throws IOException {
63         Files.deleteIfExists(testLog.toPath());
64     }
65
66     @BeforeClass
67     public static void init() {
68         emf = Persistence.createEntityManagerFactory("dr-unit-tests");
69         em = emf.createEntityManager();
70         System.setProperty(
71                 "org.onap.dmaap.datarouter.provserver.properties",
72                 "src/test/resources/h2Database.properties");
73         InternalServlet internalServlet = new InternalServlet();
74     }
75
76     @AfterClass
77     public static void tearDownClass() {
78         em.clear();
79         em.close();
80         emf.close();
81     }
82
83     @Test
84     public void Verify_File_Processing_Returns_Expected_Array() {
85         int[] actual = lfl.process(testLog);
86         int[] expect = {5, 7};
87         Assert.assertArrayEquals(expect, actual);
88         Assert.assertNotNull(lfl.getBitSet());
89         Assert.assertTrue(lfl.isIdle());
90     }
91
92     @Test
93     public void Verify_Records_Prune_When_Record_Count_Is_Less_Then_Threshold() {
94         lfl.process(testLog);
95         PowerMockito.mockStatic(Parameters.class);
96         PowerMockito.when(Parameters.getParameter(Parameters.PROV_LOG_RETENTION)).thenReturn(new Parameters(Parameters.PROV_LOG_RETENTION, "0"));
97         PowerMockito.when(Parameters.getParameter(Parameters.DEFAULT_LOG_RETENTION)).thenReturn(new Parameters(Parameters.DEFAULT_LOG_RETENTION, "1000000"));
98         assertFalse(lfl.pruneRecords());
99     }
100
101     @Test
102     public void Verify_Records_Prune_When_Record_Count_Is_Greater_Then_Threshold() {
103         lfl.process(testLog);
104         PowerMockito.mockStatic(Parameters.class);
105         PowerMockito.when(Parameters.getParameter(Parameters.PROV_LOG_RETENTION)).thenReturn(new Parameters(Parameters.PROV_LOG_RETENTION, "0"));
106         PowerMockito.when(Parameters.getParameter(Parameters.DEFAULT_LOG_RETENTION)).thenReturn(new Parameters(Parameters.DEFAULT_LOG_RETENTION, "1"));
107         assertTrue(lfl.pruneRecords());
108     }
109
110
111     private void prepFile(File logFile) {
112         String testLogs =           "2018-08-29-10-10-10-543.|LOG|1|1|https://dmaap-dr-prov:/url/file123|POST|application/vnd.att-dr.feed|100|mockType|file123|https://dmaap-dr-prov|user123|200|1|1|200|2|2\n"
113                                   + "2018-08-29-10-10-10-543.|EXP|1|1|1|'url/file123'|PUT|null|3|new reason|4\n"
114                                   + "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\n"
115                                   + "2018-08-29-10-10-10-543.|PBF|1|1|https://dmaap-dr-prov:8443/publish/1/file123/|POST|application/vnd.att-dr.feed|100|100|128.0.0.9|user123|failed\n"
116                                   + "2018-08-29-10-10-10-543.|DLX|1|1|1|100|100\n"
117                                   + "2018-08-29-10-10-10-543.|Bad Record|||\n"
118                                   + "2018-08-29-10-10-10-543.|DEL|2|1|2|https://dmaap-dr-prov:8443/publish/1/file123/|PUT|application/vnd.att-dr.feed|100|user123|200|123456";
119         try (FileWriter fileWriter = new FileWriter(logFile)) {
120             fileWriter.write(testLogs);
121         }
122         catch (IOException e){
123             System.out.println(e.getMessage());
124         }
125     }
126 }