/**
 * 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.v02.http;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

import java.net.URI;
import java.util.HashMap;
import java.util.Map;

import org.junit.Test;

import io.cloudevents.CloudEvent;
import io.cloudevents.json.types.Much;
import io.cloudevents.v02.AttributesImpl;

/**
 * 
 * @author fabiojose
 *
 */
public class HTTPBinaryUnmarshallerTest {

	@Test
	public void should_unmarshal_headers_and_json_payload() {
		// setup
		Much expected = new Much();
		expected.setWow("yes!");
		
		Map<String, Object> myHeaders = new HashMap<>();
    	myHeaders.put("ce-id", "0x11");
		myHeaders.put("ce-source", "/source");
		myHeaders.put("ce-specversion", "0.2");
		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");
		
		String payload = "{\"wow\":\"yes!\"}";
		
		// act
		CloudEvent<AttributesImpl, Much> actual = 
			Unmarshallers.binary(Much.class)
				.withHeaders(() -> myHeaders)
				.withPayload(() -> payload)
				.unmarshal();
		
		// assert
		assertEquals("0x11", actual.getAttributes().getId());
		assertEquals(URI.create("/source"), actual.getAttributes().getSource());
		assertEquals("0.2", actual.getAttributes().getSpecversion());
		assertEquals("br.my", actual.getAttributes().getType());
		assertTrue(actual.getAttributes().getTime().isPresent());
		assertTrue(actual.getAttributes().getSchemaurl().isPresent());
		assertEquals(URI.create("http://my.br"), actual.getAttributes().getSchemaurl().get());
		assertTrue(actual.getAttributes().getContenttype().isPresent());
		assertEquals("application/json", actual.getAttributes().getContenttype().get());
		assertTrue(actual.getData().isPresent());
		assertEquals(expected, actual.getData().get());
	}
}

@@ -676,16 +676,62 @@
 Equals;%0A
+import static org.junit.Assert.assertNotNull;%0A
 import s
@@ -894,16 +894,78 @@
 dEvent;%0A
+import io.cloudevents.extensions.DistributedTracingExtension;%0A
 import i
@@ -2567,10 +2567,1080 @@
 ());%0A%09%7D%0A
+%09%0A%09@Test%0A%09public void should_unmarshal_tracing_extension_from_header() %7B%0A%09%09// setup%0A%09%09Much expected = new Much();%0A%09%09expected.setWow(%22yes!%22);%0A%09%09%0A%09%09Map%3CString, Object%3E myHeaders = new HashMap%3C%3E();%0A    %09myHeaders.put(%22ce-id%22, %220x11%22);%0A%09%09myHeaders.put(%22ce-source%22, %22/source%22);%0A%09%09myHeaders.put(%22ce-specversion%22, %220.2%22);%0A%09%09myHeaders.put(%22ce-type%22, %22br.my%22);%0A%09%09myHeaders.put(%22ce-time%22, %222019-09-16T20:49:00Z%22);%0A%09%09myHeaders.put(%22ce-schemaurl%22, %22http://my.br%22);%0A%09%09myHeaders.put(%22Content-Type%22, %22application/json%22);%0A%09%09%0A%09%09myHeaders.put(%22traceparent%22, %220x200%22);%0A%09%09myHeaders.put(%22tracestate%22, %22congo=9%22);%0A%09%09%0A%09%09String payload = %22%7B%5C%22wow%5C%22:%5C%22yes!%5C%22%7D%22;%0A%09%09%0A%09%09// act%0A%09%09CloudEvent%3CAttributesImpl, Much%3E actual = %0A%09%09%09Unmarshallers.binary(Much.class)%0A%09%09%09%09.withHeaders(() -%3E myHeaders)%0A%09%09%09%09.withPayload(() -%3E payload)%0A%09%09%09%09.unmarshal();%0A%09%09%0A%09%09// assert%0A%09%09assertNotNull(actual.getExtensions()%0A%09%09%09.get(DistributedTracingExtension.Format.IN_MEMORY_KEY));%0A%09%09assertTrue(actual.getExtensions()%0A%09%09%09.get(DistributedTracingExtension.Format.IN_MEMORY_KEY) %0A%09%09%09%09instanceof DistributedTracingExtension);%0A%09%7D%0A
 %7D%0A

Test the extensions unmarshal from headers