Ray Milkey | 2287d88 | 2015-01-30 10:15:20 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | a09fe5b | 2017-08-03 21:12:30 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Foundation |
Ray Milkey | 2287d88 | 2015-01-30 10:15:20 -0800 | [diff] [blame] | 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Jian Li | 8ae9120 | 2016-03-24 14:36:16 -0700 | [diff] [blame] | 16 | package org.onosproject.rest.resources; |
Ray Milkey | 2287d88 | 2015-01-30 10:15:20 -0800 | [diff] [blame] | 17 | |
| 18 | import org.junit.Test; |
| 19 | |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 20 | import javax.ws.rs.NotAllowedException; |
| 21 | import javax.ws.rs.NotFoundException; |
| 22 | import javax.ws.rs.client.WebTarget; |
Ray Milkey | 2287d88 | 2015-01-30 10:15:20 -0800 | [diff] [blame] | 23 | |
| 24 | import static org.hamcrest.Matchers.containsString; |
| 25 | import static org.junit.Assert.assertThat; |
| 26 | import static org.junit.Assert.fail; |
| 27 | |
| 28 | /** |
| 29 | * Unit tests for bad REST requests. |
| 30 | */ |
| 31 | public class BadRequestTest extends ResourceTest { |
Ray Milkey | a0cecdc | 2015-04-09 09:51:52 -0700 | [diff] [blame] | 32 | |
| 33 | /** |
| 34 | * Tests the response for an invalid URL. |
| 35 | */ |
Ray Milkey | 2287d88 | 2015-01-30 10:15:20 -0800 | [diff] [blame] | 36 | @Test |
| 37 | public void badUrl() { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 38 | WebTarget wt = target(); |
Ray Milkey | 2287d88 | 2015-01-30 10:15:20 -0800 | [diff] [blame] | 39 | try { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 40 | wt.path("ThisIsABadURL").request().get(String.class); |
Ray Milkey | 2287d88 | 2015-01-30 10:15:20 -0800 | [diff] [blame] | 41 | fail("Fetch of non-existent URL did not throw an exception"); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 42 | } catch (NotFoundException ex) { |
| 43 | assertThat(ex.getMessage(), containsString("HTTP 404 Not Found")); |
Ray Milkey | 2287d88 | 2015-01-30 10:15:20 -0800 | [diff] [blame] | 44 | } |
| 45 | } |
Ray Milkey | a0cecdc | 2015-04-09 09:51:52 -0700 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * Tests the response for a request with a bad method. |
| 49 | */ |
| 50 | @Test |
| 51 | public void badMethod() { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 52 | WebTarget wt = target(); |
Ray Milkey | a0cecdc | 2015-04-09 09:51:52 -0700 | [diff] [blame] | 53 | try { |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 54 | wt.path("hosts").request().delete(String.class); |
Ray Milkey | a0cecdc | 2015-04-09 09:51:52 -0700 | [diff] [blame] | 55 | fail("Fetch of non-existent URL did not throw an exception"); |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 56 | } catch (NotAllowedException ex) { |
Ray Milkey | a0cecdc | 2015-04-09 09:51:52 -0700 | [diff] [blame] | 57 | assertThat(ex.getMessage(), |
Jian Li | 9d61649 | 2016-03-09 10:52:49 -0800 | [diff] [blame] | 58 | containsString("HTTP 405 Method Not Allowed")); |
Ray Milkey | a0cecdc | 2015-04-09 09:51:52 -0700 | [diff] [blame] | 59 | } |
| 60 | } |
Ray Milkey | 2287d88 | 2015-01-30 10:15:20 -0800 | [diff] [blame] | 61 | } |