blob: 51bdeb52da0f04668d855469fdb8faf2dbd6f022 [file] [log] [blame]
Jian Li47e7af72021-03-05 01:32:04 +09001/*
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 */
16package org.onosproject.kubevirtnetworking.codec;
17
18import com.fasterxml.jackson.databind.JsonNode;
19import com.fasterxml.jackson.databind.ObjectMapper;
20import com.fasterxml.jackson.databind.node.ObjectNode;
21import com.google.common.collect.ImmutableSet;
22import org.hamcrest.MatcherAssert;
23import org.junit.Before;
24import org.junit.Test;
25import org.onlab.packet.IpPrefix;
26import org.onosproject.codec.CodecContext;
27import org.onosproject.codec.JsonCodec;
28import org.onosproject.codec.impl.CodecManager;
29import org.onosproject.core.CoreService;
30import org.onosproject.kubevirtnetworking.api.DefaultKubevirtSecurityGroup;
31import org.onosproject.kubevirtnetworking.api.DefaultKubevirtSecurityGroupRule;
32import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroup;
33import org.onosproject.kubevirtnetworking.api.KubevirtSecurityGroupRule;
34
35import java.io.IOException;
36import java.io.InputStream;
37import java.util.HashMap;
38import java.util.Map;
39
40import static junit.framework.TestCase.assertEquals;
41import static org.easymock.EasyMock.createMock;
42import static org.easymock.EasyMock.expect;
43import static org.easymock.EasyMock.replay;
44import static org.hamcrest.MatcherAssert.assertThat;
45import static org.hamcrest.Matchers.notNullValue;
46import static org.onosproject.kubevirtnetworking.codec.KubevirtSecurityGroupJsonMatcher.matchesKubevirtSecurityGroup;
47import static org.onosproject.net.NetTestTools.APP_ID;
48
49/**
50 * Unit tests for KubevirtSecurityGroup codec.
51 */
52public 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}