kishore | efa3db5 | 2020-03-23 16:09:26 +0530 | [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 | |
| 17 | package org.opencord.kafka.impl; |
| 18 | |
| 19 | import com.fasterxml.jackson.databind.JsonNode; |
| 20 | import com.fasterxml.jackson.databind.ObjectMapper; |
| 21 | import org.junit.jupiter.api.AfterEach; |
| 22 | import org.junit.jupiter.api.BeforeEach; |
| 23 | import org.junit.jupiter.api.Test; |
| 24 | import org.onosproject.cluster.ClusterServiceAdapter; |
| 25 | import org.onosproject.core.ApplicationId; |
| 26 | import org.onosproject.core.CoreServiceAdapter; |
| 27 | import org.onosproject.net.config.Config; |
| 28 | import org.onosproject.net.config.ConfigApplyDelegate; |
| 29 | import org.onosproject.net.config.NetworkConfigEvent; |
| 30 | import org.onosproject.net.config.NetworkConfigListener; |
| 31 | import org.onosproject.net.config.NetworkConfigRegistryAdapter; |
| 32 | |
| 33 | import java.io.InputStream; |
| 34 | import java.util.ArrayList; |
| 35 | import java.util.HashMap; |
| 36 | import java.util.List; |
| 37 | import java.util.Map; |
| 38 | |
| 39 | import static org.junit.Assert.assertEquals; |
| 40 | |
| 41 | /** |
| 42 | * set of unit test to verify KafkaIntegration. |
| 43 | */ |
| 44 | class KafkaIntegrationTest { |
| 45 | private static final String ASSERT_MESSAGE = "Config not updated"; |
| 46 | private static final String BOOTSTRAP_SERVERS = "localhost:9092"; |
| 47 | protected NetworkConfigListener configListener; |
| 48 | protected ConfigApplyDelegate delegate; |
| 49 | protected ObjectMapper mapper; |
| 50 | protected ApplicationId subject; |
| 51 | protected NetworkConfigEvent event; |
| 52 | private KafkaConfig config = new KafkaConfig(); |
| 53 | private KafkaIntegration kafkaIntegration; |
| 54 | |
| 55 | @BeforeEach |
| 56 | public void setUp() throws Exception { |
| 57 | kafkaIntegration = new KafkaIntegration(); |
| 58 | kafkaIntegration.coreService = new MockCoreService(); |
| 59 | kafkaIntegration.clusterService = new ClusterServiceAdapter(); |
| 60 | setupConfig("/localKafkaConfig.json"); |
| 61 | kafkaIntegration.activate(); |
| 62 | } |
| 63 | |
| 64 | @AfterEach |
| 65 | public void tearDown() { |
| 66 | kafkaIntegration.deactivate(); |
| 67 | kafkaIntegration = null; |
| 68 | } |
| 69 | |
| 70 | /** |
| 71 | * Local configuration verification using Resources file. |
| 72 | */ |
| 73 | @Test |
| 74 | void testConfigAdded() { |
| 75 | event = new NetworkConfigEvent(NetworkConfigEvent.Type.CONFIG_ADDED, subject, |
| 76 | config, null, KafkaConfig.class); |
| 77 | configListener.event(event); |
| 78 | KafkaConfig expectedConfig = kafkaIntegration.configRegistry |
| 79 | .getConfig(kafkaIntegration.coreService.getAppId(KafkaIntegration.APP_NAME), |
| 80 | KafkaConfig.class); |
| 81 | assertEquals(ASSERT_MESSAGE, BOOTSTRAP_SERVERS, expectedConfig.getBootstrapServers()); |
| 82 | } |
| 83 | |
| 84 | public void setupConfig(String localConfig) throws Exception { |
| 85 | delegate = new MockConfigDelegate(); |
| 86 | mapper = new ObjectMapper(); |
| 87 | subject = kafkaIntegration.coreService.registerApplication(KafkaIntegration.APP_NAME); |
| 88 | config.init(subject, "kafka-local-mode-test", node(localConfig), mapper, delegate); |
| 89 | kafkaIntegration.configRegistry = new MockNetworkConfigRegistry(subject, config); |
| 90 | } |
| 91 | |
| 92 | protected JsonNode node(String jsonFile) throws Exception { |
| 93 | final InputStream jsonStream = KafkaConfig.class.getResourceAsStream(jsonFile); |
| 94 | return mapper.readTree(jsonStream); |
| 95 | } |
| 96 | |
| 97 | /** |
| 98 | * Mocks an ONOS configuration delegate to allow JSON based configuration to |
| 99 | * be tested. |
| 100 | */ |
| 101 | private static final class MockConfigDelegate implements ConfigApplyDelegate { |
| 102 | @Override |
Matteo Scandolo | 1298ffc | 2020-12-10 12:46:55 -0800 | [diff] [blame] | 103 | public void onApply(@SuppressWarnings({"rawtypes", "TypeParameterUnusedInFormals"}) Config config) { |
kishore | efa3db5 | 2020-03-23 16:09:26 +0530 | [diff] [blame] | 104 | config.apply(); |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | /** |
| 109 | * Mocks Core service adapter. |
| 110 | */ |
| 111 | private static class MockCoreService extends CoreServiceAdapter { |
| 112 | |
| 113 | private List<ApplicationId> idList = new ArrayList<>(); |
| 114 | private Map<String, ApplicationId> idMap = new HashMap<>(); |
| 115 | |
| 116 | /* |
| 117 | * (non-Javadoc) |
| 118 | * |
| 119 | * @see |
| 120 | * org.onosproject.core.CoreServiceAdapter#getAppId(java.lang.Short) |
| 121 | */ |
| 122 | @Override |
| 123 | public ApplicationId getAppId(Short id) { |
| 124 | if (id >= idList.size()) { |
| 125 | return null; |
| 126 | } |
| 127 | return idList.get(id); |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * (non-Javadoc) |
| 132 | * |
| 133 | * @see |
| 134 | * org.onosproject.core.CoreServiceAdapter#getAppId(java.lang.String) |
| 135 | */ |
| 136 | @Override |
| 137 | public ApplicationId getAppId(String name) { |
| 138 | return idMap.get(name); |
| 139 | } |
| 140 | |
| 141 | /* |
| 142 | * (non-Javadoc) |
| 143 | * |
| 144 | * @see |
| 145 | * org.onosproject.core.CoreServiceAdapter#registerApplication(java.lang |
| 146 | * .String) |
| 147 | */ |
| 148 | @Override |
| 149 | public ApplicationId registerApplication(String name) { |
| 150 | ApplicationId appId = idMap.get(name); |
| 151 | if (appId == null) { |
| 152 | appId = new MockApplicationId((short) idList.size(), name); |
| 153 | idList.add(appId); |
| 154 | idMap.put(name, appId); |
| 155 | } |
| 156 | return appId; |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /* |
| 161 | Mocks application id. |
| 162 | */ |
| 163 | private static class MockApplicationId implements ApplicationId { |
| 164 | |
| 165 | private final short id; |
| 166 | private final String name; |
| 167 | |
| 168 | public MockApplicationId(short id, String name) { |
| 169 | this.id = id; |
| 170 | this.name = name; |
| 171 | } |
| 172 | |
| 173 | @Override |
| 174 | public short id() { |
| 175 | return id; |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public String name() { |
| 180 | return name; |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | |
| 185 | /** |
| 186 | * Mocks the ONOS network configuration registry so that the application |
| 187 | * under test can access a JSON defined configuration. |
| 188 | */ |
| 189 | private class MockNetworkConfigRegistry<S> extends NetworkConfigRegistryAdapter { |
| 190 | private final KafkaConfig config; |
| 191 | |
| 192 | public MockNetworkConfigRegistry(final S subject, final KafkaConfig config) { |
| 193 | this.config = config; |
| 194 | } |
| 195 | |
Matteo Scandolo | 1298ffc | 2020-12-10 12:46:55 -0800 | [diff] [blame] | 196 | @SuppressWarnings({"unchecked", "TypeParameterUnusedInFormals"}) |
kishore | efa3db5 | 2020-03-23 16:09:26 +0530 | [diff] [blame] | 197 | @Override |
| 198 | public <S, C extends Config<S>> C getConfig(final S subject, final Class<C> configClass) { |
| 199 | return (C) config; |
| 200 | } |
| 201 | |
| 202 | @Override |
| 203 | public void addListener(NetworkConfigListener listener) { |
| 204 | configListener = listener; |
| 205 | } |
| 206 | } |
| 207 | } |