2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright 2019 China Mobile
6 *=================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.datalake.feeder.service;
23 import static org.mockito.Mockito.when;
25 import java.lang.reflect.InvocationTargetException;
26 import java.lang.reflect.Method;
27 import java.util.ArrayList;
28 import java.util.List;
30 import org.apache.commons.lang3.tuple.Pair;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.InjectMocks;
34 import org.mockito.Mock;
35 import org.mockito.junit.MockitoJUnitRunner;
36 import org.onap.datalake.feeder.config.ApplicationConfiguration;
37 import org.onap.datalake.feeder.dto.TopicConfig;
38 import org.springframework.context.ApplicationContext;
46 @RunWith(MockitoJUnitRunner.class)
47 public class StoreServiceTest {
50 private StoreService storeService = new StoreService();
53 private ApplicationContext context;
56 private ApplicationConfiguration config;
59 private TopicConfigPollingService configPollingService;
62 private MongodbService mongodbService;
65 private CouchbaseService couchbaseService;
68 private ElasticsearchService elasticsearchService;
71 private HdfsService hdfsService;
73 public void testInit() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
74 Method init = storeService.getClass().getDeclaredMethod("init");
75 init.setAccessible(true);
76 init.invoke(storeService);
79 private TopicConfig createTopicConfig(String topicStr, String type) {
81 TopicConfig topicConfig = new TopicConfig();
82 topicConfig.setName(topicStr);
83 topicConfig.setDataFormat(type);
84 topicConfig.setSaveRaw(true);
86 when(configPollingService.getEffectiveTopicConfig(topicStr)).thenReturn(topicConfig);
92 public void saveMessages() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
95 TopicConfig topicConfig = createTopicConfig("test1", "JSON");
96 topicConfig.setAggregateArrayPath("/test");
97 topicConfig.setFlattenArrayPath("/test");
99 topicConfig = createTopicConfig("test2", "XML");
100 topicConfig.setSaveRaw(false);
102 topicConfig = createTopicConfig("test3", "YAML");
104 topicConfig.setSinkdbs(new ArrayList<>());
105 topicConfig.getSinkdbs().add("Elasticsearch");
106 topicConfig.getSinkdbs().add("Couchbase");
107 topicConfig.getSinkdbs().add("Druid");
108 topicConfig.getSinkdbs().add("MongoDB");
109 topicConfig.getSinkdbs().add("HDFS");
111 createTopicConfig("test4", "TEXT");
113 when(config.getTimestampLabel()).thenReturn("ts");
114 when(config.getRawDataLabel()).thenReturn("raw");
117 List<Pair<Long, String>> messages = new ArrayList<>();
118 messages.add(Pair.of(100L, "{test: 1}"));
120 storeService.saveMessages("test1", messages);
123 List<Pair<Long, String>> messagesXml = new ArrayList<>();
124 messagesXml.add(Pair.of(100L, "<test></test>"));
125 messagesXml.add(Pair.of(100L, "<test></test"));//bad xml to trigger exception
127 storeService.saveMessages("test2", messagesXml);
130 List<Pair<Long, String>> messagesYaml = new ArrayList<>();
131 messagesYaml.add(Pair.of(100L, "test: yes"));
133 storeService.saveMessages("test3", messagesYaml);
136 List<Pair<Long, String>> messagesText = new ArrayList<>();
137 messagesText.add(Pair.of(100L, "test message"));
139 storeService.saveMessages("test4", messagesText);
142 storeService.saveMessages("test", null);
146 public void testFlush() {
147 storeService.flush();
148 storeService.flushStall();