| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 85 |
|
| 86 |
@Test |
| 87 |
public void doTestGetPrinters() { |
| 88 |
WebResource webResource = resource(); |
| 89 |
|
| 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 |
|
| 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 |
|
| 104 |
|
| 105 |
@Test |
| 106 |
public void doTestGetPrinterList() { |
| 107 |
WebResource webResource = resource(); |
| 108 |
|
| 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 |
|
| 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 |
|
| 123 |
|
| 124 |
@Test |
| 125 |
public void doTestGetPrinterJMakiTree() { |
| 126 |
WebResource webResource = resource(); |
| 127 |
|
| 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 |
|
| 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 |
|
| 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 |
|
| 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 |
} |