You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2011/10/04 21:24:59 UTC

svn commit: r1178919 - in /incubator/isis/trunk/framework/viewer/json: json-applib/src/main/java/org/apache/isis/viewer/json/applib/ json-tck/src/test/java/org/apache/isis/viewer/json/tck/ json-tck/src/test/java/org/apache/isis/viewer/json/tck/resource...

Author: danhaywood
Date: Tue Oct  4 19:24:59 2011
New Revision: 1178919

URL: http://svn.apache.org/viewvc?rev=1178919&view=rev
Log:
ISIS-109: working on home page accept headers

Added:
    incubator/isis/trunk/framework/viewer/json/json-tck/src/test/java/org/apache/isis/viewer/json/tck/resources/home/HomePageResourceTest_accept.java
Modified:
    incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/Parser.java
    incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/RestfulRequest.java
    incubator/isis/trunk/framework/viewer/json/json-tck/src/test/java/org/apache/isis/viewer/json/tck/RepresentationMatchers.java
    incubator/isis/trunk/framework/viewer/json/json-tck/src/test/java/org/apache/isis/viewer/json/tck/resources/home/HomePageResourceTest_representationAndHeaders.java

Modified: incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/Parser.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/Parser.java?rev=1178919&r1=1178918&r2=1178919&view=diff
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/Parser.java (original)
+++ incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/Parser.java Tue Oct  4 19:24:59 2011
@@ -2,6 +2,7 @@ package org.apache.isis.viewer.json.appl
 
 import java.text.ParseException;
 import java.text.SimpleDateFormat;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.Date;
@@ -10,6 +11,7 @@ import java.util.List;
 import javax.ws.rs.core.CacheControl;
 import javax.ws.rs.core.MediaType;
 
+import com.google.common.base.Function;
 import com.google.common.base.Joiner;
 import com.google.common.base.Splitter;
 import com.google.common.collect.Iterables;
@@ -228,5 +230,36 @@ public abstract class Parser<T> {
             }
         };
     }
+    
+    public static Parser<List<MediaType>> forListOfMediaTypes() {
+        return new Parser<List<MediaType>>() {
+
+            @Override
+            public List<MediaType> valueOf(String str) {
+                if(str == null) {
+                    return Collections.emptyList();
+                }
+                final List<String> strings = Lists.newArrayList(Splitter.on(",").split(str));
+                return Lists.transform(strings, (Function<? super String, ? extends MediaType>) new Function<String, MediaType>() {
+
+                    @Override
+                    public MediaType apply(String input) {
+                        return MediaType.valueOf(input);
+                    }
+                });
+            }
+
+            @Override
+            public String asString(List<MediaType> listOfMediaTypes) {
+                final List<String> strings = Lists.transform(listOfMediaTypes, new Function<MediaType, String>() {
+                    @Override
+                    public String apply(MediaType input) {
+                        return input.toString();
+                    }
+                });
+                return Joiner.on(",").join(strings);
+            }
+        };
+    }
 
 }

Modified: incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/RestfulRequest.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/RestfulRequest.java?rev=1178919&r1=1178918&r2=1178919&view=diff
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/RestfulRequest.java (original)
+++ incubator/isis/trunk/framework/viewer/json/json-applib/src/main/java/org/apache/isis/viewer/json/applib/RestfulRequest.java Tue Oct  4 19:24:59 2011
@@ -1,8 +1,10 @@
 package org.apache.isis.viewer.json.applib;
 
+import java.util.Arrays;
 import java.util.List;
 import java.util.Map;
 
+import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 
 import org.jboss.resteasy.client.ClientRequest;
