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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.dmaap.datarouter.node;
23 import static org.junit.Assert.assertThat;
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;
38 @RunWith(PowerMockRunner.class)
39 public class RedirManagerTest {
41 private RedirManager redirManager;
42 private String redirFilePath = System.getProperty("user.dir") + "/src/test/resources/redir_file";
46 Timer timer = new Timer("Node Configuration Timer", true);
47 redirManager = new RedirManager(redirFilePath, 10000L, timer);
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"));
56 public void Given_IsRedirected_Called_On_Valid_Sub_Id_Then_Returns_True() {
57 assertThat(redirManager.isRedirected("1"), Is.is(true));
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);
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);