Changeset 1886
- Timestamp:
- 2011-04-14 14:00:43 (13 months ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/modules/kauri-dbresources/kauri-dbresources-impl/src/test/java/org/kauriproject/dbresources/test/JpaTest.java
r1678 r1886 81 81 82 82 public void testAllow() throws IOException { 83 Request request = new Request(); 84 Response response; 83 final Request request1 = new Request(); 85 84 Set<Method> allowedMethods; 86 request .setMethod(Method.OPTIONS);85 request1.setMethod(Method.OPTIONS); 87 86 // 1) root 88 request .setResourceRef(makeTestReference("/"));89 response = client.handle(request);90 assertEquals(Status.CLIENT_ERROR_NOT_FOUND, response .getStatus());91 response .getEntity().exhaust();87 request1.setResourceRef(makeTestReference("/")); 88 final Response response1 = client.handle(request1); 89 assertEquals(Status.CLIENT_ERROR_NOT_FOUND, response1.getStatus()); 90 response1.getEntity().exhaust(); 92 91 // 2) unknown entity 93 request.setResourceRef(makeTestReference("/xyz")); 94 response = client.handle(request); 95 assertEquals(Status.CLIENT_ERROR_NOT_FOUND, response.getStatus()); 96 response.getEntity().exhaust(); 92 final Request request2 = new Request(); 93 request2.setMethod(Method.OPTIONS); 94 request2.setResourceRef(makeTestReference("/xyz")); 95 final Response response2 = client.handle(request2); 96 assertEquals(Status.CLIENT_ERROR_NOT_FOUND, response2.getStatus()); 97 response2.getEntity().exhaust(); 97 98 // 3) known entity - list 98 request.setResourceRef(makeTestReference("/dummy")); 99 response = client.handle(request); 100 assertNotNull("no response received", response); 101 allowedMethods = response.getAllowedMethods(); 99 final Request request3 = new Request(); 100 request3.setMethod(Method.OPTIONS); 101 request3.setResourceRef(makeTestReference("/dummy")); 102 final Response response3 = client.handle(request3); 103 assertNotNull("no response received", response3); 104 allowedMethods = response3.getAllowedMethods(); 102 105 assertTrue("GET on known entity (list) should be allowed.", allowedMethods.contains(Method.GET)); 103 106 assertFalse("PUT on known entity (list) should not be allowed.", allowedMethods.contains(Method.PUT)); … … 105 108 assertFalse("DELETE on known entity (list) should not be allowed.", allowedMethods 106 109 .contains(Method.DELETE)); 107 response .getEntity().exhaust();110 response3.getEntity().exhaust(); 108 111 // 4) known entity - identified 109 request.setResourceRef(makeTestReference("/dummy/1")); 110 response = client.handle(request); 111 assertNotNull("no response received", response); 112 allowedMethods = response.getAllowedMethods(); 112 final Request request4 = new Request(); 113 request4.setMethod(Method.OPTIONS); 114 request4.setResourceRef(makeTestReference("/dummy/1")); 115 final Response response4 = client.handle(request4); 116 assertNotNull("no response received", response4); 117 allowedMethods = response4.getAllowedMethods(); 113 118 assertTrue("GET on known entity (identified) should be allowed.", allowedMethods.contains(Method.GET)); 114 119 assertTrue("PUT on known entity (identified) should be allowed.", allowedMethods.contains(Method.PUT)); … … 117 122 assertTrue("DELETE on known entity (identified) should be allowed.", allowedMethods 118 123 .contains(Method.DELETE)); 119 response .getEntity().exhaust();124 response4.getEntity().exhaust(); 120 125 } 121 126 … … 144 149 145 150 public void testDelete() throws Exception { 146 Request request = new Request(); 147 request.setMethod(Method.DELETE); 148 Response response; 149 request.setResourceRef(makeTestReference("/dummy")); 150 response = client.handle(request); 151 final Request request1 = new Request(); 152 request1.setMethod(Method.DELETE); 153 request1.setResourceRef(makeTestReference("/dummy")); 154 final Response response1 = client.handle(request1); 151 155 assertEquals("DELETE on not-identified entity is not allowed.", 152 Status.CLIENT_ERROR_METHOD_NOT_ALLOWED, response .getStatus());153 response .getEntity().exhaust();156 Status.CLIENT_ERROR_METHOD_NOT_ALLOWED, response1.getStatus()); 157 response1.getEntity().exhaust(); 154 158 // first attempt: entity not found in database 155 request.setResourceRef(makeTestReference("/dummy/1")); 156 response = client.handle(request); 159 final Request request2 = new Request(); 160 request2.setMethod(Method.DELETE); 161 request2.setResourceRef(makeTestReference("/dummy/1")); 162 final Response response2 = client.handle(request2); 157 163 assertEquals("DELETE on not existing identified entity should return 'not found'.", 158 Status.CLIENT_ERROR_NOT_FOUND, response .getStatus());159 response .getEntity().exhaust();164 Status.CLIENT_ERROR_NOT_FOUND, response2.getStatus()); 165 response2.getEntity().exhaust(); 160 166 // second attempt: entity found 161 167 loadStuff(); 162 request.setResourceRef(makeTestReference("/dummy/1")); 163 response = client.handle(request); 164 assertEquals("DELETE on identified entity should be OK.", Status.SUCCESS_OK, response.getStatus()); 165 response.getEntity().exhaust(); 168 final Request request3 = new Request(); 169 request3.setResourceRef(makeTestReference("/dummy/1")); 170 request3.setMethod(Method.DELETE); 171 final Response response3 = client.handle(request3); 172 assertEquals("DELETE on identified entity should be OK.", Status.SUCCESS_OK, response3.getStatus()); 173 response3.getEntity().exhaust(); 166 174 } 167 175 168 176 public void testPost() throws Exception { 169 177 // post to known entity - list 170 Request request= new Request();171 request .setResourceRef(makeTestReference("/dummy"));172 request .setMethod(Method.POST);173 request .setEntity(new ObjectRepresentation<Dummy>(createDummy(null)));174 Response response = client.handle(request);175 assertNotNull("no response received", response );176 assertFalse("Response for existing entity contains error", response .getStatus().isError());177 assertEquals("Succesful POST should return '201 - Created'.", Status.SUCCESS_CREATED, response178 .getStatus());178 final Request request1 = new Request(); 179 request1.setResourceRef(makeTestReference("/dummy")); 180 request1.setMethod(Method.POST); 181 request1.setEntity(new ObjectRepresentation<Dummy>(createDummy(null))); 182 final Response response1 = client.handle(request1); 183 assertNotNull("no response received", response1); 184 assertFalse("Response for existing entity contains error", response1.getStatus().isError()); 185 assertEquals("Succesful POST should return '201 - Created'.", Status.SUCCESS_CREATED, 186 response1.getStatus()); 179 187 assertEquals("Succesful POST should set location of created item.", makeTestReference("/dummy/1"), 180 response .getLocationRef());181 response .getEntity().exhaust();188 response1.getLocationRef()); 189 response1.getEntity().exhaust(); 182 190 183 191 // post to known entity - identified 184 request.setResourceRef(makeTestReference("/dummy/1")); 185 response = client.handle(request); 186 assertNotNull("no response received", response); 192 final Request request2 = new Request(); 193 request2.setMethod(Method.POST); 194 request2.setResourceRef(makeTestReference("/dummy/1")); 195 final Response response2 = client.handle(request2); 196 assertNotNull("no response received", response2); 187 197 assertEquals("Post on identified entity is not allowed.", Status.CLIENT_ERROR_METHOD_NOT_ALLOWED, 188 response .getStatus());189 response .getEntity().exhaust();198 response2.getStatus()); 199 response2.getEntity().exhaust(); 190 200 } 191 201 … … 194 204 195 205 // put to known entity - list 196 Request request= new Request();197 request .setMethod(Method.PUT);198 request .setResourceRef(makeTestReference("/dummy"));199 Response response = client.handle(request);200 assertNotNull("no response received", response );201 assertEquals("Put on entity list is not allowed.", Status.CLIENT_ERROR_METHOD_NOT_ALLOWED, response202 .getStatus());203 response .getEntity().exhaust();206 final Request request1 = new Request(); 207 request1.setMethod(Method.PUT); 208 request1.setResourceRef(makeTestReference("/dummy")); 209 final Response response1 = client.handle(request1); 210 assertNotNull("no response received", response1); 211 assertEquals("Put on entity list is not allowed.", Status.CLIENT_ERROR_METHOD_NOT_ALLOWED, 212 response1.getStatus()); 213 response1.getEntity().exhaust(); 204 214 205 215 // put to known entity - identified 206 request.setResourceRef(makeTestReference("/dummy/1")); 216 final Request request2 = new Request(); 217 request2.setMethod(Method.PUT); 218 request2.setResourceRef(makeTestReference("/dummy/1")); 207 219 // first attempt: wrong id 208 request .setEntity(new ObjectRepresentation<Dummy>(createDummy(2)));209 response = client.handle(request);210 assertNotNull("no response received", response );211 assertTrue("Response must contain error because identifiers don't match.", response.getStatus()212 .isError());213 response .getEntity().exhaust();220 request2.setEntity(new ObjectRepresentation<Dummy>(createDummy(2))); 221 final Response response2 = client.handle(request2); 222 assertNotNull("no response received", response2); 223 assertTrue("Response must contain error because identifiers don't match.", 224 response2.getStatus().isError()); 225 response2.getEntity().exhaust(); 214 226 // second attempt: matching id 215 request.setEntity(new ObjectRepresentation<Dummy>(createDummy(1))); 216 response = client.handle(request); 217 assertNotNull("no response received", response); 218 assertFalse("Response for existing entity contains error", response.getStatus().isError()); 219 response.getEntity().exhaust(); 227 final Request request3 = new Request(); 228 request3.setMethod(Method.PUT); 229 request3.setResourceRef(makeTestReference("/dummy/1")); 230 request3.setEntity(new ObjectRepresentation<Dummy>(createDummy(1))); 231 final Response response3 = client.handle(request3); 232 assertNotNull("no response received", response3); 233 assertFalse("Response for existing entity contains error", response3.getStatus().isError()); 234 response3.getEntity().exhaust(); 220 235 } 221 236 … … 227 242 info.getAcceptedMediaTypes().add(pref); 228 243 // get XML 229 Request request= new Request();230 request .setMethod(Method.GET);231 request .setClientInfo(info);232 request .setResourceRef(makeTestReference("/dummy/1"));233 Response response = client.handle(request);234 assertNotNull("no response received", response );235 Representation representation = response.getEntity();244 final Request request1 = new Request(); 245 request1.setMethod(Method.GET); 246 request1.setClientInfo(info); 247 request1.setResourceRef(makeTestReference("/dummy/1")); 248 final Response response1 = client.handle(request1); 249 assertNotNull("no response received", response1); 250 final Representation representation1 = response1.getEntity(); 236 251 assertTrue("expected an xmlrepresentation", MediaType.APPLICATION_XML.getName().equals( 237 representation .getMediaType().getName()));238 representation .exhaust();252 representation1.getMediaType().getName())); 253 representation1.exhaust(); 239 254 // TODO: check some fields 240 255 … … 248 263 sb.append("</org.kauriproject.dbresources.test.po.Dummy>"); 249 264 String xml = sb.toString(); 250 request= new Request();251 request .setMethod(Method.POST);252 request .setResourceRef(makeTestReference("/dummy"));253 request .setEntity(xml, MediaType.APPLICATION_XML);254 response = client.handle(request);255 assertNotNull("no response received", response );256 assertFalse("Response for existing entity contains error", response .getStatus().isError());257 response .getEntity().exhaust();265 final Request request2 = new Request(); 266 request2.setMethod(Method.POST); 267 request2.setResourceRef(makeTestReference("/dummy")); 268 request2.setEntity(xml, MediaType.APPLICATION_XML); 269 final Response response2 = client.handle(request2); 270 assertNotNull("no response received", response2); 271 assertFalse("Response for existing entity contains error", response2.getStatus().isError()); 272 response2.getEntity().exhaust(); 258 273 // TODO: check if added ok 259 274 } … … 266 281 267 282 // get JSON 268 Request request= new Request();269 request .setMethod(Method.GET);270 request .setClientInfo(info);271 request .setResourceRef(makeTestReference("/dummy/1"));272 Response response = client.handle(request);273 assertNotNull("no response received", response );274 Representation representation = response .getEntity();283 final Request request1 = new Request(); 284 request1.setMethod(Method.GET); 285 request1.setClientInfo(info); 286 request1.setResourceRef(makeTestReference("/dummy/1")); 287 final Response response1 = client.handle(request1); 288 assertNotNull("no response received", response1); 289 Representation representation = response1.getEntity(); 275 290 assertTrue("expected a JSONrepresentation", MediaType.APPLICATION_JSON.getName().equals( 276 291 representation.getMediaType().getName())); … … 291 306 sb.append("}}"); 292 307 String json = sb.toString(); 293 request= new Request();294 request .setMethod(Method.POST);295 request .setResourceRef(makeTestReference("/dummy"));296 request .setEntity(new JsonRepresentation(json));297 response = client.handle(request);298 assertNotNull("no response received", response );299 assertFalse("Response for existing entity contains error", response .getStatus().isError());300 response .getEntity().exhaust();308 final Request request2 = new Request(); 309 request2.setMethod(Method.POST); 310 request2.setResourceRef(makeTestReference("/dummy")); 311 request2.setEntity(new JsonRepresentation(json)); 312 final Response response2 = client.handle(request2); 313 assertNotNull("no response received", response2); 314 assertFalse("Response for existing entity contains error", response2.getStatus().isError()); 315 response2.getEntity().exhaust(); 301 316 // TODO: check if added ok 302 317 } … … 305 320 // create 306 321 Dummy dummy = createDummy(null); 307 Request request= new Request();308 request .setMethod(Method.POST);309 request .setResourceRef(makeTestReference("/dummy"));310 request .setEntity(new ObjectRepresentation<Dummy>(dummy));311 Response response = client.handle(request);312 assertNotNull("no response received", response );313 assertFalse("Response contains error", response .getStatus().isError());314 response .getEntity().exhaust();322 final Request request1 = new Request(); 323 request1.setMethod(Method.POST); 324 request1.setResourceRef(makeTestReference("/dummy")); 325 request1.setEntity(new ObjectRepresentation<Dummy>(dummy)); 326 final Response response1 = client.handle(request1); 327 assertNotNull("no response received", response1); 328 assertFalse("Response contains error", response1.getStatus().isError()); 329 response1.getEntity().exhaust(); 315 330 // get 316 request.setMethod(Method.GET); 317 request.setResourceRef(makeTestReference("/dummy/1")); 318 response = client.handle(request); 319 assertNotNull("no response received", response); 320 assertFalse("Response contains error", response.getStatus().isError()); 321 Representation representation = response.getEntity(); 331 final Request request2 = new Request(); 332 request2.setMethod(Method.GET); 333 request2.setResourceRef(makeTestReference("/dummy/1")); 334 final Response response2 = client.handle(request2); 335 assertNotNull("no response received", response2); 336 assertFalse("Response contains error", response2.getStatus().isError()); 337 Representation representation = response2.getEntity(); 322 338 assertNotNull("entity not found", representation.getText()); 323 339 // update 340 final Request request3 = new Request(); 324 341 dummy = createDummy(1); 325 request .setMethod(Method.PUT);326 request .setResourceRef(makeTestReference("/dummy/1"));327 request .setEntity(new ObjectRepresentation<Dummy>(dummy));328 response = client.handle(request);329 assertNotNull("no response received", response );330 assertFalse("Response contains error", response .getStatus().isError());331 response .getEntity().exhaust();342 request3.setMethod(Method.PUT); 343 request3.setResourceRef(makeTestReference("/dummy/1")); 344 request3.setEntity(new ObjectRepresentation<Dummy>(dummy)); 345 final Response response3 = client.handle(request3); 346 assertNotNull("no response received", response3); 347 assertFalse("Response contains error", response3.getStatus().isError()); 348 response3.getEntity().exhaust(); 332 349 // delete 333 request.setMethod(Method.DELETE); 334 response = client.handle(request); 335 assertNotNull("no response received", response); 336 assertFalse("Response contains error", response.getStatus().isError()); 337 response.getEntity().exhaust(); 350 final Request request4 = new Request(); 351 request4.setResourceRef(makeTestReference("/dummy/1")); 352 request4.setMethod(Method.DELETE); 353 final Response response4 = client.handle(request4); 354 assertNotNull("no response received", response4); 355 assertFalse("Response contains error", response4.getStatus().isError()); 356 response4.getEntity().exhaust(); 338 357 } 339 358
Note: See TracChangeset
for help on using the changeset viewer.