94eeb085a2450e42dff58e758aa930ab20c3ea2e
[dcaegen2/services.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP : DATALAKE
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
10  *
11  *     http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.datalake.feeder.service;
22
23 import static org.junit.Assert.assertTrue;
24 import static org.mockito.Mockito.when;
25
26 import java.lang.reflect.InvocationTargetException;
27 import java.lang.reflect.Method;
28 import java.util.ArrayList;
29 import java.util.List;
30
31 import org.apache.commons.lang3.tuple.Pair;
32 import org.junit.Test;
33 import org.junit.runner.RunWith;
34 import org.mockito.InjectMocks;
35 import org.mockito.Mock;
36 import org.mockito.junit.MockitoJUnitRunner;
37 import org.onap.datalake.feeder.config.ApplicationConfiguration;
38 import org.onap.datalake.feeder.dto.TopicConfig;
39 import org.springframework.context.ApplicationContext;
40
41 /**
42  * Test StoreService
43  * 
44  * @author Guobiao Mo
45  *
46  */
47 @RunWith(MockitoJUnitRunner.class)
48 public class StoreServiceTest {
49
50         @InjectMocks
51         private StoreService storeService = new StoreService();
52
53         @Mock
54         private ApplicationContext context;
55
56         @Mock
57         private ApplicationConfiguration config;
58
59         @Mock
60         private TopicConfigPollingService configPollingService;
61
62         @Mock
63         private MongodbService mongodbService;
64
65         @Mock
66         private CouchbaseService couchbaseService;
67
68         @Mock
69         private ElasticsearchService elasticsearchService;
70
71         @Mock
72         private HdfsService hdfsService;
73
74         public void testInit() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
75                 Method init = storeService.getClass().getDeclaredMethod("init");
76                 init.setAccessible(true);
77                 init.invoke(storeService);
78         }
79
80         private TopicConfig createTopicConfig(String topicStr, String type) {
81
82                 TopicConfig topicConfig = new TopicConfig();
83                 topicConfig.setName(topicStr);
84                 topicConfig.setDataFormat(type);
85                 topicConfig.setSaveRaw(true);
86
87                 when(configPollingService.getEffectiveTopicConfig(topicStr)).thenReturn(topicConfig);
88
89                 return topicConfig;
90         }
91
92         @Test
93         public void saveMessages() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
94                 testInit();
95
96                 TopicConfig topicConfig = createTopicConfig("test1", "JSON");
97                 topicConfig.setAggregateArrayPath("/test");
98                 topicConfig.setFlattenArrayPath("/test");
99
100                 topicConfig = createTopicConfig("test2", "XML");
101                 topicConfig.setSaveRaw(false);
102
103                 topicConfig = createTopicConfig("test3", "YAML");
104
105                 topicConfig.setSinkdbs(new ArrayList<>());
106                 topicConfig.getSinkdbs().add("Elasticsearch");
107                 topicConfig.getSinkdbs().add("Couchbase");
108                 topicConfig.getSinkdbs().add("Druid");
109                 topicConfig.getSinkdbs().add("MongoDB");
110                 topicConfig.getSinkdbs().add("HDFS");
111
112
113                 topicConfig.setEnabledSinkdbs(new ArrayList<>());
114                 topicConfig.getEnabledSinkdbs().add("Elasticsearch");
115                 assertTrue(topicConfig.supportElasticsearch());
116                 
117                 
118                 createTopicConfig("test4", "TEXT");
119
120                 when(config.getTimestampLabel()).thenReturn("ts");
121                 when(config.getRawDataLabel()).thenReturn("raw");
122
123                 //JSON
124                 List<Pair<Long, String>> messages = new ArrayList<>();
125                 messages.add(Pair.of(100L, "{test: 1}"));
126
127                 storeService.saveMessages("test1", messages);
128
129                 //XML
130                 List<Pair<Long, String>> messagesXml = new ArrayList<>();
131                 messagesXml.add(Pair.of(100L, "<test></test>")); 
132                 messagesXml.add(Pair.of(100L, "<test></test"));//bad xml to trigger exception
133
134                 storeService.saveMessages("test2", messagesXml);
135
136                 //YAML
137                 List<Pair<Long, String>> messagesYaml = new ArrayList<>();
138                 messagesYaml.add(Pair.of(100L, "test: yes"));
139
140                 storeService.saveMessages("test3", messagesYaml);
141
142                 //TEXT
143                 List<Pair<Long, String>> messagesText = new ArrayList<>();
144                 messagesText.add(Pair.of(100L, "test message"));
145
146                 storeService.saveMessages("test4", messagesText);
147
148                 //Null mesg
149                 storeService.saveMessages("test", null);
150         }
151
152         @Test
153         public void testFlush() {
154                 storeService.flush();
155                 storeService.flushStall();
156         }
157 }