content: /*
   * Copyright (c) 2013 3 Round Stones Inc., Some Rights Reserved
   *
   * 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.callimachusproject.server;
  
  import org.callimachusproject.annotations.rel;
  import org.callimachusproject.annotations.requires;
  import org.callimachusproject.server.base.MetadataServerTestCase;
  import org.callimachusproject.server.behaviours.PUTSupport;
  import org.openrdf.annotations.Header;
  import org.openrdf.annotations.Iri;
  import org.openrdf.annotations.Method;
  import org.openrdf.annotations.Path;
  import org.openrdf.annotations.Sparql;
  import org.openrdf.annotations.Type;
  import org.openrdf.query.GraphQueryResult;
  import org.openrdf.query.QueryEvaluationException;
  import org.openrdf.query.TupleQueryResult;
  import org.openrdf.repository.RepositoryException;
  import org.openrdf.repository.object.RDFObject;
  
  import com.sun.jersey.api.client.ClientResponse;
  import com.sun.jersey.api.client.UniformInterfaceException;
  import com.sun.jersey.api.client.WebResource;
  
  public class RequestCacheTest extends MetadataServerTestCase {
  
  	private WebResource display;
  	private WebResource clock;
  
  	@Iri("urn:mimetype:application/clock")
  	public static abstract class Clock implements RDFObject {
  		@Iri("urn:test:display")
  		private Display display;
  
  		@Method("GET")
  		@Path("?display")
  		@requires("urn:test:grant")
  		@Type("text/uri-list")
  		public String getDisplay() {
  			return display.toString();
  		}
  
  		@Method("PUT")
  		@Path("?display")
  		@requires("urn:test:grant")
  		public void setDisplay(@Type("text/uri-list") String display)
  				throws RepositoryException, QueryEvaluationException {
  			this.display = getObjectConnection().getObject(Display.class,
  					display);
  		}
  
  		@Method("PUT")
  		@Path("?date")
  		@requires("urn:test:grant")
  		public void setDate(@Type("*/*") String date) {
  			display.setDate(date);
  		}
  
  		@Method("PUT")
  		@Path("?time")
  		@requires("urn:test:grant")
  		public void setTime(@Type("*/*") String time) {
  			display.setTime(time);
  		}
  	}
  
  	@Iri("urn:mimetype:application/display")
  	public interface Display {
  		
  		@Method("GET")
  		@Path("?date")
  		@Iri("urn:test:date")
  		@requires("urn:test:grant")
  		@Type("text/plain")
  		@Header("Cache-Control:max-age=3")
  		String getDate();
  
  		void setDate(String date);
  
  		@Method("GET")
  		@Path("?time")
  		@Iri("urn:test:time")
  		@requires("urn:test:grant")
  		@Type("text/plain")
  		String getTime();
  
  		void setTime(String time);
  
  		@Method("GET")
  		@rel("alternate")
  		@Path("?construct")
  		@requires("urn:test:grant")
  		@Type("application/rdf+xml")
  		@Sparql("DESCRIBE $this")
  		GraphQueryResult construct();
  
  		@Method("GET")
  		@rel("alternate")
  		@Path("?select")
  		@requires("urn:test:grant")
  		@Type("application/sparql-results+xml")
  		@Sparql("SELECT ?date ?time WHERE { $this <urn:test:date> ?date ; <urn:test:time> ?time }")
  		TupleQueryResult select();
  	}
  
  	public void setUp() throws Exception {
  		config.addConcept(Clock.class);
  		config.addConcept(Display.class);
  		config.addBehaviour(PUTSupport.class);
  		super.setUp();
  		display = client.path("/display");
  		display.type("application/display").put("display");
  		clock = client.path("/clock");
  		clock.type("application/clock").put("clock");
  		clock.queryParam("display", "").type("text/uri-list").put(display.getURI().toASCIIString());
  	}
  
  	protected void addContentEncoding(WebResource client) {
  		// seems to be a bug in this filter wrt Content-Length
  		// client.addFilter(new GZIPContentEncodingFilter());
  	}
  
  	public void testStale() throws Exception {
  		clock.queryParam("time", "").type("text/plain").put("earlier");
  		WebResource time = display.queryParam("time", "");
  		String now = time.get(String.class);
  		Thread.sleep(1000);
  		clock.queryParam("time", "").type("text/plain").put("later");
  		assertFalse(now.equals(time.get(String.class)));
  	}
  
  	public void testResponseMaxAge() throws Exception {
  		clock.queryParam("date", "").put("earlier");
  		WebResource date = display.queryParam("date", "");
  		String now = date.get(String.class);
  		Thread.sleep(1000);
  		clock.queryParam("date", "").put("later");
  		assertEquals(now, date.get(String.class));
  	}
  
  	public void testRequestMaxAge() throws Exception {
  		clock.queryParam("date", "").put("earlier");
  		WebResource time = display.queryParam("date", "");
  		String now = time.get(String.class);
  		Thread.sleep(2000);
  		clock.queryParam("date", "").put("later");
  		assertFalse(now.equals(time.header("cache-control", "max-age=1").get(
  				String.class)));
  	}
  
  	public void testRequestNoCache() throws Exception {
  		clock.queryParam("date", "").put("earlier");
  		WebResource date = display.queryParam("date", "");
  		String now = date.get(String.class);
  		Thread.sleep(1000);
  		clock.queryParam("date", "").put("later");
  		assertFalse(now.equals(date.header("cache-control", "no-cache").get(
  				String.class)));
  	}
  
  	public void testMinFresh() throws Exception {
  		clock.queryParam("date", "").put("earlier");
  		WebResource date = display.queryParam("date", "");
  		String now = date.get(String.class);
  		Thread.sleep(1000);
  		clock.queryParam("date", "").put("later");
  		assertFalse(now.equals(date.header("cache-control", "min-fresh=3").get(
  				String.class)));
  	}
  
  	public void testOnlyCached() throws Exception {
  		clock.queryParam("date", "").put("earlier");
  		WebResource date = display.queryParam("date", "");
  		String now = date.get(String.class);
  		Thread.sleep(1000);
  		clock.queryParam("date", "").put("later");
  		assertEquals(now, date.header("cache-control", "only-if-cached").get(
  				String.class));
  	}
  
  	public void testOnlyNotCached() throws Exception {
  		clock.queryParam("time", "").put("earlier");
  		WebResource date = display.queryParam("time", "");
  		Thread.sleep(1000);
  		clock.queryParam("time", "").put("later");
  		try {
  			date.header("cache-control", "only-if-cached").get(String.class);
  			fail();
  		} catch (UniformInterfaceException e) {
  			assertEquals(504, e.getResponse().getStatus());
  		}
  	}
  
  	public void testSeeAlso() throws Exception {
  		ClientResponse tuple = display.accept("application/sparql-results+xml").get(ClientResponse.class);
  		assertEquals("application/sparql-results+xml", tuple.getType().toString());
  		String tupleTag = tuple.getEntityTag().toString();
  		ClientResponse graph = display.accept("application/rdf+xml").get(ClientResponse.class);
  		assertEquals("application/rdf+xml", graph.getType().toString());
  		String graphTag = graph.getEntityTag().toString();
  		assertFalse(tupleTag.equals(graphTag));
  	}
  
  }
  