2 * Copyright © 2016-2018 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.logging.servlet;
19 import static junit.framework.TestCase.assertTrue;
20 import static org.junit.Assert.assertFalse;
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;
30 * Unit tests multiple-option headers.
35 public class HttpHeaderTest {
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;
40 @Test(expected = NullPointerException.class)
41 public void throwExceptionWhenInputArrayNull() {
42 new HttpHeader((String[]) null);
45 @Test(expected = NullPointerException.class)
46 public void throwExceptionWhenInputListNull() {
47 new HttpHeader((List<String>) null);
50 @Test(expected = IllegalArgumentException.class)
51 public void throwExceptionWhenInputArrayEmpty() {
52 new HttpHeader(new String[0]);
55 @Test(expected = IllegalArgumentException.class)
56 public void throwExceptionWhenInputListEmpty() {
57 new HttpHeader(Collections.emptyList());
61 public void valueNotReturnedWhenNameInArrayNotRequested() {
62 HttpHeader header = new HttpHeader(new String[] {"A"});
63 assertFalse(header.getAny(NULL_WHEN_NAME_NOT_B).isPresent());
67 public void valueNotReturnedWhenNameInListNotRequested() {
68 HttpHeader header = new HttpHeader(Collections.singletonList("A"));
69 assertFalse(header.getAny(NULL_WHEN_NAME_NOT_B).isPresent());
73 public void valueReturnedWhenSinglePossibleHeaderInArrayMatches() {
74 HttpHeader header = new HttpHeader(new String[] {"B"});
75 assertTrue(header.getAny(NULL_WHEN_NAME_NOT_B).isPresent());
79 public void valueReturnedWhenSinglePossibleHeaderInListMatches() {
80 HttpHeader header = new HttpHeader(Collections.singletonList("B"));
81 assertTrue(header.getAny(NULL_WHEN_NAME_NOT_B).isPresent());
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);
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);
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);
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);
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);
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);