44e76328571e59727aac4e63e1fe5842c30e5ea3
[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.mockito.Mockito.when;
24
25 import java.lang.reflect.InvocationTargetException;
26 import java.lang.reflect.Method;
27 import java.util.ArrayList;
28 import java.util.List;
29
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;
39
40 /**
41  * Test StoreService
42  * 
43  * @author Guobiao Mo
44  *
45  */
46 @RunWith(MockitoJUnitRunner.class)
47 public class StoreServiceTest {
48
49         @InjectMocks
50         private StoreService storeService = new StoreService();
51
52         @Mock
53         private ApplicationContext context;
54
55         @Mock
56         private ApplicationConfiguration config;
57
58         @Mock
59         private TopicConfigPollingService configPollingService;
60
61         @Mock
62         private MongodbService mongodbService;
63
64         @Mock
65         private CouchbaseService couchbaseService;
66
67         @Mock
68         private ElasticsearchService elasticsearchService;
69
70         @Mock
71         private HdfsService hdfsService;
72
73         public void testInit() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
74                 Method init = storeService.getClass().getDeclaredMethod("init");
75                 init.setAccessible(true);
76                 init.invoke(storeService);
77         }
78
79         private TopicConfig createTopicConfig(String topicStr, String type) {
80
81                 TopicConfig topicConfig = new TopicConfig();
82                 topicConfig.setName(topicStr);
83                 topicConfig.setDataFormat(type);
84                 topicConfig.setSaveRaw(true);
85
86                 when(configPollingService.getEffectiveTopicConfig(topicStr)).thenReturn(topicConfig);
87
88                 return topicConfig;
89         }
90
91         @Test
92         public void saveMessages() throws IllegalAccessException, NoSuchMethodException, InvocationTargetException, NoSuchFieldException {
93                 testInit();
94
95                 TopicConfig topicConfig = createTopicConfig("test1", "JSON");
96
97                 topicConfig = createTopicConfig("test2", "XML");
98                 topicConfig.setSaveRaw(false);
99
100                 topicConfig = createTopicConfig("test3", "YAML");
101
102                 topicConfig.setSinkdbs(new ArrayList<>());
103                 topicConfig.getSinkdbs().add("Elasticsearch");
104                 topicConfig.getSinkdbs().add("Couchbase");
105                 topicConfig.getSinkdbs().add("Druid");
106                 topicConfig.getSinkdbs().add("MongoDB");
107                 topicConfig.getSinkdbs().add("HDFS");
108
109                 createTopicConfig("test4", "TEXT");
110
111                 when(config.getTimestampLabel()).thenReturn("ts");
112                 when(config.getRawDataLabel()).thenReturn("raw");
113
114                 //JSON
115                 List<Pair<Long, String>> messages = new ArrayList<>();
116                 messages.add(Pair.of(100L, "{test: 1}"));
117
118                 storeService.saveMessages("test1", messages);
119
120                 //XML
121                 List<Pair<Long, String>> messagesXml = new ArrayList<>();
122                 messagesXml.add(Pair.of(100L, "<test></test>")); 
123                 messagesXml.add(Pair.of(100L, "<test></test"));//bad xml to trigger exception
124
125                 storeService.saveMessages("test2", messagesXml);
126
127                 //YAML
128                 List<Pair<Long, String>> messagesYaml = new ArrayList<>();
129                 messagesYaml.add(Pair.of(100L, "test: yes"));
130
131                 storeService.saveMessages("test3", messagesYaml);
132
133                 //TEXT
134                 List<Pair<Long, String>> messagesText = new ArrayList<>();
135                 messagesText.add(Pair.of(100L, "test message"));
136
137                 storeService.saveMessages("test4", messagesText);
138
139                 //Null mesg
140                 storeService.saveMessages("test", null);
141         }
142
143         @Test
144         public void testFlush() {
145                 storeService.flush();
146                 storeService.flushStall();
147         }
148 }