blob: 90d8912e80c1508eaaad7295ba04cf4c82724ea9 [file] [log] [blame]
Ray Milkey2287d882015-01-30 10:15:20 -08001/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2016-present Open Networking Foundation
Ray Milkey2287d882015-01-30 10:15:20 -08003 *
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 Li8ae91202016-03-24 14:36:16 -070016package org.onosproject.rest.resources;
Ray Milkey2287d882015-01-30 10:15:20 -080017
18import org.junit.Test;
19
Jian Li9d616492016-03-09 10:52:49 -080020import javax.ws.rs.NotAllowedException;
21import javax.ws.rs.NotFoundException;
22import javax.ws.rs.client.WebTarget;
Ray Milkey2287d882015-01-30 10:15:20 -080023
24import static org.hamcrest.Matchers.containsString;
25import static org.junit.Assert.assertThat;
26import static org.junit.Assert.fail;
27
28/**
29 * Unit tests for bad REST requests.
30 */
31public class BadRequestTest extends ResourceTest {
Ray Milkeya0cecdc2015-04-09 09:51:52 -070032
33 /**
34 * Tests the response for an invalid URL.
35 */
Ray Milkey2287d882015-01-30 10:15:20 -080036 @Test
37 public void badUrl() {
Jian Li9d616492016-03-09 10:52:49 -080038 WebTarget wt = target();
Ray Milkey2287d882015-01-30 10:15:20 -080039 try {
Jian Li9d616492016-03-09 10:52:49 -080040 wt.path("ThisIsABadURL").request().get(String.class);
Ray Milkey2287d882015-01-30 10:15:20 -080041 fail("Fetch of non-existent URL did not throw an exception");
Jian Li9d616492016-03-09 10:52:49 -080042 } catch (NotFoundException ex) {
43 assertThat(ex.getMessage(), containsString("HTTP 404 Not Found"));
Ray Milkey2287d882015-01-30 10:15:20 -080044 }
45 }
Ray Milkeya0cecdc2015-04-09 09:51:52 -070046
47 /**
48 * Tests the response for a request with a bad method.
49 */
50 @Test
51 public void badMethod() {
Jian Li9d616492016-03-09 10:52:49 -080052 WebTarget wt = target();
Ray Milkeya0cecdc2015-04-09 09:51:52 -070053 try {
Jian Li9d616492016-03-09 10:52:49 -080054 wt.path("hosts").request().delete(String.class);
Ray Milkeya0cecdc2015-04-09 09:51:52 -070055 fail("Fetch of non-existent URL did not throw an exception");
Jian Li9d616492016-03-09 10:52:49 -080056 } catch (NotAllowedException ex) {
Ray Milkeya0cecdc2015-04-09 09:51:52 -070057 assertThat(ex.getMessage(),
Jian Li9d616492016-03-09 10:52:49 -080058 containsString("HTTP 405 Method Not Allowed"));
Ray Milkeya0cecdc2015-04-09 09:51:52 -070059 }
60 }
Ray Milkey2287d882015-01-30 10:15:20 -080061}