Skip to content

Commit 59b65ee

Browse files
committed
Convert to tests for JSON-P usage with JAX-RS
1 parent 8d9baed commit 59b65ee

File tree

4 files changed

+101
-158
lines changed

4 files changed

+101
-158
lines changed
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3+
*
4+
* Copyright (c) 2013 Oracle and/or its affiliates. All rights reserved.
5+
*
6+
* The contents of this file are subject to the terms of either the GNU
7+
* General Public License Version 2 only ("GPL") or the Common Development
8+
* and Distribution License("CDDL") (collectively, the "License"). You
9+
* may not use this file except in compliance with the License. You can
10+
* obtain a copy of the License at
11+
* https://glassfish.dev.java.net/public/CDDL+GPL_1_1.html
12+
* or packager/legal/LICENSE.txt. See the License for the specific
13+
* language governing permissions and limitations under the License.
14+
*
15+
* When distributing the software, include this License Header Notice in each
16+
* file and include the License file at packager/legal/LICENSE.txt.
17+
*
18+
* GPL Classpath Exception:
19+
* Oracle designates this particular file as subject to the "Classpath"
20+
* exception as provided by Oracle in the GPL Version 2 section of the License
21+
* file that accompanied this code.
22+
*
23+
* Modifications:
24+
* If applicable, add the following below the License Header, with the fields
25+
* enclosed by brackets [] replaced by your own identifying information:
26+
* "Portions Copyright [year] [name of copyright owner]"
27+
*
28+
* Contributor(s):
29+
* If you wish your version of this file to be governed by only the CDDL or
30+
* only the GPL Version 2, indicate your decision by adding "[Contributor]
31+
* elects to include this software in this distribution under the [CDDL or GPL
32+
* Version 2] license." If you don't indicate a single choice of license, a
33+
* recipient has the option to distribute your version of this file under
34+
* either the CDDL, the GPL Version 2 or to extend the choice of license to
35+
* its licensees as provided above. However, if you add GPL Version 2 code
36+
* and therefore, elected the GPL Version 2 license, then the option applies
37+
* only if the new code is made subject to such option by the copyright
38+
* holder.
39+
*/
40+
package org.javaee7.jaxrs.jsonp;
41+
42+
import javax.json.JsonArray;
43+
import javax.ws.rs.Consumes;
44+
import javax.ws.rs.POST;
45+
import javax.ws.rs.Path;
46+
import javax.ws.rs.Produces;
47+
import javax.ws.rs.core.MediaType;
48+
49+
/**
50+
* @author Arun Gupta
51+
*/
52+
@Path("array")
53+
public class MyArrayResource {
54+
@POST
55+
@Consumes(MediaType.APPLICATION_JSON)
56+
@Produces(MediaType.APPLICATION_JSON)
57+
public JsonArray echoArray(JsonArray ja) {
58+
return ja;
59+
}
60+
}

jaxrs/jsonp/src/main/java/org/javaee7/jaxrs/jsonp/MyResource.java renamed to jaxrs/jsonp/src/main/java/org/javaee7/jaxrs/jsonp/MyObjectResource.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,8 +49,8 @@
4949
/**
5050
* @author Arun Gupta
5151
*/
52-
@Path("endpoint")
53-
public class MyResource {
52+
@Path("object")
53+
public class MyObjectResource {
5454
@POST
5555
@Consumes(MediaType.APPLICATION_JSON)
5656
@Produces(MediaType.APPLICATION_JSON)

jaxrs/jsonp/src/main/java/org/javaee7/jaxrs/jsonp/TestServlet.java

Lines changed: 0 additions & 145 deletions
This file was deleted.

jaxrs/jsonp/src/test/java/org/javaee7/jaxrs/jsonp/MyResourceTest.java

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
1-
/*
2-
* To change this license header, choose License Headers in Project Properties.
3-
* To change this template file, choose Tools | Templates
4-
* and open the template in the editor.
5-
*/
61
package org.javaee7.jaxrs.jsonp;
72

83
import java.net.MalformedURLException;
94
import java.net.URI;
105
import java.net.URL;
116

127
import javax.json.Json;
8+
import javax.json.JsonArray;
139
import javax.json.JsonObject;
1410
import javax.ws.rs.client.Client;
1511
import javax.ws.rs.client.ClientBuilder;
@@ -23,6 +19,10 @@
2319
import org.jboss.shrinkwrap.api.ShrinkWrap;
2420
import org.jboss.shrinkwrap.api.spec.WebArchive;
2521
import org.junit.After;
22+
import static org.junit.Assert.assertEquals;
23+
import static org.junit.Assert.assertFalse;
24+
import static org.junit.Assert.assertNotNull;
25+
import static org.junit.Assert.assertTrue;
2626
import org.junit.Before;
2727
import org.junit.Test;
2828
import org.junit.runner.RunWith;
@@ -34,15 +34,17 @@
3434
public class MyResourceTest {
3535

3636
Client client;
37-
WebTarget target;
37+
WebTarget targetObject;
38+
WebTarget targetArray;
3839

3940
@ArquillianResource
4041
URL base;
4142

4243
@Before
4344
public void setUp() throws MalformedURLException {
4445
client = ClientBuilder.newClient();
45-
target = client.target(URI.create(new URL(base, "webresources/endpoint").toExternalForm()));
46+
targetObject = client.target(URI.create(new URL(base, "webresources/object").toExternalForm()));
47+
targetArray = client.target(URI.create(new URL(base, "webresources/array").toExternalForm()));
4648
}
4749

4850
@After
@@ -55,11 +57,12 @@ public static WebArchive createDeployment() {
5557
return ShrinkWrap.create(WebArchive.class)
5658
.addClasses(
5759
MyApplication.class,
58-
MyResource.class);
60+
MyObjectResource.class,
61+
MyArrayResource.class);
5962
}
6063

6164
/**
62-
* Test of echoObject method, of class MyResource.
65+
* Test of echoObject method, of class MyObjectResource.
6366
*/
6467
@Test
6568
public void testEchoObject() {
@@ -68,10 +71,35 @@ public void testEchoObject() {
6871
.add("banana", "yellow")
6972
.build();
7073

71-
JsonObject response = target
74+
JsonObject json = targetObject
7275
.request()
7376
.post(Entity.entity(jsonObject, MediaType.APPLICATION_JSON), JsonObject.class);
74-
System.out.println(response);
77+
assertNotNull(json);
78+
assertFalse(json.isEmpty());
79+
assertTrue(json.containsKey("apple"));
80+
assertEquals("red", json.getString("apple"));
81+
assertTrue(json.containsKey("banana"));
82+
assertEquals("yellow", json.getString("banana"));
83+
}
84+
85+
@Test
86+
public void testEchoArray() {
87+
JsonArray jsonArray = Json.createArrayBuilder()
88+
.add(Json.createObjectBuilder()
89+
.add("apple", "red"))
90+
.add(Json.createObjectBuilder()
91+
.add("banana", "yellow"))
92+
.build();
93+
94+
JsonArray json = targetArray
95+
.request()
96+
.post(Entity.entity(jsonArray, MediaType.APPLICATION_JSON), JsonArray.class);
97+
assertNotNull(json);
98+
assertEquals(2, json.size());
99+
assertTrue(json.getJsonObject(0).containsKey("apple"));
100+
assertEquals("red", json.getJsonObject(0).getString("apple"));
101+
assertTrue(json.getJsonObject(1).containsKey("banana"));
102+
assertEquals("yellow", json.getJsonObject(1).getString("banana"));
75103
}
76104

77105
}

0 commit comments

Comments
 (0)