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