fc05d1d4ef11a02ec3ae8f7068140e2703431f06
[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                 topicConfig.setAggregateArrayPath("/test");
97                 topicConfig.setFlattenArrayPath("/test");
98
99                 topicConfig = createTopicConfig("test2", "XML");
100                 topicConfig.setSaveRaw(false);
101
102                 topicConfig = createTopicConfig("test3", "YAML");
103
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");
110
111                 createTopicConfig("test4", "TEXT");
112
113                 when(config.getTimestampLabel()).thenReturn("ts");
114                 when(config.getRawDataLabel()).thenReturn("raw");
115
116                 //JSON
117                 List<Pair<Long, String>> messages = new ArrayList<>();
118                 messages.add(Pair.of(100L, "{test: 1}"));
119
120                 storeService.saveMessages("test1", messages);
121
122                 //XML
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
126
127                 storeService.saveMessages("test2", messagesXml);
128
129                 //YAML
130                 List<Pair<Long, String>> messagesYaml = new ArrayList<>();
131                 messagesYaml.add(Pair.of(100L, "test: yes"));
132
133                 storeService.saveMessages("test3", messagesYaml);
134
135                 //TEXT
136                 List<Pair<Long, String>> messagesText = new ArrayList<>();
137                 messagesText.add(Pair.of(100L, "test message"));
138
139                 storeService.saveMessages("test4", messagesText);
140
141                 //Null mesg
142                 storeService.saveMessages("test", null);
143         }
144
145         @Test
146         public void testFlush() {
147                 storeService.flush();
148                 storeService.flushStall();
149         }
150 }