Debian Codegraph @

tests/functional/jmaki-backend/src/test/java/com/sun/jersey/samples/jmaki/JMakiBackendWebappTest.java

raw symbols SCIP
1 /*
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3 *
4 * Copyright (c) 2010-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 * http://glassfish.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
41 package com.sun.jersey.samples.jmaki;
42
43 import com.sun.jersey.api.client.ClientResponse;
44 import com.sun.jersey.api.client.WebResource;
45 import com.sun.jersey.api.client.filter.LoggingFilter;
46 import com.sun.jersey.core.header.MediaTypes;
47 import com.sun.jersey.samples.jmaki.beans.Printer;
48 import com.sun.jersey.samples.jmaki.beans.PrinterTableModel;
49 import com.sun.jersey.samples.jmaki.beans.TreeModel;
50 import com.sun.jersey.samples.jmaki.beans.WebResourceList;
51 import com.sun.jersey.test.framework.JerseyTest;
52 import com.sun.jersey.test.framework.WebAppDescriptor;
53 import java.util.List;
54 import javax.ws.rs.core.MediaType;
55 import javax.ws.rs.core.Response;
56 import org.junit.Assert;
57 import org.junit.Test;
58
59 /**
60 *
61 * @author Naresh
62 */
63 public class JMakiBackendWebappTest extends JerseyTest {
64
65 public JMakiBackendWebappTest() throws Exception {
66 super(new WebAppDescriptor.Builder("com.sun.jersey.samples.jmaki")
67 .contextPath("/jMakiBackend")
68 .servletPath("/webresources")
69 .build());
70 }
71
72 /**
73 * Test checks the application WADL is generated.
74 */
75 @Test
76 public void doTestApplicationWadl() {
77 WebResource webResource = resource();
78 String wadl = webResource.path("application.wadl").accept(MediaTypes.WADL).get(String.class);
79 Assert.assertTrue("Method: doTestApplicationWadl \nMessage: Something wrong, returned WADL length is not > 0",
80 wadl.length() > 0);
81 }
82
83 /**
84 * Test checks GET on resource "printers".
85 */
86 @Test
87 public void doTestGetPrinters() {
88 WebResource webResource = resource();
89 // GET Printers - mime-type application/json
90 WebResourceList resourceList = webResource.path("printers").accept(MediaType.APPLICATION_JSON).get(WebResourceList.class);
91 int numberOfResourceTypes = resourceList.items.size();
92 Assert.assertEquals("Method: doTestGetPrinters \nMessage: Number of resource types retrieved " +
93 "with MIME-TYPE application/json do not match the expected number.", 3, numberOfResourceTypes);
94
95 // GET Printers - mime-type application/xml
96 resourceList = webResource.path("printers").accept(MediaType.APPLICATION_XML).get(WebResourceList.class);
97 numberOfResourceTypes = resourceList.items.size();
98 Assert.assertEquals("Method: doTestGetPrinters \nMessage: Number of resource types retrieved " +
99 "with MIME-TYPE application/xml do not match the expected number.", 3, numberOfResourceTypes);
100 }
101
102 /**
103 * Test checks GET on resource "printers/list".
104 */
105 @Test
106 public void doTestGetPrinterList() {
107 WebResource webResource = resource();
108 //GET on printer list - mime-type application/json
109 WebResourceList resourceList = webResource.path("printers").path("list").accept(MediaType.APPLICATION_JSON).get(WebResourceList.class);
110 int numberOfPrinters = resourceList.items.size();
111 Assert.assertEquals("Method: doTestGetPrinterList \nMessage: Number of printers retrieved " +
112 "with MIME-TYPE application/json do not match the expected number.", 5, numberOfPrinters);
113
114 //GET on printer list - mime-type application/xml
115 resourceList = webResource.path("printers").path("list").accept(MediaType.APPLICATION_XML).get(WebResourceList.class);
116 numberOfPrinters = resourceList.items.size();
117 Assert.assertEquals("Method: doTestGetPrinterList \nMessage: Number of printers retrieved " +
118 "with MIME-TYPE application/xml do not match the expected number.", 5, numberOfPrinters);
119 }
120
121 /**
122 * Test checks GET on resource "printers/jMakiTree".
123 */
124 @Test
125 public void doTestGetPrinterJMakiTree() {
126 WebResource webResource = resource();
127 //GET on printer list - mime-type application/json
128 TreeModel treeModel = webResource.path("printers").path("jMakiTree").accept(MediaType.APPLICATION_JSON).get(TreeModel.class);
129 Assert.assertEquals("Method: doTestGetPrinterJMakiTree \nMessage: Root of the returned " +
130 "jMakiTree doesn't match the expected value", "printers", treeModel.root.label);
131 }
132
133 /**
134 * Test checks GET on resource "printers/jMakiTable".
135 */
136 @Test
137 public void doTestGetPrinterJMakiTable() {
138 WebResource webResource = resource();
139 PrinterTableModel printerTableModel = webResource.path("printers").path("jMakiTable").accept(MediaType.APPLICATION_JSON).get(PrinterTableModel.class);
140 List<PrinterTableModel.JMakiTableHeader> tableHeaders = printerTableModel.columns;
141 Assert.assertEquals("Method: doTestGetPrinterJMakiTable \nMessage: Number of table headers " +
142 "do not match the expected number", 4, tableHeaders.size());
143 }
144
145 /**
146 * Test checks GET on resource "printers/ids" based on id.
147 */
148 @Test
149 public void doTestGetPrinterBasedOnId() {
150 WebResource webResource = resource();
151 Printer printer = webResource.path("printers").path("ids").path("P01").accept(MediaType.APPLICATION_JSON).get(Printer.class);
152 Assert.assertEquals("Method: doTestGetPrinterBasedOnId \nMessage: ID of the retrieved printer " +
153 "doesn't match the search value", "P01", printer.id);
154 }
155
156 /**
157 * Test checks PUT on resource "printers/ids" based on id.
158 */
159 @Test
160 public void doTestPutPrinterBasedOnId() {
161 WebResource webResource = resource();
162 LoggingFilter loggingFilter = new LoggingFilter();
163 webResource.addFilter(loggingFilter);
164 Printer printer = webResource.path("printers").path("ids").path("P01").accept(MediaType.APPLICATION_JSON).get(Printer.class);
165 String printerModel = printer.model;
166 String printerLocation = printer.location;
167 String printerUrl = printer.url;
168 printer = new Printer("P01", "Xerox", printerLocation, printerUrl);
169 ClientResponse response = webResource.path("printers").path("ids").path("P01").type(MediaType.APPLICATION_JSON).put(ClientResponse.class, printer);
170 Assert.assertEquals("Method: doTestPutPrinterBasedOnId \nMessage: Response status doesn't match the expected value.",
171 Response.Status.NO_CONTENT, response.getResponseStatus());
172 printer = webResource.path("printers").path("ids").path("P01").accept(MediaType.APPLICATION_JSON).get(Printer.class);
173 Assert.assertNotSame("Method: doTestPutPrinterBasedOnId \nMessage: Printer holds the old model inspite of update.", printerModel, printer.model);
174 Assert.assertEquals("Method: doTestPutPrinterBasedOnId \nMessage: Updated printer model doesn't get reflected.", "Xerox", printer.model);
175 }
176 }