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