[DMaaP DR] JKD 11 migration
[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.PowerMockIgnore;
44 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
45 import org.powermock.modules.junit4.PowerMockRunner;
46
47 @RunWith(PowerMockRunner.class)
48 @SuppressStaticInitializationFor("org.onap.dmaap.datarouter.provisioning.beans.Parameters")
49 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*", "org.w3c.*"})
50 public class LogfileLoaderTest {
51
52     private static EntityManagerFactory emf;
53     private static EntityManager em;
54     private LogfileLoader lfl = LogfileLoader.getLoader();
55     private File testLog;
56
57     @Before
58     public void setUp() throws Exception {
59         testLog = new File(System.getProperty("user.dir") + "/src/test/resources/IN.test_prov_logs");
60         prepFile(testLog);
61     }
62
63     @After
64     public void tearDown() throws IOException {
65         Files.deleteIfExists(testLog.toPath());
66     }
67
68     @BeforeClass
69     public static void init() {
70         emf = Persistence.createEntityManagerFactory("dr-unit-tests");
71         em = emf.createEntityManager();
72         System.setProperty(
73                 "org.onap.dmaap.datarouter.provserver.properties",
74                 "src/test/resources/h2Database.properties");
75         InternalServlet internalServlet = new InternalServlet();
76     }
77
78     @AfterClass
79     public static void tearDownClass() {
80         em.clear();
81         em.close();
82         emf.close();
83     }
84
85     @Test
86     public void Verify_File_Processing_Returns_Expected_Array() {
87         int[] actual = lfl.process(testLog);
88         int[] expect = {5, 7};
89         Assert.assertArrayEquals(expect, actual);
90         Assert.assertNotNull(lfl.getBitSet());
91         Assert.assertTrue(lfl.isIdle());
92     }
93
94     @Test
95     public void Verify_Records_Prune_When_Record_Count_Is_Less_Then_Threshold() {
96         lfl.process(testLog);
97         PowerMockito.mockStatic(Parameters.class);
98         PowerMockito.when(Parameters.getParameter(Parameters.PROV_LOG_RETENTION)).thenReturn(new Parameters(Parameters.PROV_LOG_RETENTION, "0"));
99         PowerMockito.when(Parameters.getParameter(Parameters.DEFAULT_LOG_RETENTION)).thenReturn(new Parameters(Parameters.DEFAULT_LOG_RETENTION, "1000000"));
100         assertFalse(lfl.pruneRecords());
101     }
102
103     @Test
104     public void Verify_Records_Prune_When_Record_Count_Is_Greater_Then_Threshold() {
105         lfl.process(testLog);
106         PowerMockito.mockStatic(Parameters.class);
107         PowerMockito.when(Parameters.getParameter(Parameters.PROV_LOG_RETENTION)).thenReturn(new Parameters(Parameters.PROV_LOG_RETENTION, "0"));
108         PowerMockito.when(Parameters.getParameter(Parameters.DEFAULT_LOG_RETENTION)).thenReturn(new Parameters(Parameters.DEFAULT_LOG_RETENTION, "1"));
109         assertTrue(lfl.pruneRecords());
110     }
111
112
113     private void prepFile(File logFile) {
114         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"
115                                   + "2018-08-29-10-10-10-543.|EXP|1|1|1|'url/file123'|PUT|null|3|new reason|4\n"
116                                   + "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"
117                                   + "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"
118                                   + "2018-08-29-10-10-10-543.|DLX|1|1|1|100|100\n"
119                                   + "2018-08-29-10-10-10-543.|Bad Record|||\n"
120                                   + "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";
121         try (FileWriter fileWriter = new FileWriter(logFile)) {
122             fileWriter.write(testLogs);
123         }
124         catch (IOException e){
125             System.out.println(e.getMessage());
126         }
127     }
128 }