5cbd0c0d76bdf4b2ac28109c37eaa257588e80b0
[sdc.git] /
1 /*
2  * Copyright © 2016-2018 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.openecomp.sdc.logging.servlet;
18
19 import static junit.framework.TestCase.assertTrue;
20 import static org.junit.Assert.assertFalse;
21
22 import java.util.Arrays;
23 import java.util.Collections;
24 import java.util.List;
25 import java.util.function.Function;
26 import java.util.function.Supplier;
27 import org.junit.Test;
28
29 /**
30  * Unit tests multiple-option headers.
31  *
32  * @author evitaliy
33  * @since 31 Jul 2018
34  */
35 public class HttpHeaderTest {
36
37     private static final Supplier<? extends Throwable> VALUE_EXPECTED = () -> new AssertionError("Value expected");
38     private static final Function<String, String> NULL_WHEN_NAME_NOT_B = k -> "B".equals(k) ? "Value" : null;
39
40     @Test(expected = NullPointerException.class)
41     public void throwExceptionWhenInputArrayNull() {
42         new HttpHeader((String[]) null);
43     }
44
45     @Test(expected = NullPointerException.class)
46     public void throwExceptionWhenInputListNull() {
47         new HttpHeader((List<String>) null);
48     }
49
50     @Test(expected = IllegalArgumentException.class)
51     public void throwExceptionWhenInputArrayEmpty() {
52         new HttpHeader(new String[0]);
53     }
54
55     @Test(expected = IllegalArgumentException.class)
56     public void throwExceptionWhenInputListEmpty() {
57         new HttpHeader(Collections.emptyList());
58     }
59
60     @Test
61     public void valueNotReturnedWhenNameInArrayNotRequested() {
62         HttpHeader header = new HttpHeader(new String[] {"A"});
63         assertFalse(header.getAny(NULL_WHEN_NAME_NOT_B).isPresent());
64     }
65
66     @Test
67     public void valueNotReturnedWhenNameInListNotRequested() {
68         HttpHeader header = new HttpHeader(Collections.singletonList("A"));
69         assertFalse(header.getAny(NULL_WHEN_NAME_NOT_B).isPresent());
70     }
71
72     @Test
73     public void valueReturnedWhenSinglePossibleHeaderInArrayMatches() {
74         HttpHeader header = new HttpHeader(new String[] {"B"});
75         assertTrue(header.getAny(NULL_WHEN_NAME_NOT_B).isPresent());
76     }
77
78     @Test
79     public void valueReturnedWhenSinglePossibleHeaderInListMatches() {
80         HttpHeader header = new HttpHeader(Collections.singletonList("B"));
81         assertTrue(header.getAny(NULL_WHEN_NAME_NOT_B).isPresent());
82     }
83
84     @Test
85     public void valueReturnedWhenLastHeaderInArrayMatches() throws Throwable {
86         HttpHeader header = new HttpHeader(new String[] {"A", "B"});
87         header.getAny(NULL_WHEN_NAME_NOT_B).orElseThrow(VALUE_EXPECTED);
88     }
89
90     @Test
91     public void valueReturnedWhenLastHeaderInListMatches() throws Throwable {
92         HttpHeader header = new HttpHeader(Arrays.asList("A", "B"));
93         header.getAny(NULL_WHEN_NAME_NOT_B).orElseThrow(VALUE_EXPECTED);
94     }
95
96     @Test
97     public void valueReturnedWhenFirstHeaderInArrayMatches() throws Throwable {
98         HttpHeader header = new HttpHeader(new String[] {"B", "A"});
99         header.getAny(NULL_WHEN_NAME_NOT_B).orElseThrow(VALUE_EXPECTED);
100     }
101
102     @Test
103     public void valueReturnedWhenFirstHeaderInListMatches() throws Throwable {
104         HttpHeader header = new HttpHeader(Arrays.asList("B", "A"));
105         header.getAny(NULL_WHEN_NAME_NOT_B).orElseThrow(VALUE_EXPECTED);
106     }
107
108     @Test
109     public void valueReturnedWhenMiddleHeaderInArrayMatches() throws Throwable {
110         HttpHeader header = new HttpHeader(new String[] {"A", "B", "C"});
111         header.getAny(NULL_WHEN_NAME_NOT_B).orElseThrow(VALUE_EXPECTED);
112     }
113
114     @Test
115     public void valueReturnedWhenMiddleHeaderInListMatches() throws Throwable {
116         HttpHeader header = new HttpHeader(Arrays.asList("A", "B", "C"));
117         header.getAny(NULL_WHEN_NAME_NOT_B).orElseThrow(VALUE_EXPECTED);
118     }
119 }