Jian Li | 1c10cf2 | 2021-03-05 01:32:04 +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.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 com.google.common.collect.ImmutableSet; |
| 22 | import org.hamcrest.MatcherAssert; |
| 23 | import org.junit.Before; |
| 24 | import org.junit.Test; |
| 25 | import org.onlab.packet.IpPrefix; |
| 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.kubevirtnetworking.api.DefaultKubevirtSecurityGroup; |
| 31 | import org.onosproject.kubevirtnetworking.api.DefaultKubevirtSecurityGroupRule; |
| 32 | import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroup; |
| 33 | import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroupRule; |
| 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.kubevirtnetworking.codec.KubevirtSecurityGroupJsonMatcher.matchesKubevirtSecurityGroup; |
| 47 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 48 | |
| 49 | /** |
| 50 | * Unit tests for KubevirtSecurityGroup codec. |
| 51 | */ |
| 52 | public final class KubevirtSecurityGroupCodecTest { |
| 53 | |
| 54 | MockCodecContext context; |
| 55 | |
| 56 | JsonCodec<KubevirtSecurityGroup> kubevirtSecurityGroupCodec; |
| 57 | JsonCodec<KubevirtSecurityGroupRule> kubevirtSecurityGroupRuleCodec; |
| 58 | |
| 59 | final CoreService mockCoreService = createMock(CoreService.class); |
| 60 | private static final String REST_APP_ID = "org.onosproject.rest"; |
| 61 | |
| 62 | @Before |
| 63 | public void setUp() { |
| 64 | context = new MockCodecContext(); |
| 65 | kubevirtSecurityGroupCodec = new KubevirtSecurityGroupCodec(); |
| 66 | kubevirtSecurityGroupRuleCodec = new KubevirtSecurityGroupRuleCodec(); |
| 67 | |
| 68 | assertThat(kubevirtSecurityGroupCodec, notNullValue()); |
| 69 | assertThat(kubevirtSecurityGroupRuleCodec, notNullValue()); |
| 70 | expect(mockCoreService.registerApplication(REST_APP_ID)) |
| 71 | .andReturn(APP_ID).anyTimes(); |
| 72 | replay(mockCoreService); |
| 73 | context.registerService(CoreService.class, mockCoreService); |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Tests the kubevirt security group encoding. |
| 78 | */ |
| 79 | @Test |
| 80 | public void testKubevirtSecurityGroupEncode() { |
| 81 | KubevirtSecurityGroupRule rule = DefaultKubevirtSecurityGroupRule.builder() |
| 82 | .id("sgr-1") |
| 83 | .securityGroupId("sg-1") |
| 84 | .direction("ingress") |
| 85 | .etherType("IPv4") |
| 86 | .portRangeMin(0) |
| 87 | .portRangeMax(80) |
| 88 | .protocol("tcp") |
| 89 | .remoteIpPrefix(IpPrefix.valueOf("0.0.0.0/0")) |
| 90 | .remoteGroupId("g-1") |
| 91 | .build(); |
| 92 | |
| 93 | KubevirtSecurityGroup sg = DefaultKubevirtSecurityGroup.builder() |
| 94 | .id("sg-1") |
| 95 | .name("sg") |
| 96 | .description("example-sg") |
| 97 | .rules(ImmutableSet.of(rule)) |
| 98 | .build(); |
| 99 | |
| 100 | ObjectNode sgJson = kubevirtSecurityGroupCodec.encode(sg, context); |
| 101 | assertThat(sgJson, matchesKubevirtSecurityGroup(sg)); |
| 102 | } |
| 103 | |
| 104 | /** |
| 105 | * Tests the kubevirt security group decoding. |
| 106 | */ |
| 107 | @Test |
| 108 | public void testKubevirtSecurityGroupDecode() throws IOException { |
| 109 | KubevirtSecurityGroup sg = getKubevirtSecurityGroup("KubevirtSecurityGroup.json"); |
| 110 | KubevirtSecurityGroupRule rule = sg.rules().stream().findAny().orElse(null); |
| 111 | |
| 112 | assertEquals("sg-1", sg.id()); |
| 113 | assertEquals("sg", sg.name()); |
| 114 | assertEquals("example-sg", sg.description()); |
| 115 | |
| 116 | assertEquals("sgr-1", rule.id()); |
| 117 | assertEquals("sg-1", rule.securityGroupId()); |
| 118 | assertEquals("ingress", rule.direction()); |
| 119 | assertEquals("IPv4", rule.etherType()); |
| 120 | assertEquals((Integer) 80, rule.portRangeMax()); |
| 121 | assertEquals((Integer) 0, rule.portRangeMin()); |
| 122 | assertEquals("tcp", rule.protocol()); |
| 123 | assertEquals("0.0.0.0/0", rule.remoteIpPrefix().toString()); |
| 124 | assertEquals("g-1", rule.remoteGroupId()); |
| 125 | } |
| 126 | |
| 127 | private KubevirtSecurityGroup getKubevirtSecurityGroup(String resourceName) throws IOException { |
| 128 | InputStream jsonStream = KubevirtSecurityGroupCodecTest.class.getResourceAsStream(resourceName); |
| 129 | JsonNode json = context.mapper().readTree(jsonStream); |
| 130 | MatcherAssert.assertThat(json, notNullValue()); |
| 131 | KubevirtSecurityGroup sg = kubevirtSecurityGroupCodec.decode((ObjectNode) json, context); |
| 132 | assertThat(sg, notNullValue()); |
| 133 | return sg; |
| 134 | } |
| 135 | |
| 136 | private class MockCodecContext implements CodecContext { |
| 137 | |
| 138 | private final ObjectMapper mapper = new ObjectMapper(); |
| 139 | private final CodecManager manager = new CodecManager(); |
| 140 | private final Map<Class<?>, Object> services = new HashMap<>(); |
| 141 | |
| 142 | /** |
| 143 | * Constructs a new mock codec context. |
| 144 | */ |
| 145 | public MockCodecContext() { |
| 146 | manager.activate(); |
| 147 | } |
| 148 | |
| 149 | @Override |
| 150 | public ObjectMapper mapper() { |
| 151 | return mapper; |
| 152 | } |
| 153 | |
| 154 | @Override |
| 155 | public <T> JsonCodec<T> codec(Class<T> entityClass) { |
| 156 | if (entityClass == KubevirtSecurityGroupRule.class) { |
| 157 | return (JsonCodec<T>) kubevirtSecurityGroupRuleCodec; |
| 158 | } |
| 159 | |
| 160 | return manager.getCodec(entityClass); |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public <T> T getService(Class<T> serviceClass) { |
| 165 | return (T) services.get(serviceClass); |
| 166 | } |
| 167 | |
| 168 | // for registering mock services |
| 169 | public <T> void registerService(Class<T> serviceClass, T impl) { |
| 170 | services.put(serviceClass, impl); |
| 171 | } |
| 172 | } |
| 173 | } |