Use TopicConfig in backend
[dcaegen2/services.git] / components / datalake-handler / feeder / src / test / java / org / onap / datalake / feeder / service / CouchbaseServiceTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP : DATALAKE
4  * ================================================================================
5  * Copyright (C) 2018-2019 Huawei. All rights reserved.
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 com.couchbase.client.java.Cluster;
24 import com.couchbase.client.java.CouchbaseCluster;
25 import com.couchbase.client.java.env.DefaultCouchbaseEnvironment;
26 import com.couchbase.mock.Bucket;
27 import com.couchbase.mock.BucketConfiguration;
28 import com.couchbase.mock.CouchbaseMock;
29 import com.couchbase.mock.client.MockClient;
30 import org.jetbrains.annotations.NotNull;
31 import org.json.JSONObject;
32 import org.junit.After;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.junit.MockitoJUnitRunner;
37 import org.onap.datalake.feeder.config.ApplicationConfiguration;
38 import org.onap.datalake.feeder.domain.Topic;
39
40 import java.util.ArrayList;
41 import java.util.List;
42
43 @RunWith(MockitoJUnitRunner.class)
44 public class CouchbaseServiceTest {
45     protected final BucketConfiguration bucketConfiguration = new BucketConfiguration();
46     protected MockClient mockClient;
47     protected CouchbaseMock couchbaseMock;
48     protected Cluster cluster;
49     protected com.couchbase.client.java.Bucket bucket;
50     protected int carrierPort;
51     protected int httpPort;
52
53     protected void getPortInfo(String bucket) throws Exception {
54         httpPort = couchbaseMock.getHttpPort();
55         carrierPort = couchbaseMock.getCarrierPort(bucket);
56     }
57
58     protected void createMock(@NotNull String name, @NotNull String password) throws Exception {
59         bucketConfiguration.numNodes = 1;
60         bucketConfiguration.numReplicas = 1;
61         bucketConfiguration.numVBuckets = 1024;
62         bucketConfiguration.name = name;
63         bucketConfiguration.type = Bucket.BucketType.COUCHBASE;
64         bucketConfiguration.password = password;
65         ArrayList<BucketConfiguration> configList = new ArrayList<BucketConfiguration>();
66         configList.add(bucketConfiguration);
67         couchbaseMock = new CouchbaseMock(0, configList);
68         couchbaseMock.start();
69         couchbaseMock.waitForStartup();
70     }
71
72     protected void createClient() {
73         cluster = CouchbaseCluster.create(DefaultCouchbaseEnvironment.builder()
74                                                   .bootstrapCarrierDirectPort(carrierPort)
75                                                   .bootstrapHttpDirectPort(httpPort)
76                                                   .build(), "couchbase://127.0.0.1");
77         bucket = cluster.openBucket("default");
78     }
79
80     @Before
81     public void setUp() throws Exception {
82         createMock("default", "");
83         getPortInfo("default");
84         createClient();
85     }
86
87     @After
88     public void tearDown() {
89         if (cluster != null) {
90             cluster.disconnect();
91         }
92         if (couchbaseMock != null) {
93             couchbaseMock.stop();
94         }
95         if (mockClient != null) {
96             mockClient.shutdown();
97         }
98     }
99
100     @Test
101     public void testSaveJsonsWithTopicId() {
102         ApplicationConfiguration appConfig = new ApplicationConfiguration();
103         appConfig.setTimestampLabel("datalake_ts_");
104
105         String text = "{ data: { data2 : { value : 'hello'}}}";
106
107         JSONObject json = new JSONObject(text);
108
109         Topic topic = new Topic("test getMessageId");
110         topic.setMessageIdPath("/data/data2/value");
111         List<JSONObject> jsons = new ArrayList<>();
112         json.put(appConfig.getTimestampLabel(), 1234);
113         jsons.add(json);
114         CouchbaseService couchbaseService = new CouchbaseService();
115         couchbaseService.bucket = bucket;
116         couchbaseService.config = appConfig;
117         couchbaseService.saveJsons(topic.getTopicConfig(), jsons);
118
119     }
120
121     @Test
122     public void testSaveJsonsWithOutTopicId() {
123         ApplicationConfiguration appConfig = new ApplicationConfiguration();
124         appConfig.setTimestampLabel("datalake_ts_");
125
126         String text = "{ data: { data2 : { value : 'hello'}}}";
127
128         JSONObject json = new JSONObject(text);
129
130         Topic topic = new Topic("test getMessageId");
131         List<JSONObject> jsons = new ArrayList<>();
132         json.put(appConfig.getTimestampLabel(), 1234);
133         jsons.add(json);
134         CouchbaseService couchbaseService = new CouchbaseService();
135         couchbaseService.bucket = bucket;
136         couchbaseService.config = appConfig;
137         couchbaseService.saveJsons(topic.getTopicConfig(), jsons);
138     }
139
140     @Test
141     public void testCleanupBucket() {
142         CouchbaseService couchbaseService = new CouchbaseService();
143         couchbaseService.bucket = bucket;
144         couchbaseService.cleanUp();
145     }
146
147 }