/*
    Copyright 2015, Strategic Gains, Inc.

	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 org.restexpress.plugin.correlationid;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import io.netty.handler.codec.http.HttpMethod;

import java.util.UUID;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.restexpress.Request;
import org.restexpress.Response;
import org.restexpress.RestExpress;

/**
 * @author tfredrich
 * @since Jul 25, 2015
 */
public class CorrelationIdPluginTest
{
	private static final RestExpress SERVER = new RestExpress();

	private HttpClient http = new DefaultHttpClient();

	@BeforeClass
	public static void beforeClass()
	{
		SERVER.registerPlugin(new CorrelationIdPlugin());
		SERVER.uri("/test", new TestController())
			.method(HttpMethod.GET)
			.noSerialization();
		SERVER.bind();
	}

	@AfterClass
	public static void afterClass()
	{
		SERVER.shutdown(false);
	}

	@Test
	public void shouldGenerateCorrelationIdOnNull()
	throws Exception
	{
		HttpGet request = new HttpGet("http://localhost:8081/test");
		HttpResponse response = (HttpResponse) http.execute(request);
		HttpEntity entity = response.getEntity();
		String json = EntityUtils.toString(entity);
		assertNotNull(json);
		UUID.fromString(json);
	}

	// Generation on empty causes problems, since the request already has a value for Correlation-Id in this case.
	// The method call request.addHeader() only adds ANOTHER header to the request and doesn't fix the problem.
	// Currently then, the plugin only adds the header if it doesn't exist, assuming that if a client sets the
	// header to ANYTHING, that's the correlation ID they desired.
//	@Test
//	public void shouldGenerateCorrelationIdOnEmpty()
//	throws Exception
//	{
//		HttpGet request = new HttpGet("http://localhost:8081/test");
//		request.setHeader(CorrelationIdPlugin.CORRELATION_ID, "");
//		HttpResponse response = (HttpResponse) http.execute(request);
//		HttpEntity entity = response.getEntity();
//		String json = EntityUtils.toString(entity);
//		assertNotNull(json);
//		UUID.fromString(json);
//	}

	@Test
	public void shouldUseExistingCorrelationId()
	throws Exception
	{
		String myCorrelationId = "myArbitrarilyAssignedCorrelationId";
		HttpGet request = new HttpGet("http://localhost:8081/test");
		request.setHeader(CorrelationIdPlugin.CORRELATION_ID, myCorrelationId);
		HttpResponse response = (HttpResponse) http.execute(request);
		HttpEntity entity = response.getEntity();
		String json = EntityUtils.toString(entity);
		assertNotNull(json);
		assertEquals(myCorrelationId, json);
	}

	private static class TestController
	{
		@SuppressWarnings("unused")
        public String read(Request req, Response resp)
		{
			return req.getHeader(CorrelationIdPlugin.CORRELATION_ID);
		}
	}
}

@@ -782,16 +782,47 @@
 .UUID;%0A%0A
+import org.apache.http.Header;%0A
 import o
@@ -3620,11 +3620,552 @@
 ;%0A%09%09%7D%0A%09%7D
+%0A%0A%09@Test%0A%09public void shouldSetCorrelationIdToResponse()%0A%09throws Exception%0A%09%7B%0A%09%09String myCorrelationId = %22myArbitrarilyAssignedCorrelationId%22;%0A%09%09HttpGet request = new HttpGet(%22http://localhost:8081/test%22);%0A%09%09request.addHeader(CorrelationIdPlugin.CORRELATION_ID, myCorrelationId);%0A%09%09HttpResponse response = (HttpResponse) http.execute(request);%0A%09%09Header%5B%5D headers = response.getHeaders(CorrelationIdPlugin.CORRELATION_ID);%0A%09%09assertEquals(1, headers.length);%0A%09%09String header = headers%5B0%5D.getValue();%0A%09%09assertEquals(myCorrelationId, header);%0A%09%7D
 %0A%7D%0A

Add unit test for Postprocessor