@@ -66,7 +68,7 @@ public final class RestfulRequest {
 
     public static class Header<X> {
         public static Header<String> IF_MATCH = new Header<String>("If-Match", Parser.forString());
-        public static Header<List<String>> ACCEPT = new Header<List<String>>("Accept", Parser.forListOfStrings());
+        public static Header<List<MediaType>> ACCEPT = new Header<List<MediaType>>("Accept", Parser.forListOfMediaTypes());
             
         private final String name;
         private final Parser<X> parser;
@@ -94,9 +96,6 @@ public final class RestfulRequest {
         this.clientRequest = clientRequest;
     }
 
-    public <T> void header(Header<T> header, T t) {
-        header.setHeader(clientRequest, t);
-    }
 
     /**
      * Exposed primarily for testing.
@@ -105,6 +104,26 @@ public final class RestfulRequest {
         return clientRequest;
     }
 
+    public <T> RestfulRequest withHeader(Header<T> header, T t) {
+        header.setHeader(clientRequest, t);
+        return this;
+    }
+
+    public <T> RestfulRequest withHeader(Header<List<T>> header, T... ts) {
+        header.setHeader(clientRequest, Arrays.asList(ts));
+        return this;
+    }
+
+    public <Q> RestfulRequest withArg(RestfulRequest.QueryParameter<Q> queryParam, String argStr) {
+        final Q arg = queryParam.getParser().valueOf(argStr);
+        return withArg(queryParam, arg);
+    }
+
+    public <Q> RestfulRequest withArg(RestfulRequest.QueryParameter<Q> queryParam, Q arg) {
+        //return this;
+        throw new RuntimeException("not yet implemented");
+    }
+
     public RestfulResponse<JsonRepresentation> execute() {
         try {
             Response executeJaxrs = clientRequest.execute();
@@ -120,13 +139,5 @@ public final class RestfulRequest {
         return (RestfulResponse<T>) restfulResponse;
     }
 
-    public <Q> RestfulRequest withArg(RestfulRequest.QueryParameter<Q> queryParam, String argStr) {
-        final Q arg = queryParam.getParser().valueOf(argStr);
-        return withArg(queryParam, arg);
-    }
-
-    public <Q> RestfulRequest withArg(RestfulRequest.QueryParameter<Q> queryParam, Q arg) {
-        return null;
-    }
 
 }

Modified: incubator/isis/trunk/framework/viewer/json/json-tck/src/test/java/org/apache/isis/viewer/json/tck/RepresentationMatchers.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/viewer/json/json-tck/src/test/java/org/apache/isis/viewer/json/tck/RepresentationMatchers.java?rev=1178919&r1=1178918&r2=1178919&view=diff
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-tck/src/test/java/org/apache/isis/viewer/json/tck/RepresentationMatchers.java (original)
+++ incubator/isis/trunk/framework/viewer/json/json-tck/src/test/java/org/apache/isis/viewer/json/tck/RepresentationMatchers.java Tue Oct  4 19:24:59 2011
@@ -2,6 +2,7 @@ package org.apache.isis.viewer.json.tck;
 
 import java.io.IOException;
 
+import javax.ws.rs.core.CacheControl;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 
@@ -12,6 +13,7 @@ import org.apache.isis.viewer.json.appli
 import org.apache.isis.viewer.json.applib.RestfulResponse.HttpStatusCode;
 import org.apache.isis.viewer.json.applib.blocks.Link;
 import org.apache.isis.viewer.json.applib.blocks.Method;
+import org.apache.isis.viewer.json.tck.RepresentationMatchers.AbstractMatcherBuilder;
 import org.codehaus.jackson.JsonParseException;
 import org.codehaus.jackson.map.JsonMappingException;
 import org.hamcrest.Description;
@@ -349,6 +351,20 @@ public class RepresentationMatchers {
         };
     }
 
+    public static Matcher<CacheControl> hasMaxAge(final int maxAge) {
+        return new TypeSafeMatcher<CacheControl>() {
+
+            @Override
+            public void describeTo(Description description) {
+                description.appendText("has max age of " + maxAge + " secs");
+            }
+
+            @Override
+            public boolean matchesSafely(CacheControl item) {
+                return maxAge == item.getMaxAge();
+            }
+        };
+    }
 
     
 }

Added: incubator/isis/trunk/framework/viewer/json/json-tck/src/test/java/org/apache/isis/viewer/json/tck/resources/home/HomePageResourceTest_accept.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/viewer/json/json-tck/src/test/java/org/apache/isis/viewer/json/tck/resources/home/HomePageResourceTest_accept.java?rev=1178919&view=auto
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-tck/src/test/java/org/apache/isis/viewer/json/tck/resources/home/HomePageResourceTest_accept.java (added)
+++ incubator/isis/trunk/framework/viewer/json/json-tck/src/test/java/org/apache/isis/viewer/json/tck/resources/home/HomePageResourceTest_accept.java Tue Oct  4 19:24:59 2011
@@ -0,0 +1,74 @@
+package org.apache.isis.viewer.json.tck.resources.home;
+
+import static org.hamcrest.CoreMatchers.*;
+import static org.junit.Assert.assertThat;
+
+import javax.ws.rs.core.MediaType;
+
+import org.apache.isis.runtimes.dflt.webserver.WebServer;
+import org.apache.isis.viewer.json.applib.HttpMethod;
+import org.apache.isis.viewer.json.applib.RepresentationType;
+import org.apache.isis.viewer.json.applib.RestfulClient;
+import org.apache.isis.viewer.json.applib.RestfulMediaType;
+import org.apache.isis.viewer.json.applib.RestfulRequest;
+import org.apache.isis.viewer.json.applib.RestfulRequest.QueryParameter;
+import org.apache.isis.viewer.json.applib.RestfulResponse.HttpStatusCode;
+import org.apache.isis.viewer.json.applib.RestfulResponse;
+import org.apache.isis.viewer.json.applib.homepage.HomePageRepresentation;
+import org.apache.isis.viewer.json.applib.homepage.HomePageResource;
+import org.apache.isis.viewer.json.tck.IsisWebServerRule;
+import org.junit.Before;
+import org.junit.Ignore;
+import org.junit.Rule;
+import org.junit.Test;
+
+
+public class HomePageResourceTest_accept {
+
+    @Rule
+    public IsisWebServerRule webServerRule = new IsisWebServerRule();
+    
+    private RestfulClient client;
+    private HomePageResource resource;
+
+    @Before
+    public void setUp() throws Exception {
+        WebServer webServer = webServerRule.getWebServer();
+        client = new RestfulClient(webServer.getBase());
+        
+        resource = client.getHomePageResource();
+    }
+
+    @Test
+    public void applicationJson() throws Exception {
+
+        final RestfulRequest request = client.createRequest(HttpMethod.GET, "/").withHeader(RestfulRequest.Header.ACCEPT, MediaType.APPLICATION_JSON_TYPE);
+        final RestfulResponse<HomePageRepresentation> restfulResponse = request.executeT();
+        
+        assertThat(restfulResponse.getStatus(), is(HttpStatusCode.OK));
+        
+    }
+
+    @Test
+    public void applicationJson_profileHomePage() throws Exception {
+
+        final RestfulRequest request = client.createRequest(HttpMethod.GET, "/").withHeader(RestfulRequest.Header.ACCEPT, RepresentationType.HOME_PAGE.getMediaType());
+        final RestfulResponse<HomePageRepresentation> restfulResponse = request.executeT();
+        
+        assertThat(restfulResponse.getStatus(), is(HttpStatusCode.OK));
+    }
+
+    @Ignore("not yet implemented")
+    @Test
+    public void applicationJson_invalid() throws Exception {
+
+        final RestfulRequest request = client.createRequest(HttpMethod.GET, "/").withHeader(RestfulRequest.Header.ACCEPT, RepresentationType.USER.getMediaType());
+        final RestfulResponse<HomePageRepresentation> restfulResponse = request.executeT();
+        
+        assertThat(restfulResponse.getStatus(), is(HttpStatusCode.NOT_ACCEPTABLE));
+    }
+
+}
+
+
+    
\ No newline at end of file

Modified: incubator/isis/trunk/framework/viewer/json/json-tck/src/test/java/org/apache/isis/viewer/json/tck/resources/home/HomePageResourceTest_representationAndHeaders.java
URL: http://svn.apache.org/viewvc/incubator/isis/trunk/framework/viewer/json/json-tck/src/test/java/org/apache/isis/viewer/json/tck/resources/home/HomePageResourceTest_representationAndHeaders.java?rev=1178919&r1=1178918&r2=1178919&view=diff
==============================================================================
--- incubator/isis/trunk/framework/viewer/json/json-tck/src/test/java/org/apache/isis/viewer/json/tck/resources/home/HomePageResourceTest_representationAndHeaders.java (original)
+++ incubator/isis/trunk/framework/viewer/json/json-tck/src/test/java/org/apache/isis/viewer/json/tck/resources/home/HomePageResourceTest_representationAndHeaders.java Tue Oct  4 19:24:59 2011
@@ -1,6 +1,7 @@
 package org.apache.isis.viewer.json.tck.resources.home;
 
 import static org.apache.isis.viewer.json.tck.RepresentationMatchers.assertThat;
+import static org.apache.isis.viewer.json.tck.RepresentationMatchers.hasMaxAge;
 import static org.apache.isis.viewer.json.tck.RepresentationMatchers.hasParameter;
 import static org.apache.isis.viewer.json.tck.RepresentationMatchers.hasSubType;
 import static org.apache.isis.viewer.json.tck.RepresentationMatchers.hasType;
@@ -15,12 +16,12 @@ import static org.junit.Assert.assertTha
 
 import java.io.IOException;
 
+import javax.ws.rs.core.CacheControl;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 import javax.ws.rs.core.Response.Status.Family;
 
 import org.apache.isis.runtimes.dflt.webserver.WebServer;
-import org.apache.isis.viewer.json.applib.RepresentationType;
 import org.apache.isis.viewer.json.applib.RestfulClient;
 import org.apache.isis.viewer.json.applib.RestfulResponse;
 import org.apache.isis.viewer.json.applib.RestfulResponse.Header;
@@ -59,15 +60,13 @@ public class HomePageResourceTest_repres
         Response resp = resource.resources();
         
         // when
-        RestfulResponse<HomePageRepresentation> jsonResp = RestfulResponse.ofT(resp);
-        assertThat(jsonResp.getStatus().getFamily(), is(Family.SUCCESSFUL));
+        RestfulResponse<HomePageRepresentation> restfulResponse = RestfulResponse.ofT(resp);
+        assertThat(restfulResponse.getStatus().getFamily(), is(Family.SUCCESSFUL));
         
         // then
-        assertThat(jsonResp.getStatus(), is(HttpStatusCode.OK));
-        assertThat(jsonResp.getHeader(RestfulResponse.Header.CONTENT_TYPE), is(RepresentationType.HOME_PAGE.getMediaType()));
-        assertThat(jsonResp.getHeader(RestfulResponse.Header.CACHE_CONTROL).getMaxAge(), is(24*60*60));
+        assertThat(restfulResponse.getStatus(), is(HttpStatusCode.OK));
         
-        HomePageRepresentation repr = jsonResp.getEntity();
+        HomePageRepresentation repr = restfulResponse.getEntity();
         assertThat(repr, is(not(nullValue())));
         assertThat(repr.isMap(), is(true));
         
@@ -86,13 +85,17 @@ public class HomePageResourceTest_repres
         Response resp = resource.resources();
         
         // when
-        RestfulResponse<HomePageRepresentation> jsonResp = RestfulResponse.ofT(resp);
-        final MediaType contentType = jsonResp.getHeader(Header.CONTENT_TYPE);
+        RestfulResponse<HomePageRepresentation> restfulResponse = RestfulResponse.ofT(resp);
         
         // then
+        final MediaType contentType = restfulResponse.getHeader(Header.CONTENT_TYPE);
         assertThat(contentType, hasType("application"));
         assertThat(contentType, hasSubType("json"));
         assertThat(contentType, hasParameter("profile", "http://restfulobjects.org/profiles/homepage"));
+        
+        // then
+        final CacheControl cacheControl = restfulResponse.getHeader(Header.CACHE_CONTROL);
+        assertThat(cacheControl, hasMaxAge(24*60*60));
     }
 
     @Test
@@ -116,8 +119,8 @@ public class HomePageResourceTest_repres
     }
 
     private HomePageRepresentation givenRepresentation() throws JsonParseException, JsonMappingException, IOException {
-        RestfulResponse<HomePageRepresentation> jsonResp = RestfulResponse.ofT(resource.resources());
-        return jsonResp.getEntity();
+        RestfulResponse<HomePageRepresentation> response = RestfulResponse.ofT(resource.resources());
+        return response.getEntity();
     }
 
 }