aboutsummaryrefslogtreecommitdiffstats
path: root/datarouter-node/src/test/java/org/onap/dmaap/datarouter/node/RedirManagerTest.java
blob: 87abe84e28c447e3e1f232cd09cd9e42f28d6c0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2019 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.dmaap.datarouter.node;

import static org.junit.Assert.assertThat;

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Timer;
import org.hamcrest.core.Is;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.powermock.core.classloader.annotations.PowerMockIgnore;
import org.powermock.modules.junit4.PowerMockRunner;

@RunWith(PowerMockRunner.class)
@PowerMockIgnore({"com.sun.org.apache.xerces.*", "javax.xml.*", "org.xml.*"})
public class RedirManagerTest {

    private RedirManager redirManager;
    private String redirFilePath = System.getProperty("user.dir") + "/src/test/resources/redir_file";

    @Before
    public void setUp() {
        Timer timer = new Timer("Node Configuration Timer", true);
        redirManager = new RedirManager(redirFilePath, 10000L, timer);
    }

    @Test
    public void Given_Lookup_On_Valid_Redirect_Returns_Target_URL() {
        assertThat(redirManager.lookup("1", "http://destination:8443/path/to"), Is.is("http://redirect:8443/path/to"));
    }

    @Test
    public void Given_IsRedirected_Called_On_Valid_Sub_Id_Then_Returns_True() {
        assertThat(redirManager.isRedirected("1"), Is.is(true));
    }

    @Test
    public void Given_Redirect_Called_On_Valid_Redirect_New_Redirect_Added() throws IOException {
        long origFileLenght = new File(redirFilePath).length();
        redirManager.redirect("3", "http://destination3:8443/path/to", "http://redirect3:8443/path/to");
        assertThat(redirManager.lookup("3", "http://destination3:8443/path/to"), Is.is("http://redirect3:8443/path/to"));
        new RandomAccessFile(redirFilePath, "rw").setLength(origFileLenght);
    }

    @Test
    public void Given_Lookup_On_Invalid_Redirect_Returns_Primary_Target_URL_And_Is_Forgotten() throws IOException {
        assertThat(redirManager.lookup("2", "http://invalid:8443/path/to"), Is.is("http://invalid:8443/path/to"));
        Files.write(Paths.get(redirFilePath), "2 http://destination2:8443/path/to http://redirect2:8443/path/to".getBytes(), StandardOpenOption.APPEND);
    }
}