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