SureshBR | 0bc6499 | 2015-12-03 16:38:02 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014-2015 Open Networking Laboratory |
| 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 | */ |
| 16 | package org.onosproject.vtnweb.resources; |
| 17 | |
| 18 | import static org.easymock.EasyMock.createMock; |
| 19 | import static org.easymock.EasyMock.expect; |
| 20 | import static org.easymock.EasyMock.replay; |
| 21 | import static org.hamcrest.Matchers.is; |
| 22 | import static org.hamcrest.Matchers.notNullValue; |
| 23 | import static org.junit.Assert.assertThat; |
| 24 | import static org.onosproject.net.NetTestTools.device; |
| 25 | import static org.onosproject.net.NetTestTools.did; |
| 26 | |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 27 | import com.eclipsesource.json.Json; |
SureshBR | 0bc6499 | 2015-12-03 16:38:02 +0530 | [diff] [blame] | 28 | import org.junit.After; |
| 29 | import org.junit.Before; |
| 30 | import org.junit.Test; |
| 31 | import org.onlab.osgi.ServiceDirectory; |
| 32 | import org.onlab.osgi.TestServiceDirectory; |
| 33 | import org.onlab.rest.BaseResource; |
| 34 | import org.onosproject.codec.CodecService; |
| 35 | import org.onosproject.net.Device; |
| 36 | import org.onosproject.net.DeviceId; |
| 37 | import org.onosproject.vtnrsc.classifier.ClassifierService; |
| 38 | import org.onosproject.vtnweb.web.SfcCodecContext; |
| 39 | |
| 40 | import com.eclipsesource.json.JsonObject; |
| 41 | import com.google.common.collect.ImmutableList; |
| 42 | import com.sun.jersey.api.client.WebResource; |
| 43 | /** |
| 44 | * Unit tests for classifier REST APIs. |
| 45 | */ |
| 46 | public 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()); |
| 59 | BaseResource.setServiceDirectory(testDirectory); |
| 60 | |
| 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); |
| 78 | final WebResource rs = resource(); |
| 79 | final String response = rs.path("classifiers").get(String.class); |
| 80 | 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 | |
| 95 | final WebResource rs = resource(); |
| 96 | final String response = rs.path("classifiers").get(String.class); |
Jian Li | 80cfe45 | 2016-01-14 16:04:58 -0800 | [diff] [blame] | 97 | final JsonObject result = Json.parse(response).asObject(); |
SureshBR | 0bc6499 | 2015-12-03 16:38:02 +0530 | [diff] [blame] | 98 | assertThat(result, notNullValue()); |
| 99 | } |
| 100 | } |