Adding more DR-Node unit tests
[dmaap/datarouter.git] / datarouter-node / src / test / java / org / onap / dmaap / datarouter / node / RedirManagerTest.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.assertThat;
24
25 import java.io.File;
26 import java.io.IOException;
27 import java.io.RandomAccessFile;
28 import java.nio.file.Files;
29 import java.nio.file.Paths;
30 import java.nio.file.StandardOpenOption;
31 import java.util.Timer;
32 import org.hamcrest.core.Is;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.powermock.modules.junit4.PowerMockRunner;
37
38 @RunWith(PowerMockRunner.class)
39 public class RedirManagerTest {
40
41     private RedirManager redirManager;
42     private String redirFilePath = System.getProperty("user.dir") + "/src/test/resources/redir_file";
43
44     @Before
45     public void setUp() {
46         Timer timer = new Timer("Node Configuration Timer", true);
47         redirManager = new RedirManager(redirFilePath, 10000L, timer);
48     }
49
50     @Test
51     public void Given_Lookup_On_Valid_Redirect_Returns_Target_URL() {
52         assertThat(redirManager.lookup("1", "http://destination:8443/path/to"), Is.is("http://redirect:8443/path/to"));
53     }
54
55     @Test
56     public void Given_IsRedirected_Called_On_Valid_Sub_Id_Then_Returns_True() {
57         assertThat(redirManager.isRedirected("1"), Is.is(true));
58     }
59
60     @Test
61     public void Given_Redirect_Called_On_Valid_Redirect_New_Redirect_Added() throws IOException {
62         long origFileLenght = new File(redirFilePath).length();
63         redirManager.redirect("3", "http://destination3:8443/path/to", "http://redirect3:8443/path/to");
64         assertThat(redirManager.lookup("3", "http://destination3:8443/path/to"), Is.is("http://redirect3:8443/path/to"));
65         new RandomAccessFile(redirFilePath, "rw").setLength(origFileLenght);
66     }
67
68     @Test
69     public void Given_Lookup_On_Invalid_Redirect_Returns_Primary_Target_URL_And_Is_Forgotten() throws IOException {
70         assertThat(redirManager.lookup("2", "http://invalid:8443/path/to"), Is.is("http://invalid:8443/path/to"));
71         Files.write(Paths.get(redirFilePath), "2 http://destination2:8443/path/to http://redirect2:8443/path/to".getBytes(), StandardOpenOption.APPEND);
72     }
73 }