2 * ============LICENSE_START=======================================================
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
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.db;
23 import java.util.ArrayList;
24 import java.util.List;
26 import org.jetbrains.annotations.NotNull;
27 import org.json.JSONObject;
28 import org.junit.After;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.junit.MockitoJUnitRunner;
33 import org.onap.datalake.feeder.config.ApplicationConfiguration;
34 import org.onap.datalake.feeder.domain.Db;
35 import org.onap.datalake.feeder.domain.EffectiveTopic;
36 import org.onap.datalake.feeder.domain.Topic;
37 import org.onap.datalake.feeder.util.TestUtil;
39 import com.couchbase.client.java.Cluster;
40 import com.couchbase.client.java.CouchbaseCluster;
41 import com.couchbase.client.java.env.DefaultCouchbaseEnvironment;
42 import com.couchbase.mock.Bucket;
43 import com.couchbase.mock.BucketConfiguration;
44 import com.couchbase.mock.CouchbaseMock;
45 import com.couchbase.mock.client.MockClient;
47 @RunWith(MockitoJUnitRunner.class)
48 public class CouchbaseServiceTest {
49 protected final BucketConfiguration bucketConfiguration = new BucketConfiguration();
50 protected MockClient mockClient;
51 protected CouchbaseMock couchbaseMock;
52 protected Cluster cluster;
53 protected com.couchbase.client.java.Bucket bucket;
54 protected int carrierPort;
55 protected int httpPort;
57 protected void getPortInfo(String bucket) throws Exception {
58 httpPort = couchbaseMock.getHttpPort();
59 carrierPort = couchbaseMock.getCarrierPort(bucket);
62 protected void createMock(@NotNull String name, @NotNull String password) throws Exception {
63 bucketConfiguration.numNodes = 1;
64 bucketConfiguration.numReplicas = 1;
65 bucketConfiguration.numVBuckets = 1024;
66 bucketConfiguration.name = name;
67 bucketConfiguration.type = Bucket.BucketType.COUCHBASE;
68 bucketConfiguration.password = password;
69 ArrayList<BucketConfiguration> configList = new ArrayList<BucketConfiguration>();
70 configList.add(bucketConfiguration);
71 couchbaseMock = new CouchbaseMock(0, configList);
72 couchbaseMock.start();
73 couchbaseMock.waitForStartup();
76 protected void createClient() {
77 cluster = CouchbaseCluster.create(DefaultCouchbaseEnvironment.builder().bootstrapCarrierDirectPort(carrierPort).bootstrapHttpDirectPort(httpPort).build(), "couchbase://127.0.0.1");
78 bucket = cluster.openBucket("default");
82 public void setUp() throws Exception {
83 createMock("default", "");
84 getPortInfo("default");
89 public void tearDown() {
90 if (cluster != null) {
93 if (couchbaseMock != null) {
96 if (mockClient != null) {
97 mockClient.shutdown();
102 public void testSaveJsonsWithTopicId() {
103 ApplicationConfiguration appConfig = new ApplicationConfiguration();
104 appConfig.setTimestampLabel("datalake_ts_");
106 String text = "{ data: { data2 : { value : 'hello'}}}";
108 JSONObject json = new JSONObject(text);
110 Topic topic = TestUtil.newTopic("test getMessageId");
111 topic.setMessageIdPath("/data/data2/value");
112 List<JSONObject> jsons = new ArrayList<>();
113 json.put(appConfig.getTimestampLabel(), 1234);
115 CouchbaseService couchbaseService = new CouchbaseService(new Db());
116 couchbaseService.bucket = bucket;
117 couchbaseService.config = appConfig;
119 couchbaseService.init();
120 EffectiveTopic effectiveTopic = new EffectiveTopic(topic, "test");
121 couchbaseService.saveJsons(effectiveTopic, jsons);
126 public void testSaveJsonsWithOutTopicId() {
127 ApplicationConfiguration appConfig = new ApplicationConfiguration();
128 appConfig.setTimestampLabel("datalake_ts_");
130 String text = "{ data: { data2 : { value : 'hello'}}}";
132 JSONObject json = new JSONObject(text);
134 Topic topic = TestUtil.newTopic("test getMessageId");
135 List<JSONObject> jsons = new ArrayList<>();
136 json.put(appConfig.getTimestampLabel(), 1234);
138 CouchbaseService couchbaseService = new CouchbaseService(new Db());
139 couchbaseService.bucket = bucket;
140 couchbaseService.config = appConfig;
142 couchbaseService.init();
143 EffectiveTopic effectiveTopic = new EffectiveTopic(topic, "test");
144 couchbaseService.saveJsons(effectiveTopic, jsons);
148 public void testCleanupBucket() {
149 // CouchbaseService couchbaseService = new CouchbaseService(new Db());
150 // couchbaseService.bucket = bucket;
151 // ApplicationConfiguration appConfig = new ApplicationConfiguration();
152 // couchbaseService.config = appConfig;
153 // couchbaseService.cleanUp();