blob: 511fa6ddb5bc88b6d4fbab941bda9fceb39cdffc [file] [log] [blame]
Jian Li69600e02018-12-24 13:21:18 +09001/*
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 */
16package org.onosproject.openstacktelemetry.impl;
17
18import com.google.common.collect.Maps;
19import org.junit.After;
20import org.junit.Before;
21import org.junit.Test;
22import org.onosproject.TestApplicationId;
23import org.onosproject.core.ApplicationId;
24import org.onosproject.core.CoreServiceAdapter;
25import org.onosproject.openstacktelemetry.api.config.TelemetryConfig;
26import org.onosproject.openstacktelemetry.api.config.TelemetryConfig.ConfigType;
27import org.onosproject.store.service.TestStorageService;
28
29import java.util.Map;
30
31import static org.junit.Assert.assertEquals;
32import static org.junit.Assert.assertFalse;
33import static org.junit.Assert.assertTrue;
34
35/**
36 * Distributed TelemetryConfig store test suite.
37 */
38public class DistributedTelemetryConfigStoreTest {
39
40 private static final String NAME_1 = "grpc";
41 private static final String NAME_2 = "kafka";
42
43 private static final ConfigType TYPE_1 = ConfigType.GRPC;
44 private static final ConfigType TYPE_2 = ConfigType.KAFKA;
45
46 private static final String MANUFACTURER_1 = "grpc.io";
47 private static final String MANUFACTURER_2 = "kafka.apache.org";
48
49 private static final String SW_VERSION_1 = "1.0";
50 private static final String SW_VERSION_2 = "1.0";
51
52 private static final Map<String, String> PROP_1 = Maps.newConcurrentMap();
53 private static final Map<String, String> PROP_2 = Maps.newConcurrentMap();
54
55 private static final String PROP_1_KEY_1 = "key11";
56 private static final String PROP_1_KEY_2 = "key12";
57 private static final String PROP_1_VALUE_1 = "value11";
58 private static final String PROP_1_VALUE_2 = "value12";
59 private static final String PROP_2_KEY_1 = "key21";
60 private static final String PROP_2_KEY_2 = "key22";
61 private static final String PROP_2_VALUE_1 = "value21";
62 private static final String PROP_2_VALUE_2 = "value22";
63
64 private static final boolean ENABLED_1 = true;
65 private static final boolean ENABLED_2 = false;
66
67 private TelemetryConfig config1;
68 private TelemetryConfig config2;
69
70 private DistributedTelemetryConfigStore configStore;
71
72 /**
73 * Sets up the telemetry config store and the storage service test harness.
74 */
75 @Before
76 public void setUp() {
77 configStore = new DistributedTelemetryConfigStore();
78 configStore.storageService = new TestStorageService();
79 configStore.coreService = new TestCoreService();
80 configStore.setDelegate(event -> {
81 });
82 configStore.activate();
83
84 initTelemetryConfigs();
85 }
86
87 /**
88 * Tears down the telemetry config store.
89 */
90 @After
91 public void tearDown() {
92 configStore.deactivate();
93 }
94
95 /**
96 * Tests adding, removing and getting.
97 */
98 @Test
99 public void basics() {
100 configStore.createTelemetryConfig(config1);
101 assertTrue("There should be one telemetry config in the set.",
102 configStore.telemetryConfigs().contains(config1));
103 assertTrue("The same telemetry config should be returned.",
104 configStore.telemetryConfigsByType(ConfigType.GRPC).contains(config1));
105 assertEquals("The telemetry config should be the same.",
106 configStore.telemetryConfig(NAME_1), config1);
107 configStore.removeTelemetryConfig(NAME_1);
108 assertFalse("There should be no telemetry config in the set.",
109 configStore.telemetryConfigs().contains(config1));
110
111 configStore.createTelemetryConfig(config1);
112 configStore.createTelemetryConfig(config2);
113 assertEquals("There should be two configs in the sets.",
114 configStore.telemetryConfigs().size(), 2);
115 }
116
117 /**
118 * Test core service; For generate test application ID.
119 */
120 public class TestCoreService extends CoreServiceAdapter {
121 @Override
122 public ApplicationId registerApplication(String name) {
123 return TestApplicationId.create(name);
124 }
125 }
126
127 private void initTelemetryConfigs() {
128 PROP_1.put(PROP_1_KEY_1, PROP_1_VALUE_1);
129 PROP_1.put(PROP_1_KEY_2, PROP_1_VALUE_2);
130 PROP_2.put(PROP_2_KEY_1, PROP_2_VALUE_1);
131 PROP_2.put(PROP_2_KEY_2, PROP_2_VALUE_2);
132
133 config1 = new DefaultTelemetryConfig(NAME_1, TYPE_1, null,
134 MANUFACTURER_1, SW_VERSION_1, ENABLED_1, PROP_1);
135 config2 = new DefaultTelemetryConfig(NAME_2, TYPE_2, null,
136 MANUFACTURER_2, SW_VERSION_2, ENABLED_2, PROP_2);
137 }
138}