Sean Condon | 0e89bda | 2017-03-21 14:23:19 +0000 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright 2017-present Open Networking Foundation |
| 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.cfm.impl; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 19 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 20 | import org.junit.Before; |
| 21 | import org.junit.Test; |
| 22 | import org.onlab.osgi.ServiceDirectory; |
| 23 | import org.onlab.osgi.TestServiceDirectory; |
| 24 | import org.onlab.rest.BaseResource; |
| 25 | import org.onosproject.cfm.CfmCodecContext; |
| 26 | import org.onosproject.codec.CodecService; |
| 27 | import org.onosproject.incubator.net.l2monitoring.cfm.DefaultMaintenanceDomain; |
| 28 | import org.onosproject.incubator.net.l2monitoring.cfm.MaintenanceDomain; |
| 29 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdId; |
| 30 | import org.onosproject.incubator.net.l2monitoring.cfm.identifier.MdIdCharStr; |
| 31 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmConfigException; |
| 32 | import org.onosproject.incubator.net.l2monitoring.cfm.service.CfmMdService; |
| 33 | |
| 34 | import javax.ws.rs.InternalServerErrorException; |
| 35 | import javax.ws.rs.client.Entity; |
| 36 | import javax.ws.rs.client.WebTarget; |
| 37 | import javax.ws.rs.core.Response; |
| 38 | import java.io.BufferedReader; |
| 39 | import java.io.ByteArrayInputStream; |
| 40 | import java.io.IOException; |
| 41 | import java.io.InputStreamReader; |
| 42 | import java.util.ArrayList; |
| 43 | import java.util.List; |
| 44 | import java.util.Optional; |
| 45 | |
| 46 | import static junit.framework.TestCase.fail; |
| 47 | import static org.easymock.EasyMock.createMock; |
| 48 | import static org.easymock.EasyMock.expect; |
| 49 | import static org.easymock.EasyMock.replay; |
| 50 | import static org.hamcrest.Matchers.is; |
| 51 | import static org.junit.Assert.assertEquals; |
| 52 | import static org.junit.Assert.assertThat; |
| 53 | |
| 54 | public class MdWebResourceTest extends CfmResourceTest { |
| 55 | private final CfmMdService mdService = createMock(CfmMdService.class); |
| 56 | |
| 57 | private static final MdId MDNAME1 = MdIdCharStr.asMdId("md-1"); |
| 58 | private static final MdId MDNAME2 = MdIdCharStr.asMdId("md-2"); |
| 59 | |
| 60 | private List<MaintenanceDomain> mdList; |
| 61 | |
| 62 | @Before |
| 63 | public void setUpTest() throws CfmConfigException { |
| 64 | CfmCodecContext context = new CfmCodecContext(); |
| 65 | ServiceDirectory testDirectory = new TestServiceDirectory() |
| 66 | .add(CfmMdService.class, mdService) |
| 67 | .add(CodecService.class, context.codecManager()); |
| 68 | BaseResource.setServiceDirectory(testDirectory); |
| 69 | |
| 70 | mdList = new ArrayList<>(); |
| 71 | |
| 72 | mdList.add(DefaultMaintenanceDomain.builder(MDNAME1) |
| 73 | .mdLevel(MaintenanceDomain.MdLevel.LEVEL1).build()); |
| 74 | mdList.add(DefaultMaintenanceDomain.builder(MDNAME2) |
| 75 | .mdLevel(MaintenanceDomain.MdLevel.LEVEL2).build()); |
| 76 | } |
| 77 | |
| 78 | @Test |
| 79 | public void testGetMds() { |
| 80 | expect(mdService.getAllMaintenanceDomain()).andReturn(mdList).anyTimes(); |
| 81 | replay(mdService); |
| 82 | |
| 83 | final WebTarget wt = target(); |
| 84 | final String response = wt.path("md").request().get(String.class); |
| 85 | |
| 86 | assertThat(response, is("{\"mds\":[[" + |
| 87 | "{\"mdName\":\"md-1\",\"mdNameType\":\"CHARACTERSTRING\"," + |
| 88 | "\"mdLevel\":\"LEVEL1\",\"maList\":[]}," + |
| 89 | "{\"mdName\":\"md-2\",\"mdNameType\":\"CHARACTERSTRING\"," + |
| 90 | "\"mdLevel\":\"LEVEL2\",\"maList\":[]}]]}")); |
| 91 | } |
| 92 | |
| 93 | @Test |
| 94 | public void testGetMdsEmpty() { |
| 95 | expect(mdService.getAllMaintenanceDomain()) |
| 96 | .andReturn(new ArrayList<>()).anyTimes(); |
| 97 | replay(mdService); |
| 98 | |
| 99 | final WebTarget wt = target(); |
| 100 | final String response = wt.path("md").request().get(String.class); |
| 101 | |
| 102 | assertThat(response, is("{\"mds\":[[]]}")); |
| 103 | } |
| 104 | |
| 105 | @Test |
| 106 | public void testGetMd() { |
| 107 | expect(mdService.getMaintenanceDomain(MDNAME1)) |
| 108 | .andReturn(Optional.ofNullable(mdList.get(0))).anyTimes(); |
| 109 | replay(mdService); |
| 110 | |
| 111 | final WebTarget wt = target(); |
| 112 | final String response = wt.path("md/" + MDNAME1).request().get(String.class); |
| 113 | |
| 114 | assertThat(response, is("{\"md\":" + |
| 115 | "{\"mdName\":\"md-1\",\"mdNameType\":\"CHARACTERSTRING\"," + |
| 116 | "\"mdLevel\":\"LEVEL1\",\"maList\":[]}}")); |
| 117 | } |
| 118 | |
| 119 | @Test |
| 120 | public void testGetMdEmpty() throws IOException { |
| 121 | final MdId mdName3 = MdIdCharStr.asMdId("md-3"); |
| 122 | expect(mdService.getMaintenanceDomain(mdName3)) |
| 123 | .andReturn(Optional.empty()).anyTimes(); |
| 124 | replay(mdService); |
| 125 | |
| 126 | final WebTarget wt = target(); |
| 127 | try { |
| 128 | final String response = wt.path("md/" + mdName3).request().get(String.class); |
| 129 | fail("Expected InternalServerErrorException, as MD is unknown"); |
| 130 | } catch (InternalServerErrorException e) { |
| 131 | ByteArrayInputStream is = (ByteArrayInputStream) e.getResponse().getEntity(); |
| 132 | BufferedReader br = new BufferedReader(new InputStreamReader(is)); |
| 133 | String line = null; |
| 134 | StringBuffer sb = new StringBuffer(); |
| 135 | while ((line = br.readLine()) != null) { |
| 136 | sb.append(line); |
| 137 | } |
| 138 | |
| 139 | assertThat(sb.toString(), is("{ \"failure\":" + |
| 140 | "\"java.lang.IllegalArgumentException: MD md-3 not Found\" }")); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | @Test |
| 145 | public void testDeleteMd() throws CfmConfigException { |
| 146 | expect(mdService.deleteMaintenanceDomain(MDNAME1)) |
| 147 | .andReturn(true).anyTimes(); |
| 148 | replay(mdService); |
| 149 | |
| 150 | final WebTarget wt = target(); |
| 151 | final Response response = wt.path("md/" + MDNAME1).request().delete(); |
| 152 | |
| 153 | assertEquals(200, response.getStatus()); |
| 154 | } |
| 155 | |
| 156 | @Test |
| 157 | public void testDeleteMdNotPresent() throws CfmConfigException { |
| 158 | expect(mdService.deleteMaintenanceDomain(MDNAME1)) |
| 159 | .andReturn(false).anyTimes(); |
| 160 | replay(mdService); |
| 161 | |
| 162 | final WebTarget wt = target(); |
| 163 | final Response response = wt.path("md/" + MDNAME1).request().delete(); |
| 164 | |
| 165 | assertEquals(304, response.getStatus()); |
| 166 | } |
| 167 | |
| 168 | @Test |
| 169 | public void testCreateMd() throws CfmConfigException { |
| 170 | MaintenanceDomain md3 = DefaultMaintenanceDomain |
| 171 | .builder(MdIdCharStr.asMdId("md-3")) |
| 172 | .mdLevel(MaintenanceDomain.MdLevel.LEVEL3) |
| 173 | .mdNumericId((short) 3) |
| 174 | .build(); |
| 175 | |
| 176 | expect(mdService.createMaintenanceDomain(mdList.get(1))) |
| 177 | .andReturn(false).anyTimes(); |
| 178 | replay(mdService); |
| 179 | |
| 180 | ObjectMapper mapper = new ObjectMapper(); |
| 181 | CfmCodecContext context = new CfmCodecContext(); |
| 182 | ObjectNode node = mapper.createObjectNode(); |
| 183 | node.set("md", context.codec(MaintenanceDomain.class) |
| 184 | .encode(mdList.get(1), context)); |
| 185 | |
| 186 | |
| 187 | final WebTarget wt = target(); |
| 188 | final Response response = wt.path("md") |
| 189 | .request().post(Entity.json(node.toString())); |
| 190 | |
| 191 | assertEquals(201, response.getStatus()); |
| 192 | } |
| 193 | } |