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