3376c922e423d65ba74393f8c86b53f9cf989011
[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.jaxrs;
18
19 import static org.testng.Assert.assertEquals;
20 import static org.testng.Assert.assertNull;
21
22 import java.util.function.Function;
23 import org.openecomp.sdc.logging.servlet.HttpHeader;
24 import org.testng.annotations.Test;
25
26 /**
27  * Unit tests mutliple-option headers.
28  *
29  * @author evitaliy
30  * @since 25 Mar 2018
31  */
32 public class HttpHeaderTest {
33
34     @Test
35     public void valueReturnedWhenSinglePossibleHeader() {
36
37         final String key = "Head";
38         final String value = "1234";
39
40         Function<String, String> reader = createReader(key, value);
41         HttpHeader header = new HttpHeader(key);
42         assertEquals(header.getAny(reader), value);
43     }
44
45     @Test
46     public void nullReturnedWhenSingleNoMatchingHeader() {
47
48         final String key = "Head";
49
50         Function<String, String> reader = createReader(key, null);
51         HttpHeader header = new HttpHeader(key);
52         assertNull(header.getAny(reader));
53     }
54
55     @Test
56     public void nullReturnedWhenNoneHeaderMatches() {
57         Function<String, String> reader = createReader("None", "Value");
58         HttpHeader header = new HttpHeader("A", "B", "C");
59         assertNull(header.getAny(reader));
60     }
61
62     @Test
63     public void valueReturnedWhenLastHeaderMatches() {
64
65         final String lastKey = "Last";
66         final String value = "1234";
67
68         Function<String, String> reader = createReader(lastKey, value);
69         HttpHeader header = new HttpHeader("First", "Second", lastKey);
70         assertEquals(header.getAny(reader), value);
71     }
72
73     @Test
74     public void valueReturnedWhenFirstHeaderMatches() {
75
76         final String firstKey = "First";
77         final String value = "1234";
78
79         Function<String, String> reader = createReader(firstKey, value);
80         HttpHeader header = new HttpHeader(firstKey, "Second", "Third");
81         assertEquals(header.getAny(reader), value);
82     }
83
84     @Test
85     public void valueReturnedWhenMiddleHeaderMatches() {
86
87         final String middleKey = "Second";
88         final String value = "1234";
89
90         Function<String, String> reader = createReader(middleKey, value);
91         HttpHeader header = new HttpHeader("First", middleKey, "Third");
92         assertEquals(header.getAny(reader), value);
93     }
94
95     private Function<String, String> createReader(String key, String value) {
96         return  h -> h.equals(key) ? value : null;
97     }
98 }