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