blob: 6b29e5bc28e6a62f47075f5d2950cf36f2f59403 [file] [log] [blame]
SureshBR0bc64992015-12-03 16:38:02 +05301/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
SureshBR0bc64992015-12-03 16:38:02 +05303 *
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 */
16package org.onosproject.vtnweb.resources;
17
Jian Li80cfe452016-01-14 16:04:58 -080018import com.eclipsesource.json.Json;
Jian Li9d616492016-03-09 10:52:49 -080019import com.eclipsesource.json.JsonObject;
20import com.google.common.collect.ImmutableList;
SureshBR0bc64992015-12-03 16:38:02 +053021import org.junit.After;
22import org.junit.Before;
23import org.junit.Test;
24import org.onlab.osgi.ServiceDirectory;
25import org.onlab.osgi.TestServiceDirectory;
26import org.onlab.rest.BaseResource;
27import org.onosproject.codec.CodecService;
28import org.onosproject.net.Device;
29import org.onosproject.net.DeviceId;
30import org.onosproject.vtnrsc.classifier.ClassifierService;
31import org.onosproject.vtnweb.web.SfcCodecContext;
32
Jian Li9d616492016-03-09 10:52:49 -080033import javax.ws.rs.client.WebTarget;
34
35import static org.easymock.EasyMock.createMock;
36import static org.easymock.EasyMock.expect;
37import static org.easymock.EasyMock.replay;
38import static org.hamcrest.Matchers.is;
39import static org.hamcrest.Matchers.notNullValue;
40import static org.junit.Assert.assertThat;
41import static org.onosproject.net.NetTestTools.device;
42import static org.onosproject.net.NetTestTools.did;
43
SureshBR0bc64992015-12-03 16:38:02 +053044/**
45 * Unit tests for classifier REST APIs.
46 */
47public class ClassifierResourceTest extends VtnResourceTest {
48
49 final ClassifierService classifierService = createMock(ClassifierService.class);
50
51 /**
52 * Sets up the global values for all the tests.
53 */
54 @Before
55 public void setUpTest() {
56
57 SfcCodecContext context = new SfcCodecContext();
58 ServiceDirectory testDirectory = new TestServiceDirectory().add(ClassifierService.class, classifierService)
59 .add(CodecService.class, context.codecManager());
60 BaseResource.setServiceDirectory(testDirectory);
61
62 }
63
64 /**
65 * Cleans up.
66 */
67 @After
68 public void tearDownTest() {
69 }
70
71 /**
72 * Tests the result of the rest api GET when there are no classifiers.
73 */
74 @Test
75 public void testClassifiersEmpty() {
76
77 expect(classifierService.getClassifiers()).andReturn(null).anyTimes();
78 replay(classifierService);
Jian Li9d616492016-03-09 10:52:49 -080079 final WebTarget wt = target();
80 final String response = wt.path("classifiers").request().get(String.class);
SureshBR0bc64992015-12-03 16:38:02 +053081 assertThat(response, is("{\"classifiers\":[]}"));
82 }
83
84 /**
85 * Tests the result of a rest api GET for classifiers.
86 */
87 @Test
88 public void testClassifiers() {
89
90 DeviceId devId1 = did("dev1");
91 Device device1 = device("dev1");
92
93 expect(classifierService.getClassifiers()).andReturn(ImmutableList.of(devId1)).anyTimes();
94 replay(classifierService);
95
Jian Li9d616492016-03-09 10:52:49 -080096 final WebTarget wt = target();
97 final String response = wt.path("classifiers").request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -080098 final JsonObject result = Json.parse(response).asObject();
SureshBR0bc64992015-12-03 16:38:02 +053099 assertThat(result, notNullValue());
100 }
101}