Add test cases for 65% branch coverage
[dmaap/datarouter.git] / datarouter-node / src / test / java / org / onap / dmaap / datarouter / node / LogManagerTest.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.node;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNull;
26 import static org.junit.Assert.assertTrue;
27
28 import java.io.File;
29 import java.util.Timer;
30 import org.apache.commons.lang3.reflect.FieldUtils;
31 import org.junit.After;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.onap.dmaap.datarouter.node.LogManager.Uploader;
37 import org.powermock.api.mockito.PowerMockito;
38 import org.powermock.core.classloader.annotations.SuppressStaticInitializationFor;
39 import org.powermock.modules.junit4.PowerMockRunner;
40
41 @RunWith(PowerMockRunner.class)
42 @SuppressStaticInitializationFor({"org.onap.dmaap.datarouter.node.NodeConfigManager"})
43 public class LogManagerTest {
44
45     @Mock
46     private NodeConfigManager config;
47
48     private LogManager logManager;
49
50     @Before
51     public void setUp() throws IllegalAccessException {
52         mockNodeConfigManager();
53         FieldUtils.writeDeclaredStaticField(StatusLog.class, "config", config, true);
54         logManager = new LogManager(config);
55     }
56
57     @After
58     public void tearDown() {
59         File spoolDir = new File(System.getProperty("user.dir") + "/src/test/resources/.spool");
60         for (File file : spoolDir.listFiles()) {
61             if (file.exists()) {
62                 file.delete();
63             }
64         }
65         spoolDir.delete();
66     }
67
68     @Test
69     public void Verify_LogManager_Attempts_To_Deliver_Log_Files_To_Prov() {
70         logManager.run();
71         try {
72             Thread.sleep(1000);
73         } catch (Exception e) {
74             System.out.println("Exception caught: " + e.getMessage());
75         }
76         File file = new File(System.getProperty("user.dir") + "/src/test/resources/.spool/.lastqueued");
77         assertTrue(file.isFile());
78     }
79
80     @Test
81     public void Validate_Uploader_Getters() {
82         Uploader worker = logManager.getWorker();
83         assertEquals(10000L, worker.getInitFailureTimer());
84         assertEquals(600000L, worker.getWaitForFileProcessFailureTimer());
85         assertEquals(2.0, worker.getFailureBackoff(), 0.0);
86         assertEquals(150000L, worker.getMaxFailureTimer());
87         assertEquals(604800000L, worker.getExpirationTimer());
88         assertEquals(10000, worker.getFairFileLimit());
89         assertEquals(86400000, worker.getFairTimeLimit());
90         assertEquals("https://dmaap-dr-prov:8443/internal/logs",
91                 worker.getDestURL(new DestInfoBuilder().createDestInfo(), "String"));
92         assertFalse(worker.handleRedirection(new DestInfoBuilder().createDestInfo(), "", ""));
93         assertFalse(worker.isFollowRedirects());
94         assertNull(worker.getFeedId(""));
95     }
96
97     private void mockNodeConfigManager() {
98         PowerMockito.when(config.getLogDir()).thenReturn(System.getProperty("user.dir") + "/src/test/resources");
99         PowerMockito.when(config.getTimer()).thenReturn(new Timer("Node Configuration Timer", true));
100         PowerMockito.when(config.getEventLogPrefix())
101                 .thenReturn(System.getProperty("user.dir") + "/src/test/resources/events");
102         PowerMockito.when(config.getEventLogSuffix()).thenReturn(".log");
103         PowerMockito.when(config.getLogRetention()).thenReturn(94608000000L);
104         PowerMockito.when(config.getEventLogInterval()).thenReturn("30s");
105         PowerMockito.when(config.getPublishId()).thenReturn("123456789.dmaap-dr-node");
106         PowerMockito.when(config.getEventLogUrl()).thenReturn("https://dmaap-dr-prov:8443/internal/logs");
107     }
108
109 }