/**
 * 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.v1.kafka;

import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.HashMap;
import java.util.Map;

import org.junit.jupiter.api.Test;

import io.cloudevents.v1.kafka.HeaderMapper;

/**
 * 
 * @author fabiojose
 *
 */
public class HeaderMapperTest {
	@Test
	public void error_when_attributes_map_isnull() {
		// setup 
		Map<String, String> extensions = new HashMap<>();
		
		assertThrows(NullPointerException.class, () -> {
			// act
			HeaderMapper.map(null, extensions);
		});
	}
	
	@Test
	public void error_when_extensions_map_isnull() {
		// setup 
		Map<String, String> attributes = new HashMap<>();
		
		assertThrows(NullPointerException.class, () -> {
			// act
			HeaderMapper.map(attributes, null);
		});
	}
	
	@Test
	public void should_not_map_null_attribute_value() {
		// setup
		Map<String, String> attributes = new HashMap<>();
		attributes.put("type", null);
		attributes.put("specversion", "1.0");
		
		Map<String, String> extensions = new HashMap<>();
		
		// act
		Map<String, byte[]> actual = HeaderMapper.map(attributes, extensions);
		
		//assert
		assertFalse(actual.containsKey("ce-type"));
	}
	
	@Test
	public void should_not_map_null_extension_value() {
		// setup
		Map<String, String> attributes = new HashMap<>();
		attributes.put("type", "mytype");
		attributes.put("specversion", "1.0");
		
		Map<String, String> extensions = new HashMap<>();
		extensions.put("null-ext", null);
		extensions.put("comexampleextension1", "value");
		
		// act
		Map<String, byte[]> actual = HeaderMapper.map(attributes, extensions);
		
		//assert
		assertFalse(actual.containsKey("null-ext"));
	}
	
	@Test
	public void should_not_map_absent_contenttype() {
		// setup
		Map<String, String> attributes = new HashMap<>();
		attributes.put("type", "mytype");
		attributes.put("specversion", "1.0");
		
		Map<String, String> extensions = new HashMap<>();
		extensions.put("null-ext", "null-value");
		extensions.put("comexampleextension1", "value");
		
		// act
		Map<String, byte[]> actual = HeaderMapper.map(attributes, extensions);
		
		//assert
		assertFalse(actual.containsKey("Content-Type"));	
	}
	
	@Test
	public void should_map_extension_without_prefix() {
		// setup
		Map<String, String> attributes = new HashMap<>();
		attributes.put("type", "mytype");
		attributes.put("specversion", "1.0");
		
		Map<String, String> extensions = new HashMap<>();
		extensions.put("null-ext", "null-value");
		extensions.put("comexampleextension1", "value");
		
		// act
		Map<String, byte[]> actual = HeaderMapper.map(attributes, extensions);
		
		//assert
		assertTrue(actual.containsKey("comexampleextension1"));	
	}
	
	@Test
	public void should_all_values_as_byte_array() {
		// setup
		Map<String, String> attributes = new HashMap<>();
		attributes.put("type", "mytype");
		attributes.put("specversion", "1.0");
		
		Map<String, String> extensions = new HashMap<>();
		extensions.put("null-ext", "null-value");
		extensions.put("comexampleextension1", "value");
		
		// act
		Map<String, byte[]> actuals = HeaderMapper.map(attributes, extensions);
		
		// assert
		actuals.values()
			.forEach(actual -> {
				assertTrue(actual instanceof byte[]);
			});
	}
}

@@ -631,16 +631,77 @@
 kafka;%0A%0A
+import static org.junit.jupiter.api.Assertions.assertEquals;%0A
 import s
@@ -958,16 +958,60 @@
 .Test;%0A%0A
+import io.cloudevents.v1.ContextAttributes;%0A
 import i
@@ -1985,32 +1985,527 @@
 ));%0A%09%7D%0A%09%0A%09@Test%0A
+%09public void should_map_datacontenttype_to_content_type() %7B%0A%09%09// setup%0A%09%09Map%3CString, String%3E attributes = new HashMap%3C%3E();%0A%09%09attributes.put(ContextAttributes.datacontenttype.name(), %22application/json%22);%0A%09%09%0A%09%09Map%3CString, String%3E extensions = new HashMap%3C%3E();%0A%09%09%0A%09%09// act%0A%09%09Map%3CString, byte%5B%5D%3E actual = HeaderMapper.map(attributes, extensions);%0A%09%09%0A%09%09//assert%0A%09%09assertTrue(actual.containsKey(%22content-type%22));%0A%09%09assertEquals(%22application/json%22, new String(actual.get(%22content-type%22)));%0A%09%7D%0A%09%0A%09@Test%0A
 %09public void sho

Test for kafka content type header mapping