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