http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/QueryResource.java ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/QueryResource.java b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/QueryResource.java new file mode 100644 index 0000000..db470ac --- /dev/null +++ b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/QueryResource.java @@ -0,0 +1,63 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you 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.apache.juneau.rest.test; + +import org.apache.juneau.*; +import org.apache.juneau.rest.*; +import org.apache.juneau.rest.annotation.*; + +/** + * JUnit automated testcase resource. + */ +@RestResource( + path="/testQuery" +) +public class QueryResource extends RestServletDefault { + private static final long serialVersionUID = 1L; + + //==================================================================================================== + // Default values. + //==================================================================================================== + + @RestMethod(name="GET", path="/defaultQuery", defaultQuery={"f1:1","f2=2"," f3 : 3 "}) + public ObjectMap defaultQuery(RequestQuery query) { + return new ObjectMap() + .append("f1", query.getString("f1")) + .append("f2", query.getString("f2")) + .append("f3", query.getString("f3")); + } + + @RestMethod(name="GET", path="/annotatedQuery") + public ObjectMap annotatedQuery(@Query("f1") String f1, @Query("f2") String f2, @Query("f3") String f3) { + return new ObjectMap() + .append("f1", f1) + .append("f2", f2) + .append("f3", f3); + } + + @RestMethod(name="GET", path="/annotatedQueryDefault") + public ObjectMap annotatedQueryDefault(@Query(value="f1",def="1") String f1, @Query(value="f2",def="2") String f2, @Query(value="f3",def="3") String f3) { + return new ObjectMap() + .append("f1", f1) + .append("f2", f2) + .append("f3", f3); + } + + @RestMethod(name="GET", path="/annotatedAndDefaultQuery", defaultQuery={"f1:1","f2=2"," f3 : 3 "}) + public ObjectMap annotatedAndDefaultQuery(@Query(value="f1",def="4") String f1, @Query(value="f2",def="5") String f2, @Query(value="f3",def="6") String f3) { + return new ObjectMap() + .append("f1", f1) + .append("f2", f2) + .append("f3", f3); + } +}
http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/RequestBeanProxyResource.java ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/RequestBeanProxyResource.java b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/RequestBeanProxyResource.java new file mode 100644 index 0000000..eed0679 --- /dev/null +++ b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/RequestBeanProxyResource.java @@ -0,0 +1,50 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you 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.apache.juneau.rest.test; + + +import java.io.*; + +import org.apache.juneau.microservice.*; +import org.apache.juneau.rest.*; +import org.apache.juneau.rest.annotation.*; + +/** + * Validates the functionality of <ja>@RequestBeans</ja>. + */ +@RestResource( + path="/testRequestBeanProxy" +) +@SuppressWarnings("serial") +public class RequestBeanProxyResource extends ResourceJena { + + @RestMethod(name="GET", path="/echoQuery") + public Reader echoQuery(RestRequest req) throws Exception { + return new StringReader(req.getQuery().toString(true)); + } + + @RestMethod(name="POST", path="/echoFormData") + public Reader echoFormData(RestRequest req) throws Exception { + return new StringReader(req.getFormData().toString(true)); + } + + @RestMethod(name="GET", path="/echoHeaders") + public Reader echoHeaders(RestRequest req) throws Exception { + return new StringReader(req.getHeaders().subset("a,b,c,d,e,f,g,h,i,a1,a2,a3,a4,b1,b2,b3,b4,c1,c2,c3,c4").toString(true)); + } + + @RestMethod(name="GET", path="/echoPath/*") + public Reader echoPath(RestRequest req) throws Exception { + return new StringReader(req.getPathMatch().getRemainder()); + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/RestClient2Resource.java ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/RestClient2Resource.java b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/RestClient2Resource.java new file mode 100644 index 0000000..5aa04b9 --- /dev/null +++ b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/RestClient2Resource.java @@ -0,0 +1,36 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you 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.apache.juneau.rest.test; + +import java.io.*; + +import org.apache.juneau.rest.*; +import org.apache.juneau.rest.annotation.*; + +/** + * JUnit automated testcase resource. + */ +@RestResource( + path="/testRestClient" +) +public class RestClient2Resource extends RestServletDefault { + private static final long serialVersionUID = 1L; + + //==================================================================================================== + // Echo response + //==================================================================================================== + @RestMethod(name="POST", path="/") + public Reader test1(RestRequest req) throws Exception { + return new StringReader(req.getBody().asString()); + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/RestHooksInitResource.java ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/RestHooksInitResource.java b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/RestHooksInitResource.java new file mode 100644 index 0000000..4a85c2c --- /dev/null +++ b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/RestHooksInitResource.java @@ -0,0 +1,274 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you 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.apache.juneau.rest.test; + +import static org.apache.juneau.rest.annotation.HookEvent.*; + +import java.util.*; + +import javax.servlet.*; + +import org.apache.juneau.rest.*; +import org.apache.juneau.rest.annotation.*; + +/** + * Validates the behavior of the @RestHook(INIT/POST_INIT/POST_INIT_CHILD_FIRST) annotations. + */ +@RestResource( + path="/testRestHooksInit", + children={ + RestHooksInitResource.Super.class, + RestHooksInitResource.Sub.class + } +) +public class RestHooksInitResource extends RestServletDefault { + private static final long serialVersionUID = 1L; + + @RestResource( + path="/super" + ) + public static class Super extends RestServletDefault { + private static final long serialVersionUID = 1L; + + protected List<String> init = new ArrayList<String>(); + protected List<String> postInit = new ArrayList<String>(); + protected List<String> postInitChildFirst = new ArrayList<String>(); + + @RestHook(INIT) + public void init1c(RestConfig config) { + init.add("super-1c"); + } + + @RestHook(INIT) + public void init1a(ServletConfig config) { + init.add("super-1a"); + } + + @RestHook(INIT) + public void init1b() { + init.add("super-1b"); + } + + @RestHook(INIT) + public void init2a() { + init.add("super-2a"); + } + + @RestHook(POST_INIT) + public void postInit1c(RestContext context) { + postInit.add("super-1c"); + } + + @RestHook(POST_INIT) + public void postInit1a(RestContext context) { + postInit.add("super-1a"); + } + + @RestHook(POST_INIT) + public void postInit1b() { + postInit.add("super-1b"); + } + + @RestHook(POST_INIT) + public void postInit2a() { + postInit.add("super-2a"); + } + + @RestHook(POST_INIT_CHILD_FIRST) + public void postInitChildFirst1c(RestContext context) { + postInitChildFirst.add("super-1c"); + } + + @RestHook(POST_INIT_CHILD_FIRST) + public void postInitChildFirst1a(RestContext context) { + postInitChildFirst.add("super-1a"); + } + + @RestHook(POST_INIT_CHILD_FIRST) + public void postInitChildFirst1b() { + postInitChildFirst.add("super-1b"); + } + + @RestHook(POST_INIT_CHILD_FIRST) + public void postInitChildFirst2a() { + postInitChildFirst.add("super-2a"); + } + + @RestMethod(name="GET", path="/init") + public List<String> getInitEvents() { + return init; + } + + @RestMethod(name="GET", path="/postInit") + public List<String> getPostInitEvents() { + return postInit; + } + + @RestMethod(name="GET", path="/postInitChildFirst") + public List<String> getPostInitChildFirstEvents() { + return postInitChildFirst; + } + } + + @RestResource( + path="/sub", + children={ + Child.class + } + ) + public static class Sub extends Super { + private static final long serialVersionUID = 1L; + + protected static String postInitOrderTest; + protected static String postInitChildFirstOrderTest; + + @Override + @RestHook(INIT) + public void init1c(RestConfig config) { + init.add("sub-1c"); + } + + @Override + @RestHook(INIT) + public void init1a(ServletConfig config) { + init.add("sub-1a"); + } + + @Override + @RestHook(INIT) + public void init1b() { + init.add("sub-1b"); + } + + @RestHook(INIT) + public void init2b() { + init.add("sub-2b"); + } + + @Override + @RestHook(POST_INIT) + public void postInit1c(RestContext context) { + postInit.add("sub-1c"); + } + + @Override + @RestHook(POST_INIT) + public void postInit1a(RestContext context) { + postInit.add("sub-1a"); + } + + @Override + @RestHook(POST_INIT) + public void postInit1b() { + postInit.add("sub-1b"); + } + + @RestHook(POST_INIT) + public void postInit2b() { + postInit.add("sub-2b"); + } + + @Override + @RestHook(POST_INIT_CHILD_FIRST) + public void postInitChildFirst1c(RestContext context) { + postInitChildFirst.add("sub-1c"); + } + + @Override + @RestHook(POST_INIT_CHILD_FIRST) + public void postInitChildFirst1a(RestContext context) { + postInitChildFirst.add("sub-1a"); + } + + @Override + @RestHook(POST_INIT_CHILD_FIRST) + public void postInitChildFirst1b() { + postInitChildFirst.add("sub-1b"); + } + + @RestHook(POST_INIT_CHILD_FIRST) + public void postInitChildFirst2b() { + postInitChildFirst.add("sub-2b"); + } + + @RestHook(POST_INIT) + public void postInitOrderTestSub() { + postInitOrderTest = "PARENT"; + } + + @RestHook(POST_INIT_CHILD_FIRST) + public void postInitChildFirstOrderTestSub() { + postInitChildFirstOrderTest = "PARENT"; + } + + @RestMethod(name="GET", path="/postInitOrder") + public String postInitOrderTest() { + return postInitOrderTest; + } + + @RestMethod(name="GET", path="/postInitChildFirstOrder") + public String postInitChildFirstOrderTest() { + return postInitChildFirstOrderTest; + } + } + + @RestResource( + path="/child" + ) + public static class Child extends Super { + private static final long serialVersionUID = 1L; + + @Override + @RestHook(INIT) + public void init1c(RestConfig config) { + init.add("child-1c"); + } + + @RestHook(INIT) + public void init2b() { + init.add("child-2b"); + } + + @Override + @RestHook(POST_INIT) + public void postInit1c(RestContext context) { + postInit.add("child-1c"); + } + + @RestHook(POST_INIT) + public void postInit2b() { + postInit.add("child-2b"); + } + + @Override + @RestHook(POST_INIT_CHILD_FIRST) + public void postInitChildFirst1c(RestContext context) { + postInitChildFirst.add("child-1c"); + } + + @RestHook(POST_INIT_CHILD_FIRST) + public void postInitChildFirst2b() { + postInitChildFirst.add("child-2b"); + } + + @RestHook(POST_INIT) + public void postInitOrderTestSub() { + Sub.postInitOrderTest = "CHILD"; + } + + @RestHook(POST_INIT_CHILD_FIRST) + public void postInitChildFirstOrderTestSub() { + Sub.postInitChildFirstOrderTest = "CHILD"; + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/RestHooksResource.java ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/RestHooksResource.java b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/RestHooksResource.java new file mode 100644 index 0000000..0597272 --- /dev/null +++ b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/RestHooksResource.java @@ -0,0 +1,189 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you 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.apache.juneau.rest.test; + +import static org.apache.juneau.rest.annotation.HookEvent.*; + +import java.util.*; + +import javax.servlet.http.*; + +import org.apache.juneau.http.*; +import org.apache.juneau.rest.*; +import org.apache.juneau.rest.annotation.*; +import org.apache.juneau.utils.*; + +/** + * Validates the behavior of the @RestHook(START/PRE/POST) annotations. + */ +@RestResource( + path="/testRestHooks", + children={ + RestHooksResource.Start.class, + RestHooksResource.Pre.class, + RestHooksResource.Post.class, + } +) +public class RestHooksResource extends RestServletDefault { + private static final long serialVersionUID = 1L; + + @RestResource( + path="/start" + ) + public static class Start extends StartParent { + private static final long serialVersionUID = 1L; + + private boolean start3Called; + + @RestHook(START_CALL) + public void start3() { + start3Called = true; + } + + @RestHook(START_CALL) + public void start4(HttpServletRequest req, HttpServletResponse res) { + res.setHeader("start3-called", ""+start3Called); + start3Called = false; + if (res.getHeader("start4-called") != null) + throw new RuntimeException("start4 called multiple times."); + res.setHeader("start4-called", "true"); + } + + @RestMethod(path="/") + public Map<String,Object> getHeaders(RestRequest req, RestResponse res) { + return new AMap<String,Object>() + .append("1", res.getHeader("start1-called")) + .append("2", res.getHeader("start2-called")) + .append("3", res.getHeader("start3-called")) + .append("4", res.getHeader("start4-called")); + } + } + + public static class StartParent extends RestServletDefault { + private static final long serialVersionUID = 1L; + + private boolean start1Called; + + @RestHook(START_CALL) + public void start1() { + start1Called = true; + } + + @RestHook(START_CALL) + public void start2(HttpServletRequest req, HttpServletResponse res) { + res.setHeader("start1-called", ""+start1Called); + start1Called = false; + if (res.getHeader("start2-called") != null) + throw new RuntimeException("start2 called multiple times."); + res.setHeader("start2-called", "true"); + } + } + + @RestResource( + path="/pre" + ) + public static class Pre extends PreParent { + private static final long serialVersionUID = 1L; + + private boolean pre3Called; + + @RestHook(PRE_CALL) + public void pre3() { + pre3Called = true; + } + + @RestHook(PRE_CALL) + public void pre4(HttpServletRequest req, HttpServletResponse res) { + res.setHeader("pre3-called", ""+pre3Called); + pre3Called = false; + if (res.getHeader("pre4-called") != null) + throw new RuntimeException("pre4 called multiple times."); + res.setHeader("pre4-called", "true"); + } + + @RestMethod(path="/") + public Map<String,Object> getHeaders(RestRequest req, RestResponse res) { + return new AMap<String,Object>() + .append("1", res.getHeader("pre1-called")) + .append("2", res.getHeader("pre2-called")) + .append("3", res.getHeader("pre3-called")) + .append("4", res.getHeader("pre4-called")); + } + } + + public static class PreParent extends RestServletDefault { + private static final long serialVersionUID = 1L; + + private boolean pre1Called; + + @RestHook(PRE_CALL) + public void pre1() { + pre1Called = true; + } + + @RestHook(PRE_CALL) + public void pre2(Accept accept, RestRequest req, RestResponse res) { + res.setHeader("pre1-called", ""+pre1Called); + pre1Called = false; + if (res.getHeader("pre2-called") != null) + throw new RuntimeException("pre2 called multiple times."); + res.setHeader("pre2-called", "true"); + } + } + + @RestResource( + path="/post" + ) + public static class Post extends PostParent { + private static final long serialVersionUID = 1L; + private boolean post3Called; + + @RestHook(POST_CALL) + public void post3() { + post3Called = true; + } + + @RestHook(POST_CALL) + public void post4(HttpServletRequest req, HttpServletResponse res) { + res.setHeader("post3-called", ""+post3Called); + post3Called = false; + if (res.getHeader("post4-called") != null) + throw new RuntimeException("post4 called multiple times."); + res.setHeader("post4-called", "true"); + } + + @RestMethod(path="/") + public String doGet() { + return "OK"; + } + } + + public static class PostParent extends RestServletDefault { + private static final long serialVersionUID = 1L; + private boolean post1Called; + + @RestHook(POST_CALL) + public void post1() { + post1Called = true; + } + + @RestHook(POST_CALL) + public void post2(Accept accept, RestRequest req, RestResponse res) { + res.setHeader("post1-called", ""+post1Called); + post1Called = false; + if (res.getHeader("post2-called") != null) + throw new RuntimeException("post2 called multiple times."); + res.setHeader("post2-called", "true"); + } + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/Root.java ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/Root.java b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/Root.java new file mode 100644 index 0000000..a7f51e1 --- /dev/null +++ b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/Root.java @@ -0,0 +1,84 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you 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.apache.juneau.rest.test; + +import org.apache.juneau.microservice.resources.*; +import org.apache.juneau.rest.*; +import org.apache.juneau.rest.annotation.*; +import org.apache.juneau.rest.labels.*; + +@RestResource( + path="/", + children={ + AcceptCharsetResource.class, + BeanContextPropertiesResource.class, + BpiResource.class, + CallbackStringsResource.class, + CharsetEncodingsResource.class, + ClientFuturesResource.class, + ClientVersionResource.class, + ConfigResource.class, + ContentResource.class, + DefaultContentTypesResource.class, + ErrorConditionsResource.class, + TransformsResource.class, + FormDataResource.class, + GroupsResource.class, + GzipResource.TestGzipOff.class, + GzipResource.TestGzipOn.class, + HeadersResource.class, + HtmlDocResource.class, + HtmlDocLinksResource.class, + InheritanceResource.TestEncoders.class, + InheritanceResource.TestTransforms.class, + InheritanceResource.TestParsers.class, + InheritanceResource.TestProperties.class, + InheritanceResource.TestSerializers.class, + InterfaceProxyResource.class, + LargePojosResource.class, + MessagesResource.Messages2Resource.class, + MessagesResource.class, + NlsResource.class, + NlsPropertyResource.class, + NoParserInputResource.class, + OnPostCallResource.class, + OnPreCallResource.class, + OptionsWithoutNlsResource.class, + OverlappingMethodsResource.class, + ParamsResource.class, + ParsersResource.class, + PathResource.class, + PathsResource.class, + PathVariablesResource.class, + PropertiesResource.class, + QueryResource.class, + RequestBeanProxyResource.class, + RestClient2Resource.class, + RestHooksInitResource.class, + RestHooksResource.class, + SerializersResource.class, + StaticFilesResource.class, + ThirdPartyProxyResource.class, + UrisResource.class, + UrlContentResource.class, + ShutdownResource.class + } +) +public class Root extends RestServletDefault { + private static final long serialVersionUID = 1L; + + @RestMethod(name="GET", path="/") + public ChildResourceDescriptions doGet(RestRequest req) { + return new ChildResourceDescriptions(getContext(), req); + } +} \ No newline at end of file http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/SerializersResource.java ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/SerializersResource.java b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/SerializersResource.java new file mode 100644 index 0000000..21b1f37 --- /dev/null +++ b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/SerializersResource.java @@ -0,0 +1,143 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you 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.apache.juneau.rest.test; + +import static org.apache.juneau.rest.annotation.Inherit.*; + +import org.apache.juneau.*; +import org.apache.juneau.rest.*; +import org.apache.juneau.rest.annotation.*; +import org.apache.juneau.serializer.*; + +/** + * JUnit automated testcase resource. + */ +@RestResource( + path="/testSerializers", + serializers=SerializersResource.TestSerializerA.class +) +public class SerializersResource extends RestServletDefault { + private static final long serialVersionUID = 1L; + + public static class TestSerializerA extends WriterSerializer { + + public TestSerializerA(PropertyStore propertyStore) { + super(propertyStore, "text/a"); + } + + @Override /* Serializer */ + public WriterSerializerSession createSession(SerializerSessionArgs args) { + return new WriterSerializerSession(args) { + + @Override /* SerializerSession */ + protected void doSerialize(SerializerPipe out, Object o) throws Exception { + out.getWriter().write("text/a - " + o); + } + }; + } + } + + public static class TestSerializerB extends WriterSerializer { + + public TestSerializerB(PropertyStore propertyStore) { + super(propertyStore, "text/b"); + } + + @Override /* Serializer */ + public WriterSerializerSession createSession(SerializerSessionArgs args) { + return new WriterSerializerSession(args) { + + @Override /* SerializerSession */ + protected void doSerialize(SerializerPipe out, Object o) throws Exception { + out.getWriter().write("text/b - " + o); + } + }; + } + } + + //==================================================================================================== + // Serializer defined on class. + //==================================================================================================== + @RestMethod(name="GET", path="/testSerializerOnClass") + public String testSerializerOnClass() { + return "test1"; + } + + //==================================================================================================== + // Serializer defined on method. + //==================================================================================================== + @RestMethod(name="GET", path="/testSerializerOnMethod", serializers=TestSerializerB.class) + public String testSerializerOnMethod() { + return "test2"; + } + + //==================================================================================================== + // Serializer overridden on method. + //==================================================================================================== + @RestMethod(name="GET", path="/testSerializerOverriddenOnMethod", serializers={TestSerializerB.class,TestSerializerC.class}, serializersInherit=SERIALIZERS) + public String testSerializerOverriddenOnMethod() { + return "test3"; + } + + public static class TestSerializerC extends WriterSerializer { + + public TestSerializerC(PropertyStore propertyStore) { + super(propertyStore, "text/a"); + } + + @Override /* Serializer */ + public WriterSerializerSession createSession(SerializerSessionArgs args) { + return new WriterSerializerSession(args) { + + @Override /* SerializerSession */ + protected void doSerialize(SerializerPipe out, Object o) throws Exception { + out.getWriter().write("text/c - " + o); + } + }; + } + } + + //==================================================================================================== + // Serializer with different Accept than Content-Type. + //==================================================================================================== + @RestMethod(name="GET", path="/testSerializerWithDifferentMediaTypes", serializers={TestSerializerD.class}, serializersInherit=SERIALIZERS) + public String testSerializerWithDifferentMediaTypes() { + return "test4"; + } + + public static class TestSerializerD extends WriterSerializer { + + public TestSerializerD(PropertyStore propertyStore) { + super(propertyStore, "text/d", "text/a", "text/d"); + } + + @Override /* Serializer */ + public WriterSerializerSession createSession(SerializerSessionArgs args) { + return new WriterSerializerSession(args) { + + @Override /* SerializerSession */ + protected void doSerialize(SerializerPipe out, Object o) throws Exception { + out.getWriter().write("text/d - " + o); + } + }; + } + } + + //==================================================================================================== + // Check for valid 406 error response. + //==================================================================================================== + @RestMethod(name="GET", path="/test406") + public String test406() { + return "test406"; + } +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/StaticFilesResource.java ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/StaticFilesResource.java b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/StaticFilesResource.java new file mode 100644 index 0000000..ba9d895 --- /dev/null +++ b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/StaticFilesResource.java @@ -0,0 +1,36 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you 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.apache.juneau.rest.test; + +import org.apache.juneau.rest.*; +import org.apache.juneau.rest.annotation.*; + +/** + * JUnit automated testcase resource. + */ +@RestResource( + path="/testStaticFiles", + staticFiles="{xdocs:'xdocs'}" +) +public class StaticFilesResource extends RestServlet { + private static final long serialVersionUID = 1L; + + //==================================================================================================== + // Tests the @RestResource(staticFiles) annotation. + //==================================================================================================== + @RestMethod(name="GET", path="/*") + public String testXdocs() { + return null; + } + +} http://git-wip-us.apache.org/repos/asf/incubator-juneau/blob/750916a9/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/TestUtils.java ---------------------------------------------------------------------- diff --git a/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/TestUtils.java b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/TestUtils.java new file mode 100644 index 0000000..4fab49a --- /dev/null +++ b/juneau-microservice/juneau-microservice-test/src/main/java/org/apache/juneau/rest/test/TestUtils.java @@ -0,0 +1,98 @@ +// *************************************************************************************************************************** +// * Licensed to the Apache Software Foundation (ASF) under one or more contributor license agreements. See the NOTICE file * +// * distributed with this work for additional information regarding copyright ownership. The ASF licenses this file * +// * to you 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.apache.juneau.rest.test; + +import static org.apache.juneau.internal.StringUtils.*; + +import java.util.*; + +import org.apache.juneau.json.*; +import org.apache.juneau.rest.client.*; +import org.apache.juneau.serializer.*; +import org.apache.juneau.transforms.*; +import org.junit.Assert; +import org.junit.ComparisonFailure; + +import junit.framework.*; + +public class TestUtils { + + private static JsonSerializer js2 = new JsonSerializerBuilder() + .simple() + .pojoSwaps(IteratorSwap.class, EnumerationSwap.class) + .build(); + + /** + * Assert that the object equals the specified string after running it through JsonSerializer.DEFAULT_LAX.toString(). + */ + public static void assertObjectEquals(String s, Object o) { + assertObjectEquals(s, o, js2); + } + + /** + * Assert that the object is an instance of the specified class. + */ + public static void assertClass(Class<?> c, Object o) { + Assert.assertEquals(c, o == null ? null : o.getClass()); + } + + /** + * Assert that the object equals the specified string after running it through ws.toString(). + */ + public static void assertObjectEquals(String s, Object o, WriterSerializer ws) { + if ("xxx".equals(s)) + System.err.println("Actual=" + ws.toString(o)); + Assert.assertEquals(s, ws.toString(o)); + } + + public static void checkErrorResponse(boolean debug, RestCallException e, int status, String...contains) throws AssertionFailedError { + String r = e.getResponseMessage(); + if (debug) { + System.err.println(r); // NOT DEBUG + e.printStackTrace(); + } + if (status != e.getResponseCode()) { + dumpResponse(r, "Response status code was not correct. Expected: ''{0}''. Actual: ''{1}''", status, e.getResponseCode()); + throw new AssertionFailedError(format("Response status code was not correct. Expected: ''{0}''. Actual: ''{1}''", status, e.getResponseCode())); + } + for (String s : contains) { + if (r == null || ! r.contains(s)) { + if (! debug) + dumpResponse(r, "Response did not have the following expected text: ''{0}''", s); + throw new AssertionFailedError(format("Response did not have the following expected text: ''{0}''", s)); + } + } + } + + private static void dumpResponse(String r, String msg, Object...args) { + System.err.println("*** Failure ****************************************************************************************"); // NOT DEBUG + System.err.println(format(msg, args)); + System.err.println("*** Response-Start *********************************************************************************"); // NOT DEBUG + System.err.println(r); // NOT DEBUG + System.err.println("*** Response-End ***********************************************************************************"); // NOT DEBUG + } + + public static void assertEqualsAfterSort(String expected, String actual, String msg, Object...args) { + String[] e = expected.trim().split("\n"), a = actual.trim().split("\n"); + + if (e.length != a.length) + throw new ComparisonFailure(format(msg, args), expected, actual); + + Arrays.sort(e); + Arrays.sort(a); + + for (int i = 0; i < e.length; i++) + if (! e[i].equals(a[i])) + throw new ComparisonFailure(format(msg, args), expected, actual); + } +}
