package org.callimachusproject.server;

import java.io.IOException;
import java.io.InputStream;

import org.callimachusproject.annotations.method;
import org.callimachusproject.annotations.requires;
import org.callimachusproject.annotations.type;
import org.callimachusproject.server.base.MetadataServerTestCase;
import org.callimachusproject.server.behaviours.PUTSupport;
import org.callimachusproject.server.behaviours.TextFile;
import org.callimachusproject.server.concepts.HTTPFileObject;
import org.junit.Ignore;

import com.sun.jersey.api.client.ClientResponse;
import com.sun.jersey.api.client.WebResource;

public class RangeTest extends MetadataServerTestCase {

	public static abstract class StringFile implements HTTPFileObject {
		@method("GET")
		@requires("urn:test:grant")
		@type("application/string")
		public InputStream getInputStream() throws IOException {
			return openInputStream();
		}
	}

	public void setUp() throws Exception {
		config.addBehaviour(TextFile.class, "urn:mimetype:text/plain");
		config.addBehaviour(StringFile.class, "urn:mimetype:application/string");
		config.addBehaviour(PUTSupport.class);
		super.setUp();
	}

	public void testUptoRange() throws Exception {
		WebResource web = client.path("/hello");
		web.type("application/string").put("Hello World!");
		assertEquals("Hello World!", web.header("Range", "bytes=0-100").get(String.class));
	}

	public void testStartRange() throws Exception {
		WebResource web = client.path("/hello");
		web.type("application/string").put("Hello World!");
		assertEquals("Hello", web.header("Range", "bytes=0-4").get(String.class));
	}

	public void testEndRange() throws Exception {
		WebResource web = client.path("/hello");
		web.type("application/string").put("Hello World!");
		assertEquals("World!", web.header("Range", "bytes=-6").get(String.class));
	}

	public void testLastRange() throws Exception {
		WebResource web = client.path("/hello");
		web.type("application/string").put("Hello World!");
		assertEquals("Hello World!", web.header("Range", "bytes=-100").get(String.class));
	}

	public void testIfRangeTag() throws Exception {
		WebResource web = client.path("/hello");
		ClientResponse resp = web.type("application/string").put(ClientResponse.class, "Hello World!");
		String tag = resp.getEntityTag().toString();
		assertEquals("Hello", web.header("If-Range", tag).header("Range", "bytes=0-4").get(String.class));
	}

	public void testIfRangetagFail() throws Exception {
		WebResource web = client.path("/hello");
		ClientResponse resp = web.type("application/string").put(ClientResponse.class, "Hello World!");
		String tag = resp.getEntityTag().toString();
		web.type("application/string").put("Hey World!");
		assertEquals("Hey World!", web.header("If-Range", tag).header("Range", "bytes=0-4").get(String.class));
	}

	@Ignore
	public void testIfRangeDate() throws Exception {
		WebResource web = client.path("/hello");
		ClientResponse resp = web.type("application/string").put(ClientResponse.class, "Hello World!");
		String m = resp.getHeaders().getFirst("Last-Modified");
		assertNotNull(m);
		assertEquals("Hello", web.header("If-Range", m).header("Range", "bytes=0-4").get(String.class));
	}

	public void testIfRangeDateFail() throws Exception {
		WebResource web = client.path("/hello");
		ClientResponse resp = web.type("application/string").put(ClientResponse.class, "Hello World!");
		String m = resp.getHeaders().getFirst("Last-Modified");
		assertNotNull(m);
		Thread.sleep(1000);
		web.type("application/string").put("Hey World!");
		assertEquals("Hey World!", web.header("If-Range", m).header("Range", "bytes=0-4").get(String.class));
	}
}

@@ -2821,389 +2821,8 @@
 %09%7D%0A%0A
-%09@Ignore%0A%09public void testIfRangeDate() throws Exception %7B%0A%09%09WebResource web = client.path(%22/hello%22);%0A%09%09ClientResponse resp = web.type(%22application/string%22).put(ClientResponse.class, %22Hello World!%22);%0A%09%09String m = resp.getHeaders().getFirst(%22Last-Modified%22);%0A%09%09assertNotNull(m);%0A%09%09assertEquals(%22Hello%22, web.header(%22If-Range%22, m).header(%22Range%22, %22bytes=0-4%22).get(String.class));%0A%09%7D%0A%0A
 %09pub

Remove date range test