Jian Li | 9ee9c8b | 2019-01-24 11:48:12 +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.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.onlab.packet.MacAddress; |
| 26 | import org.onosproject.codec.CodecContext; |
| 27 | import org.onosproject.codec.JsonCodec; |
| 28 | import org.onosproject.codec.impl.CodecManager; |
| 29 | import org.onosproject.core.CoreService; |
| 30 | import org.onosproject.k8snetworking.api.DefaultK8sPort; |
| 31 | import org.onosproject.k8snetworking.api.K8sPort; |
| 32 | import org.onosproject.net.DeviceId; |
| 33 | import org.onosproject.net.PortNumber; |
| 34 | |
| 35 | import java.io.IOException; |
| 36 | import java.io.InputStream; |
| 37 | import java.util.HashMap; |
| 38 | import java.util.Map; |
| 39 | |
| 40 | import static junit.framework.TestCase.assertEquals; |
| 41 | import static org.easymock.EasyMock.createMock; |
| 42 | import static org.easymock.EasyMock.expect; |
| 43 | import static org.easymock.EasyMock.replay; |
| 44 | import static org.hamcrest.MatcherAssert.assertThat; |
| 45 | import static org.hamcrest.Matchers.notNullValue; |
| 46 | import static org.onosproject.k8snetworking.api.K8sPort.State.ACTIVE; |
| 47 | import static org.onosproject.k8snetworking.codec.K8sPortJsonMatcher.matchesK8sPort; |
| 48 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 49 | |
| 50 | /** |
| 51 | * Unit tests for kubernetes port codec. |
| 52 | */ |
| 53 | public class K8sPortCodecTest { |
| 54 | |
| 55 | MockCodecContext context; |
| 56 | |
| 57 | JsonCodec<K8sPort> k8sPortCodec; |
| 58 | |
| 59 | final CoreService mockCoreService = createMock(CoreService.class); |
| 60 | private static final String REST_APP_ID = "org.onosproject.rest"; |
| 61 | |
| 62 | /** |
| 63 | * Initial setup for this unit test. |
| 64 | */ |
| 65 | @Before |
| 66 | public void setUp() { |
| 67 | context = new MockCodecContext(); |
| 68 | k8sPortCodec = new K8sPortCodec(); |
| 69 | |
| 70 | assertThat(k8sPortCodec, notNullValue()); |
| 71 | |
| 72 | expect(mockCoreService.registerApplication(REST_APP_ID)) |
| 73 | .andReturn(APP_ID).anyTimes(); |
| 74 | replay(mockCoreService); |
| 75 | context.registerService(CoreService.class, mockCoreService); |
| 76 | } |
| 77 | |
| 78 | /** |
| 79 | * Tests the kubernetes port encoding. |
| 80 | */ |
| 81 | @Test |
| 82 | public void testK8sPortEncode() { |
| 83 | K8sPort port = DefaultK8sPort.builder() |
| 84 | .networkId("network-1") |
| 85 | .portId("port-1") |
| 86 | .deviceId(DeviceId.deviceId("of:0000000000000001")) |
| 87 | .ipAddress(IpAddress.valueOf("10.10.10.2")) |
| 88 | .macAddress(MacAddress.valueOf("00:11:22:33:44:55")) |
| 89 | .portNumber(PortNumber.portNumber(1)) |
| 90 | .state(ACTIVE) |
| 91 | .build(); |
| 92 | |
| 93 | ObjectNode nodeJson = k8sPortCodec.encode(port, context); |
| 94 | assertThat(nodeJson, matchesK8sPort(port)); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Tests the kubernetes port decoding. |
| 99 | */ |
| 100 | @Test |
| 101 | public void testK8sPortDecode() throws IOException { |
| 102 | K8sPort port = getK8sPort("K8sPort.json"); |
| 103 | |
| 104 | assertEquals("network-1", port.networkId()); |
| 105 | assertEquals("port-1", port.portId()); |
| 106 | assertEquals("00:11:22:33:44:55", port.macAddress().toString()); |
| 107 | assertEquals("10.10.10.10", port.ipAddress().toString()); |
| 108 | assertEquals("of:0000000000000001", port.deviceId().toString()); |
| 109 | assertEquals("1", port.portNumber().toString()); |
| 110 | assertEquals("ACTIVE", port.state().name()); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Reads in an kubernetes port from the given resource and decodes it. |
| 115 | * |
| 116 | * @param resourceName resource to use to read the JSON for the rule |
| 117 | * @return decoded kubernetes port |
| 118 | * @throws IOException if processing the resource fails |
| 119 | */ |
| 120 | private K8sPort getK8sPort(String resourceName) throws IOException { |
| 121 | InputStream jsonStream = K8sPortCodecTest.class.getResourceAsStream(resourceName); |
| 122 | JsonNode json = context.mapper().readTree(jsonStream); |
| 123 | MatcherAssert.assertThat(json, notNullValue()); |
| 124 | K8sPort port = k8sPortCodec.decode((ObjectNode) json, context); |
| 125 | assertThat(port, notNullValue()); |
| 126 | return port; |
| 127 | } |
| 128 | |
| 129 | private class MockCodecContext implements CodecContext { |
| 130 | private final ObjectMapper mapper = new ObjectMapper(); |
| 131 | private final CodecManager manager = new CodecManager(); |
| 132 | private final Map<Class<?>, Object> services = new HashMap<>(); |
| 133 | |
| 134 | /** |
| 135 | * Constructs a new mock codec context. |
| 136 | */ |
| 137 | public MockCodecContext() { |
| 138 | manager.activate(); |
| 139 | } |
| 140 | |
| 141 | @Override |
| 142 | public ObjectMapper mapper() { |
| 143 | return mapper; |
| 144 | } |
| 145 | |
| 146 | @Override |
| 147 | @SuppressWarnings("unchecked") |
| 148 | public <T> JsonCodec<T> codec(Class<T> entityClass) { |
| 149 | if (entityClass == K8sPort.class) { |
| 150 | return (JsonCodec<T>) k8sPortCodec; |
| 151 | } |
| 152 | return manager.getCodec(entityClass); |
| 153 | } |
| 154 | |
| 155 | @SuppressWarnings("unchecked") |
| 156 | @Override |
| 157 | public <T> T getService(Class<T> serviceClass) { |
| 158 | return (T) services.get(serviceClass); |
| 159 | } |
| 160 | |
| 161 | // for registering mock services |
| 162 | public <T> void registerService(Class<T> serviceClass, T impl) { |
| 163 | services.put(serviceClass, impl); |
| 164 | } |
| 165 | } |
| 166 | } |