Jian Li | c84e3f1 | 2018-02-13 15:12:18 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018-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.openstacknode.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.net.DeviceId; |
| 27 | import org.onosproject.openstacknode.api.NodeState; |
| 28 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 29 | import org.onosproject.openstacknode.api.OpenstackNodeAdminService; |
| 30 | import org.onosproject.openstacknode.api.OpenstackNodeService; |
| 31 | import org.onosproject.openstacknode.codec.OpenstackNodeCodec; |
| 32 | import org.onosproject.openstacknode.impl.DefaultOpenstackNode; |
| 33 | import org.onosproject.rest.resources.ResourceTest; |
| 34 | |
| 35 | import javax.ws.rs.client.Entity; |
| 36 | import javax.ws.rs.client.WebTarget; |
| 37 | import javax.ws.rs.core.MediaType; |
| 38 | import javax.ws.rs.core.Response; |
| 39 | import java.io.InputStream; |
| 40 | |
| 41 | import static org.easymock.EasyMock.anyObject; |
| 42 | import static org.easymock.EasyMock.anyString; |
| 43 | import static org.easymock.EasyMock.createMock; |
| 44 | import static org.easymock.EasyMock.expect; |
| 45 | import static org.easymock.EasyMock.replay; |
| 46 | import static org.easymock.EasyMock.verify; |
| 47 | import static org.hamcrest.Matchers.is; |
| 48 | import static org.junit.Assert.assertThat; |
| 49 | |
| 50 | /** |
| 51 | * Unit test for Openstack node REST API. |
| 52 | */ |
| 53 | public class OpenstackNodeWebResourceTest extends ResourceTest { |
| 54 | |
| 55 | final OpenstackNodeService mockOpenstackNodeService = |
| 56 | createMock(OpenstackNodeService.class); |
| 57 | final OpenstackNodeAdminService mockOpenstackNodeAdminService = |
| 58 | createMock(OpenstackNodeAdminService.class); |
| 59 | private static final String PATH = "configure"; |
| 60 | |
| 61 | /** |
| 62 | * Constructs an openstack node resource test instance. |
| 63 | */ |
| 64 | public OpenstackNodeWebResourceTest() { |
| 65 | super(ResourceConfig.forApplicationClass(OpenstackNodeWebApplication.class)); |
| 66 | } |
| 67 | |
| 68 | private OpenstackNode openstackNode; |
| 69 | |
| 70 | /** |
| 71 | * Sets up the global values for all the tests. |
| 72 | */ |
| 73 | @Before |
| 74 | public void setUpTest() { |
| 75 | final CodecManager codecService = new CodecManager(); |
| 76 | codecService.activate(); |
| 77 | codecService.registerCodec(OpenstackNode.class, new OpenstackNodeCodec()); |
| 78 | ServiceDirectory testDirectory = |
| 79 | new TestServiceDirectory() |
| 80 | .add(OpenstackNodeService.class, mockOpenstackNodeService) |
| 81 | .add(OpenstackNodeAdminService.class, mockOpenstackNodeAdminService) |
| 82 | .add(CodecService.class, codecService); |
| 83 | setServiceDirectory(testDirectory); |
| 84 | |
| 85 | openstackNode = DefaultOpenstackNode.builder() |
| 86 | .hostname("compute-node") |
| 87 | .type(OpenstackNode.NodeType.COMPUTE) |
| 88 | .dataIp(IpAddress.valueOf("10.134.34.222")) |
| 89 | .managementIp(IpAddress.valueOf("10.134.231.30")) |
| 90 | .intgBridge(DeviceId.deviceId("of:00000000000000a1")) |
| 91 | .uplinkPort("eth2") |
| 92 | .state(NodeState.INIT) |
| 93 | .build(); |
| 94 | } |
| 95 | |
| 96 | /** |
Jian Li | b959d01 | 2018-02-19 15:27:58 +0900 | [diff] [blame^] | 97 | * Tests the results of the REST API POST method with creating new nodes operation. |
Jian Li | c84e3f1 | 2018-02-13 15:12:18 +0900 | [diff] [blame] | 98 | */ |
| 99 | @Test |
| 100 | public void testCreateNodesWithCreateOperation() { |
| 101 | expect(mockOpenstackNodeService.node(anyString())).andReturn(null).once(); |
| 102 | replay(mockOpenstackNodeService); |
| 103 | |
| 104 | mockOpenstackNodeAdminService.createNode(anyObject()); |
| 105 | replay(mockOpenstackNodeAdminService); |
| 106 | |
| 107 | final WebTarget wt = target(); |
| 108 | InputStream jsonStream = OpenstackNodeWebResourceTest.class |
| 109 | .getResourceAsStream("openstack-node-gateway-config.json"); |
| 110 | Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE) |
| 111 | .post(Entity.json(jsonStream)); |
| 112 | final int status = response.getStatus(); |
| 113 | |
| 114 | assertThat(status, is(201)); |
| 115 | |
| 116 | verify(mockOpenstackNodeService); |
| 117 | verify(mockOpenstackNodeAdminService); |
| 118 | } |
| 119 | |
| 120 | /** |
Jian Li | b959d01 | 2018-02-19 15:27:58 +0900 | [diff] [blame^] | 121 | * Tests the results of the REST API POST method without creating new nodes operation. |
Jian Li | c84e3f1 | 2018-02-13 15:12:18 +0900 | [diff] [blame] | 122 | */ |
| 123 | @Test |
| 124 | public void testCreateNodesWithoutCreateOperation() { |
| 125 | expect(mockOpenstackNodeService.node(anyString())).andReturn(openstackNode).once(); |
| 126 | replay(mockOpenstackNodeService); |
| 127 | |
| 128 | final WebTarget wt = target(); |
| 129 | InputStream jsonStream = OpenstackNodeWebResourceTest.class |
| 130 | .getResourceAsStream("openstack-node-gateway-config.json"); |
| 131 | Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE) |
| 132 | .post(Entity.json(jsonStream)); |
| 133 | final int status = response.getStatus(); |
| 134 | |
| 135 | assertThat(status, is(201)); |
| 136 | |
| 137 | verify(mockOpenstackNodeService); |
| 138 | } |
| 139 | |
| 140 | /** |
Jian Li | b959d01 | 2018-02-19 15:27:58 +0900 | [diff] [blame^] | 141 | * Tests the results of the REST API PUT method with modifying the nodes. |
Jian Li | c84e3f1 | 2018-02-13 15:12:18 +0900 | [diff] [blame] | 142 | */ |
| 143 | @Test |
| 144 | public void testUpdateNodesWithoutModifyOperation() { |
| 145 | expect(mockOpenstackNodeService.node(anyString())).andReturn(openstackNode).once(); |
| 146 | replay(mockOpenstackNodeService); |
| 147 | |
| 148 | mockOpenstackNodeAdminService.updateNode(anyObject()); |
| 149 | replay(mockOpenstackNodeAdminService); |
| 150 | |
| 151 | final WebTarget wt = target(); |
| 152 | InputStream jsonStream = OpenstackNodeWebResourceTest.class |
| 153 | .getResourceAsStream("openstack-node-gateway-config.json"); |
| 154 | Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE) |
| 155 | .put(Entity.json(jsonStream)); |
| 156 | final int status = response.getStatus(); |
| 157 | |
| 158 | assertThat(status, is(200)); |
| 159 | |
| 160 | verify(mockOpenstackNodeService); |
| 161 | verify(mockOpenstackNodeAdminService); |
| 162 | } |
| 163 | |
| 164 | /** |
Jian Li | b959d01 | 2018-02-19 15:27:58 +0900 | [diff] [blame^] | 165 | * Tests the results of the REST API PUT method without modifying the nodes. |
Jian Li | c84e3f1 | 2018-02-13 15:12:18 +0900 | [diff] [blame] | 166 | */ |
| 167 | @Test |
| 168 | public void testUpdateNodesWithModifyOperation() { |
| 169 | expect(mockOpenstackNodeService.node(anyString())).andReturn(null).once(); |
| 170 | replay(mockOpenstackNodeService); |
| 171 | |
| 172 | final WebTarget wt = target(); |
| 173 | InputStream jsonStream = OpenstackNodeWebResourceTest.class |
| 174 | .getResourceAsStream("openstack-node-gateway-config.json"); |
| 175 | Response response = wt.path(PATH).request(MediaType.APPLICATION_JSON_TYPE) |
| 176 | .put(Entity.json(jsonStream)); |
| 177 | final int status = response.getStatus(); |
| 178 | |
| 179 | assertThat(status, is(304)); |
| 180 | |
| 181 | verify(mockOpenstackNodeService); |
| 182 | } |
Jian Li | b959d01 | 2018-02-19 15:27:58 +0900 | [diff] [blame^] | 183 | |
| 184 | /** |
| 185 | * Tests the results of the REST API DELETE method with deleting the nodes. |
| 186 | */ |
| 187 | @Test |
| 188 | public void testDeleteNodesWithDeletionOperation() { |
| 189 | expect(mockOpenstackNodeService.node(anyString())).andReturn(openstackNode).once(); |
| 190 | replay(mockOpenstackNodeService); |
| 191 | |
| 192 | expect(mockOpenstackNodeAdminService.removeNode(anyString())).andReturn(openstackNode).once(); |
| 193 | replay(mockOpenstackNodeAdminService); |
| 194 | |
| 195 | String location = PATH + "/gateway-node"; |
| 196 | |
| 197 | final WebTarget wt = target(); |
| 198 | Response response = wt.path(location).request( |
| 199 | MediaType.APPLICATION_JSON_TYPE).delete(); |
| 200 | |
| 201 | final int status = response.getStatus(); |
| 202 | |
| 203 | assertThat(status, is(204)); |
| 204 | |
| 205 | verify(mockOpenstackNodeService); |
| 206 | verify(mockOpenstackNodeAdminService); |
| 207 | } |
| 208 | |
| 209 | /** |
| 210 | * Tests the results of the REST API DELETE method without deleting the nodes. |
| 211 | */ |
| 212 | @Test |
| 213 | public void testDeleteNodesWithoutDeletionOperation() { |
| 214 | expect(mockOpenstackNodeService.node(anyString())).andReturn(null).once(); |
| 215 | replay(mockOpenstackNodeService); |
| 216 | |
| 217 | String location = PATH + "/gateway-node"; |
| 218 | |
| 219 | final WebTarget wt = target(); |
| 220 | Response response = wt.path(location).request( |
| 221 | MediaType.APPLICATION_JSON_TYPE).delete(); |
| 222 | |
| 223 | final int status = response.getStatus(); |
| 224 | |
| 225 | assertThat(status, is(304)); |
| 226 | |
| 227 | verify(mockOpenstackNodeService); |
| 228 | } |
Jian Li | c84e3f1 | 2018-02-13 15:12:18 +0900 | [diff] [blame] | 229 | } |