Tests for attributer mapper
Spec 0.3 HTTP Handle on null values Signed-off-by: Fabio José <fabiojose@gmail.com>
This commit is contained in:
parent
8608be402a
commit
b65f91120a
|
@ -20,40 +20,46 @@ import static java.util.stream.Collectors.toMap;
|
|||
import java.util.Locale;
|
||||
import java.util.Map;
|
||||
import java.util.Objects;
|
||||
import java.util.Optional;
|
||||
import java.util.AbstractMap.SimpleEntry;
|
||||
import java.util.Map.Entry;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
|
||||
import io.cloudevents.fun.BinaryFormatAttributeMapper;
|
||||
import io.cloudevents.v03.ContextAttributes;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author fabiojose
|
||||
*
|
||||
* @version 0.3
|
||||
*/
|
||||
public class AttributeMapper {
|
||||
private AttributeMapper() {}
|
||||
|
||||
static final String HEADER_PREFIX = "ce-";
|
||||
|
||||
/**
|
||||
* Following the signature of {@link BinaryFormatAttributeMapper#map(Map)}
|
||||
* @param headers Map of HTTP request
|
||||
* @return Map with spec attributes and values without parsing
|
||||
* @see ContextAttributes
|
||||
*/
|
||||
public static Map<String, String> map(final Map<String, Object> headers) {
|
||||
Objects.requireNonNull(headers);
|
||||
|
||||
headers.keySet()
|
||||
.stream()
|
||||
.map(header -> header.toLowerCase(Locale.US))
|
||||
.filter(header -> header.startsWith(HEADER_PREFIX))
|
||||
.map(header -> header.substring(HEADER_PREFIX.length()));
|
||||
|
||||
final AtomicReference<Entry<String, Object>> ct =
|
||||
final AtomicReference<Optional<Entry<String, Object>>> ct =
|
||||
new AtomicReference<>();
|
||||
|
||||
ct.set(Optional.empty());
|
||||
|
||||
Map<String, String> result = headers.entrySet()
|
||||
.stream()
|
||||
.filter(header -> null!= header.getValue())
|
||||
.map(header -> new SimpleEntry<>(header.getKey()
|
||||
.toLowerCase(Locale.US), header.getValue()))
|
||||
.peek(header -> {
|
||||
if("content-type".equals(header.getKey())) {
|
||||
ct.set(header);
|
||||
ct.set(Optional.ofNullable(header));
|
||||
}
|
||||
})
|
||||
.filter(header -> header.getKey().startsWith(HEADER_PREFIX))
|
||||
|
@ -63,8 +69,10 @@ public class AttributeMapper {
|
|||
header.getValue().toString()))
|
||||
.collect(toMap(Entry::getKey, Entry::getValue));
|
||||
|
||||
result.put(ContextAttributes.datacontenttype.name(),
|
||||
ct.get().getValue().toString());
|
||||
ct.get().ifPresent(contentType -> {
|
||||
result.put(ContextAttributes.datacontenttype.name(),
|
||||
contentType.getValue().toString());
|
||||
});
|
||||
|
||||
return result;
|
||||
}
|
||||
|
|
|
@ -0,0 +1,114 @@
|
|||
/**
|
||||
* Copyright 2019 The CloudEvents Authors
|
||||
*
|
||||
* 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.
|
||||
*/
|
||||
package io.cloudevents.v03.http;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
|
||||
import io.cloudevents.v03.http.AttributeMapper;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author fabiojose
|
||||
*
|
||||
*/
|
||||
public class AttributeMapperTest {
|
||||
@Rule
|
||||
public ExpectedException expectedEx = ExpectedException.none();
|
||||
|
||||
@Test
|
||||
public void error_when_headers_map_isnull() {
|
||||
// setup
|
||||
expectedEx.expect(NullPointerException.class);
|
||||
|
||||
// act
|
||||
AttributeMapper.map(null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_not_map_null_value() {
|
||||
// setup
|
||||
Map<String, Object> headers = new HashMap<>();
|
||||
headers.put("ce-type", null);
|
||||
|
||||
String expected = "type";
|
||||
|
||||
// act
|
||||
Map<String, String> attributes =
|
||||
AttributeMapper.map(headers);
|
||||
|
||||
// assert
|
||||
assertFalse(attributes.containsKey(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_ok_when_no_content_type() {
|
||||
// setup
|
||||
Map<String, Object> headers = new HashMap<>();
|
||||
headers.put("ce-specversion", "0.3");
|
||||
|
||||
// act
|
||||
Map<String, String> attributes =
|
||||
AttributeMapper.map(headers);
|
||||
|
||||
// assert
|
||||
assertFalse(attributes.isEmpty());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_map_cespecversion_to_specversion() {
|
||||
// setup
|
||||
Map<String, Object> headers = new HashMap<>();
|
||||
headers.put("ce-specversion", "0.3");
|
||||
headers.put("Content-Type", "application/json");
|
||||
|
||||
String expected = "specversion";
|
||||
|
||||
// act
|
||||
Map<String, String> attributes =
|
||||
AttributeMapper.map(headers);
|
||||
|
||||
// assert
|
||||
assertNotNull(attributes.get(expected));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_all_without_prefix_ce() {
|
||||
// setup
|
||||
Map<String, Object> myHeaders = new HashMap<>();
|
||||
myHeaders.put("ce-id", "0x11");
|
||||
myHeaders.put("ce-source", "/source");
|
||||
myHeaders.put("ce-specversion", "0.3");
|
||||
myHeaders.put("ce-type", "br.my");
|
||||
myHeaders.put("ce-time", "2019-09-16T20:49:00Z");
|
||||
myHeaders.put("ce-schemaurl", "http://my.br");
|
||||
myHeaders.put("Content-Type", "application/json");
|
||||
|
||||
Map<String, String> actual = AttributeMapper.map(myHeaders);
|
||||
|
||||
actual.keySet()
|
||||
.forEach((attribute) -> {
|
||||
assertFalse(attribute.startsWith("ce-"));
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue