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 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.Db;
39 import org.onap.datalake.feeder.domain.Kafka;
40 import org.onap.datalake.feeder.domain.Topic;
41 import org.onap.datalake.feeder.service.db.CouchbaseService;
42 import org.onap.datalake.feeder.util.TestUtil;
44 import static org.mockito.Mockito.when;
46 import java.util.ArrayList;
47 import java.util.List;
48 import java.util.concurrent.locks.ReentrantReadWriteLock;
50 @RunWith(MockitoJUnitRunner.class)
51 public class CouchbaseServiceTest {
52 protected final BucketConfiguration bucketConfiguration = new BucketConfiguration();
53 protected MockClient mockClient;
54 protected CouchbaseMock couchbaseMock;
55 protected Cluster cluster;
56 protected com.couchbase.client.java.Bucket bucket;
57 protected int carrierPort;
58 protected int httpPort;
60 protected void getPortInfo(String bucket) throws Exception {
61 httpPort = couchbaseMock.getHttpPort();
62 carrierPort = couchbaseMock.getCarrierPort(bucket);
65 protected void createMock(@NotNull String name, @NotNull String password) throws Exception {
66 bucketConfiguration.numNodes = 1;
67 bucketConfiguration.numReplicas = 1;
68 bucketConfiguration.numVBuckets = 1024;
69 bucketConfiguration.name = name;
70 bucketConfiguration.type = Bucket.BucketType.COUCHBASE;
71 bucketConfiguration.password = password;
72 ArrayList<BucketConfiguration> configList = new ArrayList<BucketConfiguration>();
73 configList.add(bucketConfiguration);
74 couchbaseMock = new CouchbaseMock(0, configList);
75 couchbaseMock.start();
76 couchbaseMock.waitForStartup();
79 protected void createClient() {
80 cluster = CouchbaseCluster.create(DefaultCouchbaseEnvironment.builder()
81 .bootstrapCarrierDirectPort(carrierPort)
82 .bootstrapHttpDirectPort(httpPort)
83 .build(), "couchbase://127.0.0.1");
84 bucket = cluster.openBucket("default");
88 public void setUp() throws Exception {
89 createMock("default", "");
90 getPortInfo("default");
95 public void tearDown() {
96 if (cluster != null) {
99 if (couchbaseMock != null) {
100 couchbaseMock.stop();
102 if (mockClient != null) {
103 mockClient.shutdown();
108 public void testSaveJsonsWithTopicId() {
109 ApplicationConfiguration appConfig = new ApplicationConfiguration();
110 appConfig.setTimestampLabel("datalake_ts_");
112 String text = "{ data: { data2 : { value : 'hello'}}}";
114 JSONObject json = new JSONObject(text);
116 Topic topic = TestUtil.newTopic("test getMessageId");
117 topic.setMessageIdPath("/data/data2/value");
118 List<JSONObject> jsons = new ArrayList<>();
119 json.put(appConfig.getTimestampLabel(), 1234);
121 CouchbaseService couchbaseService = new CouchbaseService(new Db());
122 couchbaseService.bucket = bucket;
123 couchbaseService.config = appConfig;
124 // couchbaseService.saveJsons(topic.getTopicConfig(), jsons);
129 public void testSaveJsonsWithOutTopicId() {
130 ApplicationConfiguration appConfig = new ApplicationConfiguration();
131 appConfig.setTimestampLabel("datalake_ts_");
133 String text = "{ data: { data2 : { value : 'hello'}}}";
135 JSONObject json = new JSONObject(text);
137 Topic topic = TestUtil.newTopic("test getMessageId");
138 List<JSONObject> jsons = new ArrayList<>();
139 json.put(appConfig.getTimestampLabel(), 1234);
141 CouchbaseService couchbaseService = new CouchbaseService(new Db());
142 couchbaseService.bucket = bucket;
143 couchbaseService.config = appConfig;
144 // couchbaseService.saveJsons(topic.getTopicConfig(), 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();