Jian Li | 5b402c7 | 2018-02-27 14:27:34 +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.codec; |
| 17 | |
| 18 | import com.fasterxml.jackson.databind.JsonNode; |
| 19 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 20 | import com.fasterxml.jackson.databind.node.ObjectNode; |
| 21 | import org.hamcrest.MatcherAssert; |
| 22 | import org.junit.Before; |
| 23 | import org.junit.Test; |
| 24 | import org.onlab.packet.IpAddress; |
| 25 | import org.onosproject.codec.CodecContext; |
| 26 | import org.onosproject.codec.JsonCodec; |
| 27 | import org.onosproject.codec.impl.CodecManager; |
| 28 | import org.onosproject.core.CoreService; |
| 29 | import org.onosproject.net.DeviceId; |
| 30 | import org.onosproject.openstacknode.api.NodeState; |
| 31 | import org.onosproject.openstacknode.api.OpenstackNode; |
| 32 | import org.onosproject.openstacknode.impl.DefaultOpenstackNode; |
| 33 | |
| 34 | import java.io.IOException; |
| 35 | import java.io.InputStream; |
| 36 | import java.util.HashMap; |
| 37 | import java.util.Map; |
| 38 | |
| 39 | import static org.easymock.EasyMock.createMock; |
| 40 | import static org.easymock.EasyMock.expect; |
| 41 | import static org.easymock.EasyMock.replay; |
| 42 | import static org.hamcrest.MatcherAssert.assertThat; |
| 43 | import static org.hamcrest.Matchers.is; |
| 44 | import static org.hamcrest.Matchers.notNullValue; |
| 45 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 46 | import static org.onosproject.openstacknode.codec.OpenstackNodeJsonMatcher.matchesOpenstackNode; |
| 47 | |
| 48 | /** |
| 49 | * Unit tests for OpenstackNode codec. |
| 50 | */ |
| 51 | public class OpenstackNodeCodecTest { |
| 52 | MockCodecContext context; |
| 53 | JsonCodec<OpenstackNode> openstackNodeCodec; |
| 54 | final CoreService mockCoreService = createMock(CoreService.class); |
| 55 | private static final String REST_APP_ID = "org.onosproject.rest"; |
| 56 | |
| 57 | @Before |
| 58 | public void setUp() { |
| 59 | context = new MockCodecContext(); |
| 60 | openstackNodeCodec = new OpenstackNodeCodec(); |
| 61 | assertThat(openstackNodeCodec, notNullValue()); |
| 62 | |
| 63 | expect(mockCoreService.registerApplication(REST_APP_ID)) |
| 64 | .andReturn(APP_ID).anyTimes(); |
| 65 | replay(mockCoreService); |
| 66 | context.registerService(CoreService.class, mockCoreService); |
| 67 | } |
| 68 | |
| 69 | @Test |
| 70 | public void testOpenstackNodeEncode() { |
| 71 | OpenstackNode node = DefaultOpenstackNode.builder() |
| 72 | .hostname("compute") |
| 73 | .type(OpenstackNode.NodeType.COMPUTE) |
| 74 | .state(NodeState.INIT) |
| 75 | .managementIp(IpAddress.valueOf("10.10.10.1")) |
| 76 | .intgBridge(DeviceId.deviceId("br-int")) |
| 77 | .vlanIntf("vxlan") |
| 78 | .dataIp(IpAddress.valueOf("20.20.20.2")) |
| 79 | .build(); |
| 80 | |
| 81 | ObjectNode nodeJson = openstackNodeCodec.encode(node, context); |
| 82 | assertThat(nodeJson, matchesOpenstackNode(node)); |
| 83 | } |
| 84 | |
| 85 | @Test |
| 86 | public void testOpenstackNodeDecode() throws IOException { |
| 87 | OpenstackNode node = getOpenstackNode("OpenstackNode.json"); |
| 88 | |
| 89 | assertThat(node.hostname(), is("compute-01")); |
| 90 | assertThat(node.type().name(), is("COMPUTE")); |
| 91 | assertThat(node.managementIp().toString(), is("172.16.130.4")); |
| 92 | assertThat(node.dataIp().toString(), is("172.16.130.4")); |
| 93 | assertThat(node.intgBridge().toString(), is("of:00000000000000a1")); |
| 94 | assertThat(node.vlanIntf(), is("eth2")); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Reads in an openstack node from the given resource and decodes it. |
| 99 | * |
| 100 | * @param resourceName resource to use to read the JSON for the rule |
| 101 | * @return decoded openstack node |
| 102 | * @throws IOException if processing the resource fails |
| 103 | */ |
| 104 | private OpenstackNode getOpenstackNode(String resourceName) throws IOException { |
| 105 | InputStream jsonStream = OpenstackNodeCodecTest.class.getResourceAsStream(resourceName); |
| 106 | JsonNode json = context.mapper().readTree(jsonStream); |
| 107 | MatcherAssert.assertThat(json, notNullValue()); |
| 108 | OpenstackNode node = openstackNodeCodec.decode((ObjectNode) json, context); |
| 109 | assertThat(node, notNullValue()); |
| 110 | return node; |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Mock codec context for use in codec unit tests. |
| 115 | */ |
| 116 | private class MockCodecContext implements CodecContext { |
| 117 | private final ObjectMapper mapper = new ObjectMapper(); |
| 118 | private final CodecManager manager = new CodecManager(); |
| 119 | private final Map<Class<?>, Object> services = new HashMap<>(); |
| 120 | |
| 121 | /** |
| 122 | * Constructs a new mock codec context. |
| 123 | */ |
| 124 | public MockCodecContext() { |
| 125 | manager.activate(); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public ObjectMapper mapper() { |
| 130 | return mapper; |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | @SuppressWarnings("unchecked") |
| 135 | public <T> JsonCodec<T> codec(Class<T> entityClass) { |
| 136 | return manager.getCodec(entityClass); |
| 137 | } |
| 138 | |
| 139 | @SuppressWarnings("unchecked") |
| 140 | @Override |
| 141 | public <T> T getService(Class<T> serviceClass) { |
| 142 | return (T) services.get(serviceClass); |
| 143 | } |
| 144 | |
| 145 | // for registering mock services |
| 146 | public <T> void registerService(Class<T> serviceClass, T impl) { |
| 147 | services.put(serviceClass, impl); |
| 148 | } |
| 149 | } |
| 150 | } |