blob: 5fa977cdc4030d2d51d7688276526d6529198422 [file] [log] [blame]
SureshBR0bc64992015-12-03 16:38:02 +05301/*
Brian O'Connora09fe5b2017-08-03 21:12:30 -07002 * Copyright 2015-present Open Networking Foundation
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;
SureshBR0bc64992015-12-03 16:38:02 +053026import org.onosproject.codec.CodecService;
27import org.onosproject.net.Device;
28import org.onosproject.net.DeviceId;
29import org.onosproject.vtnrsc.classifier.ClassifierService;
30import org.onosproject.vtnweb.web.SfcCodecContext;
31
Jian Li9d616492016-03-09 10:52:49 -080032import javax.ws.rs.client.WebTarget;
33
34import static org.easymock.EasyMock.createMock;
35import static org.easymock.EasyMock.expect;
36import static org.easymock.EasyMock.replay;
37import static org.hamcrest.Matchers.is;
38import static org.hamcrest.Matchers.notNullValue;
39import static org.junit.Assert.assertThat;
40import static org.onosproject.net.NetTestTools.device;
41import static org.onosproject.net.NetTestTools.did;
42
SureshBR0bc64992015-12-03 16:38:02 +053043/**
44 * Unit tests for classifier REST APIs.
45 */
46public class ClassifierResourceTest extends VtnResourceTest {
47
48 final ClassifierService classifierService = createMock(ClassifierService.class);
49
50 /**
51 * Sets up the global values for all the tests.
52 */
53 @Before
54 public void setUpTest() {
55
56 SfcCodecContext context = new SfcCodecContext();
57 ServiceDirectory testDirectory = new TestServiceDirectory().add(ClassifierService.class, classifierService)
58 .add(CodecService.class, context.codecManager());
Ray Milkey094a1352018-01-22 14:03:54 -080059 setServiceDirectory(testDirectory);
SureshBR0bc64992015-12-03 16:38:02 +053060
61 }
62
63 /**
64 * Cleans up.
65 */
66 @After
67 public void tearDownTest() {
68 }
69
70 /**
71 * Tests the result of the rest api GET when there are no classifiers.
72 */
73 @Test
74 public void testClassifiersEmpty() {
75
76 expect(classifierService.getClassifiers()).andReturn(null).anyTimes();
77 replay(classifierService);
Jian Li9d616492016-03-09 10:52:49 -080078 final WebTarget wt = target();
79 final String response = wt.path("classifiers").request().get(String.class);
SureshBR0bc64992015-12-03 16:38:02 +053080 assertThat(response, is("{\"classifiers\":[]}"));
81 }
82
83 /**
84 * Tests the result of a rest api GET for classifiers.
85 */
86 @Test
87 public void testClassifiers() {
88
89 DeviceId devId1 = did("dev1");
90 Device device1 = device("dev1");
91
92 expect(classifierService.getClassifiers()).andReturn(ImmutableList.of(devId1)).anyTimes();
93 replay(classifierService);
94
Jian Li9d616492016-03-09 10:52:49 -080095 final WebTarget wt = target();
96 final String response = wt.path("classifiers").request().get(String.class);
Jian Li80cfe452016-01-14 16:04:58 -080097 final JsonObject result = Json.parse(response).asObject();
SureshBR0bc64992015-12-03 16:38:02 +053098 assertThat(result, notNullValue());
99 }
100}