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