Jian Li | f97a07e | 2021-01-13 18:05:00 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021-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.kubevirtnetworking.web; |
| 17 | |
| 18 | import org.glassfish.jersey.server.ResourceConfig; |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.osgi.ServiceDirectory; |
| 22 | import org.onlab.osgi.TestServiceDirectory; |
| 23 | import org.onlab.packet.IpAddress; |
| 24 | import org.onosproject.codec.CodecService; |
| 25 | import org.onosproject.codec.impl.CodecManager; |
| 26 | import org.onosproject.kubevirtnetworking.api.DefaultKubevirtNetwork; |
| 27 | import org.onosproject.kubevirtnetworking.api.KubevirtHostRoute; |
| 28 | import org.onosproject.kubevirtnetworking.api.KubevirtIpPool; |
| 29 | import org.onosproject.kubevirtnetworking.api.KubevirtNetwork; |
| 30 | import org.onosproject.kubevirtnetworking.api.KubevirtNetworkAdminService; |
| 31 | import org.onosproject.kubevirtnetworking.codec.KubevirtHostRouteCodec; |
| 32 | import org.onosproject.kubevirtnetworking.codec.KubevirtIpPoolCodec; |
| 33 | import org.onosproject.kubevirtnetworking.codec.KubevirtNetworkCodec; |
| 34 | import org.onosproject.rest.resources.ResourceTest; |
| 35 | |
| 36 | import javax.ws.rs.client.Entity; |
| 37 | import javax.ws.rs.client.WebTarget; |
| 38 | import javax.ws.rs.core.MediaType; |
| 39 | import javax.ws.rs.core.Response; |
| 40 | import java.io.InputStream; |
| 41 | |
| 42 | import static org.easymock.EasyMock.anyObject; |
| 43 | import static org.easymock.EasyMock.anyString; |
| 44 | import static org.easymock.EasyMock.createMock; |
| 45 | import static org.easymock.EasyMock.expect; |
| 46 | import static org.easymock.EasyMock.replay; |
| 47 | import static org.easymock.EasyMock.verify; |
| 48 | import static org.hamcrest.Matchers.is; |
| 49 | import static org.junit.Assert.assertThat; |
| 50 | |
| 51 | /** |
| 52 | * Unit tests for kubevirt network REST API. |
| 53 | */ |
| 54 | public class KubevirtNetworkWebResourceTest extends ResourceTest { |
| 55 | |
| 56 | final KubevirtNetworkAdminService mockAdminService = createMock(KubevirtNetworkAdminService.class); |
| 57 | private static final String PATH = "network"; |
| 58 | |
| 59 | private KubevirtNetwork network; |
| 60 | |
| 61 | /** |
| 62 | * Constructs a kubernetes networking resource test instance. |
| 63 | */ |
| 64 | public KubevirtNetworkWebResourceTest() { |
| 65 | super(ResourceConfig.forApplicationClass(KubevirtNetworkingWebApplication.class)); |
| 66 | } |
| 67 | |
| 68 | /** |
| 69 | * Sets up the global values for all the tests. |
| 70 | */ |
| 71 | @Before |
| 72 | public void setUpTest() { |
| 73 | final CodecManager codecService = new CodecManager(); |
| 74 | codecService.activate(); |
| 75 | codecService.registerCodec(KubevirtHostRoute.class, new KubevirtHostRouteCodec()); |
| 76 | codecService.registerCodec(KubevirtIpPool.class, new KubevirtIpPoolCodec()); |
| 77 | codecService.registerCodec(KubevirtNetwork.class, new KubevirtNetworkCodec()); |
| 78 | ServiceDirectory testDirectory = |
| 79 | new TestServiceDirectory() |
| 80 | .add(KubevirtNetworkAdminService.class, mockAdminService) |
| 81 | .add(CodecService.class, codecService); |
| 82 | setServiceDirectory(testDirectory); |
| 83 | |
| 84 | network = DefaultKubevirtNetwork.builder() |
| 85 | .networkId("network") |
| 86 | .name("network") |
| 87 | .type(KubevirtNetwork.Type.FLAT) |
| 88 | .cidr("10.10.10.0/24") |
| 89 | .mtu(1500) |
| 90 | .gatewayIp(IpAddress.valueOf("10.10.10.1")) |
| 91 | .ipPool(new KubevirtIpPool(IpAddress.valueOf("10.10.10.100"), |
| 92 | IpAddress.valueOf("10.10.10.200"))) |
| 93 | .build(); |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * Tests the results of the REST API POST method with creating new network operation. |
| 98 | */ |
| 99 | @Test |
| 100 | public void testCreateNetworkWithCreateOperation() { |
| 101 | mockAdminService.createNetwork(anyObject()); |
| 102 | replay(mockAdminService); |
| 103 | |
| 104 | final WebTarget wt = target(); |
| 105 | InputStream jsonStream = KubevirtNetworkWebResourceTest.class |
| 106 | .getResourceAsStream("kubevirt-network.json"); |
| 107 | Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE) |
| 108 | .post(Entity.json(jsonStream)); |
| 109 | final int status = response.getStatus(); |
| 110 | |
| 111 | assertThat(status, is(201)); |
| 112 | |
| 113 | verify(mockAdminService); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Tests the results of the REST API PUT method with modifying the network. |
| 118 | */ |
| 119 | @Test |
| 120 | public void testUpdateNetworkWithModifyOperation() { |
| 121 | mockAdminService.updateNetwork(anyObject()); |
| 122 | replay(mockAdminService); |
| 123 | |
| 124 | String location = PATH + "/network"; |
| 125 | |
| 126 | final WebTarget wt = target(); |
| 127 | InputStream jsonStream = KubevirtNetworkWebResourceTest.class |
| 128 | .getResourceAsStream("kubevirt-network.json"); |
| 129 | Response response = wt.path(location) |
| 130 | .request(MediaType.APPLICATION_JSON_TYPE) |
| 131 | .put(Entity.json(jsonStream)); |
| 132 | final int status = response.getStatus(); |
| 133 | |
| 134 | assertThat(status, is(200)); |
| 135 | |
| 136 | verify(mockAdminService); |
| 137 | } |
| 138 | |
| 139 | /** |
| 140 | * Tests the results of the REST API DELETE method with deleting the network. |
| 141 | */ |
| 142 | @Test |
| 143 | public void testDeleteNetworkWithDeletionOperation() { |
| 144 | mockAdminService.removeNetwork(anyString()); |
| 145 | replay(mockAdminService); |
| 146 | |
| 147 | String location = PATH + "/network"; |
| 148 | |
| 149 | final WebTarget wt = target(); |
| 150 | Response response = wt.path(location).request( |
| 151 | MediaType.APPLICATION_JSON_TYPE).delete(); |
| 152 | |
| 153 | final int status = response.getStatus(); |
| 154 | |
| 155 | assertThat(status, is(204)); |
| 156 | |
| 157 | verify(mockAdminService); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Tests the results of checking network existence. |
| 162 | */ |
| 163 | @Test |
| 164 | public void testHasNetworkWithValidNetwork() { |
| 165 | expect(mockAdminService.network(anyString())).andReturn(network); |
| 166 | replay(mockAdminService); |
| 167 | |
| 168 | String location = PATH + "/exist/network"; |
| 169 | |
| 170 | final WebTarget wt = target(); |
| 171 | String response = wt.path(location).request( |
| 172 | MediaType.APPLICATION_JSON_TYPE).get(String.class); |
| 173 | |
| 174 | assertThat(response, is("{\"result\":true}")); |
| 175 | |
| 176 | verify((mockAdminService)); |
| 177 | } |
| 178 | |
| 179 | /** |
| 180 | * Tests the results of checking network existence. |
| 181 | */ |
| 182 | @Test |
| 183 | public void testHasNetworkWithNullNetwork() { |
| 184 | expect(mockAdminService.network(anyString())).andReturn(null); |
| 185 | replay(mockAdminService); |
| 186 | |
| 187 | String location = PATH + "/exist/network"; |
| 188 | |
| 189 | final WebTarget wt = target(); |
| 190 | String response = wt.path(location).request( |
| 191 | MediaType.APPLICATION_JSON_TYPE).get(String.class); |
| 192 | |
| 193 | assertThat(response, is("{\"result\":false}")); |
| 194 | |
| 195 | verify((mockAdminService)); |
| 196 | } |
| 197 | } |