[DMaaP DR] JKD 11 migration
[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.core.classloader.annotations.PowerMockIgnore;
37 import org.powermock.modules.junit4.PowerMockRunner;
38
39 @RunWith(PowerMockRunner.class)
40 @PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*"})
41 public class RedirManagerTest {
42
43     private RedirManager redirManager;
44     private String redirFilePath = System.getProperty("user.dir") + "/src/test/resources/redir_file";
45
46     @Before
47     public void setUp() {
48         Timer timer = new Timer("Node Configuration Timer", true);
49         redirManager = new RedirManager(redirFilePath, 10000L, timer);
50     }
51
52     @Test
53     public void Given_Lookup_On_Valid_Redirect_Returns_Target_URL() {
54         assertThat(redirManager.lookup("1", "http://destination:8443/path/to"), Is.is("http://redirect:8443/path/to"));
55     }
56
57     @Test
58     public void Given_IsRedirected_Called_On_Valid_Sub_Id_Then_Returns_True() {
59         assertThat(redirManager.isRedirected("1"), Is.is(true));
60     }
61
62     @Test
63     public void Given_Redirect_Called_On_Valid_Redirect_New_Redirect_Added() throws IOException {
64         long origFileLenght = new File(redirFilePath).length();
65         redirManager.redirect("3", "http://destination3:8443/path/to", "http://redirect3:8443/path/to");
66         assertThat(redirManager.lookup("3", "http://destination3:8443/path/to"), Is.is("http://redirect3:8443/path/to"));
67         new RandomAccessFile(redirFilePath, "rw").setLength(origFileLenght);
68     }
69
70     @Test
71     public void Given_Lookup_On_Invalid_Redirect_Returns_Primary_Target_URL_And_Is_Forgotten() throws IOException {
72         assertThat(redirManager.lookup("2", "http://invalid:8443/path/to"), Is.is("http://invalid:8443/path/to"));
73         Files.write(Paths.get(redirFilePath), "2 http://destination2:8443/path/to http://redirect2:8443/path/to".getBytes(), StandardOpenOption.APPEND);
74     }
75 }