Jian Li | daa7d6a | 2021-04-13 17:22:56 +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.IpAddress; |
| 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.DefaultKubevirtLoadBalancer; |
| 31 | import org.onosproject.kubevirtnetworking.api.DefaultKubevirtLoadBalancerRule; |
| 32 | import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancer; |
| 33 | import org.onosproject.kubevirtnetworking.api.KubevirtLoadBalancerRule; |
| 34 | |
| 35 | import java.io.IOException; |
| 36 | import java.io.InputStream; |
| 37 | import java.util.HashMap; |
| 38 | import java.util.Map; |
| 39 | import java.util.Set; |
| 40 | |
| 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.is; |
| 46 | import static org.hamcrest.Matchers.notNullValue; |
| 47 | import static org.onosproject.kubevirtnetworking.codec.KubevirtLoadBalancerJsonMatcher.matchesKubevirtLoadBalancer; |
| 48 | import static org.onosproject.net.NetTestTools.APP_ID; |
| 49 | |
| 50 | /** |
| 51 | * Unit tests for KubevirtLoadBalancer codec. |
| 52 | */ |
| 53 | public final class KubevirtLoadBalancerCodecTest { |
| 54 | |
| 55 | MockCodecContext context; |
| 56 | |
| 57 | JsonCodec<KubevirtLoadBalancer> kubevirtLoadBalancerCodec; |
| 58 | JsonCodec<KubevirtLoadBalancerRule> kubevirtLoadBalancerRuleCodec; |
| 59 | |
| 60 | private static final KubevirtLoadBalancerRule RULE1 = DefaultKubevirtLoadBalancerRule.builder() |
| 61 | .protocol("tcp") |
| 62 | .portRangeMax(8000) |
| 63 | .portRangeMin(7000) |
| 64 | .build(); |
| 65 | private static final KubevirtLoadBalancerRule RULE2 = DefaultKubevirtLoadBalancerRule.builder() |
| 66 | .protocol("udp") |
| 67 | .portRangeMax(9000) |
| 68 | .portRangeMin(8000) |
| 69 | .build(); |
| 70 | |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame^] | 71 | private static final KubevirtLoadBalancerRule RULE3 = DefaultKubevirtLoadBalancerRule.builder() |
| 72 | .protocol("icmp") |
| 73 | .build(); |
| 74 | |
Jian Li | daa7d6a | 2021-04-13 17:22:56 +0900 | [diff] [blame] | 75 | final CoreService mockCoreService = createMock(CoreService.class); |
| 76 | private static final String REST_APP_ID = "org.onosproject.rest"; |
| 77 | |
| 78 | @Before |
| 79 | public void setUp() { |
| 80 | context = new MockCodecContext(); |
| 81 | kubevirtLoadBalancerCodec = new KubevirtLoadBalancerCodec(); |
| 82 | kubevirtLoadBalancerRuleCodec = new KubevirtLoadBalancerRuleCodec(); |
| 83 | |
| 84 | assertThat(kubevirtLoadBalancerCodec, notNullValue()); |
| 85 | assertThat(kubevirtLoadBalancerRuleCodec, notNullValue()); |
| 86 | |
| 87 | expect(mockCoreService.registerApplication(REST_APP_ID)) |
| 88 | .andReturn(APP_ID).anyTimes(); |
| 89 | replay(mockCoreService); |
| 90 | context.registerService(CoreService.class, mockCoreService); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Tests the kubevirt load balancer encoding. |
| 95 | */ |
| 96 | @Test |
| 97 | public void testKubevirtLoadBalancerEncode() { |
| 98 | KubevirtLoadBalancer lb = DefaultKubevirtLoadBalancer.builder() |
| 99 | .name("lb-1") |
| 100 | .networkId("net-1") |
| 101 | .vip(IpAddress.valueOf("10.10.10.10")) |
| 102 | .members(ImmutableSet.of(IpAddress.valueOf("10.10.10.11"), |
| 103 | IpAddress.valueOf("10.10.10.12"))) |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame^] | 104 | .rules(ImmutableSet.of(RULE1, RULE2, RULE3)) |
Jian Li | daa7d6a | 2021-04-13 17:22:56 +0900 | [diff] [blame] | 105 | .description("network load balancer") |
| 106 | .build(); |
| 107 | |
| 108 | ObjectNode lbJson = kubevirtLoadBalancerCodec.encode(lb, context); |
| 109 | assertThat(lbJson, matchesKubevirtLoadBalancer(lb)); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Tests the kubevirt load balancer decoding. |
| 114 | */ |
| 115 | @Test |
| 116 | public void testKubevirtLoadBalancerDecode() throws IOException { |
| 117 | KubevirtLoadBalancer lb = getKubevirtLoadBalancer("KubevirtLoadBalancer.json"); |
| 118 | |
| 119 | assertThat(lb.name(), is("lb-1")); |
| 120 | assertThat(lb.description(), is("Example Load Balancer")); |
| 121 | assertThat(lb.networkId(), is("net-1")); |
| 122 | assertThat(lb.vip(), is(IpAddress.valueOf("10.10.10.10"))); |
| 123 | |
| 124 | Set<IpAddress> expectedMembers = ImmutableSet.of(IpAddress.valueOf("10.10.10.11"), |
| 125 | IpAddress.valueOf("10.10.10.12")); |
| 126 | Set<IpAddress> realMembers = lb.members(); |
| 127 | assertThat(true, is(expectedMembers.containsAll(realMembers))); |
| 128 | assertThat(true, is(realMembers.containsAll(expectedMembers))); |
| 129 | |
Daniel Park | 05a9458 | 2021-05-12 10:57:02 +0900 | [diff] [blame^] | 130 | Set<KubevirtLoadBalancerRule> expectedRules = ImmutableSet.of(RULE1, RULE2, RULE3); |
Jian Li | daa7d6a | 2021-04-13 17:22:56 +0900 | [diff] [blame] | 131 | Set<KubevirtLoadBalancerRule> realRules = lb.rules(); |
| 132 | assertThat(true, is(expectedRules.containsAll(realRules))); |
| 133 | assertThat(true, is(realRules.containsAll(expectedRules))); |
| 134 | } |
| 135 | |
| 136 | private KubevirtLoadBalancer getKubevirtLoadBalancer(String resourceName) throws IOException { |
| 137 | InputStream jsonStream = KubevirtLoadBalancerCodecTest.class.getResourceAsStream(resourceName); |
| 138 | JsonNode json = context.mapper().readTree(jsonStream); |
| 139 | MatcherAssert.assertThat(json, notNullValue()); |
| 140 | KubevirtLoadBalancer lb = kubevirtLoadBalancerCodec.decode((ObjectNode) json, context); |
| 141 | assertThat(lb, notNullValue()); |
| 142 | return lb; |
| 143 | } |
| 144 | |
| 145 | private class MockCodecContext implements CodecContext { |
| 146 | |
| 147 | private final ObjectMapper mapper = new ObjectMapper(); |
| 148 | private final CodecManager manager = new CodecManager(); |
| 149 | private final Map<Class<?>, Object> services = new HashMap<>(); |
| 150 | |
| 151 | /** |
| 152 | * Constructs a new mock codec context. |
| 153 | */ |
| 154 | public MockCodecContext() { |
| 155 | manager.activate(); |
| 156 | } |
| 157 | |
| 158 | @Override |
| 159 | public ObjectMapper mapper() { |
| 160 | return mapper; |
| 161 | } |
| 162 | |
| 163 | @Override |
| 164 | public <T> JsonCodec<T> codec(Class<T> entityClass) { |
| 165 | if (entityClass == KubevirtLoadBalancerRule.class) { |
| 166 | return (JsonCodec<T>) kubevirtLoadBalancerRuleCodec; |
| 167 | } |
| 168 | return manager.getCodec(entityClass); |
| 169 | } |
| 170 | |
| 171 | @Override |
| 172 | public <T> T getService(Class<T> serviceClass) { |
| 173 | return (T) services.get(serviceClass); |
| 174 | } |
| 175 | |
| 176 | // for registering mock services |
| 177 | public <T> void registerService(Class<T> serviceClass, T impl) { |
| 178 | services.put(serviceClass, impl); |
| 179 | } |
| 180 | } |
| 181 | } |