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;
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;
40 import java.util.ArrayList;
41 import java.util.List;
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;
53 protected void getPortInfo(String bucket) throws Exception {
54 httpPort = couchbaseMock.getHttpPort();
55 carrierPort = couchbaseMock.getCarrierPort(bucket);
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();
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");
81 public void setUp() throws Exception {
82 createMock("default", "");
83 getPortInfo("default");
88 public void tearDown() {
89 if (cluster != null) {
92 if (couchbaseMock != null) {
95 if (mockClient != null) {
96 mockClient.shutdown();
101 public void testSaveJsonsWithTopicId() {
102 ApplicationConfiguration appConfig = new ApplicationConfiguration();
103 appConfig.setTimestampLabel("datalake_ts_");
105 String text = "{ data: { data2 : { value : 'hello'}}}";
107 JSONObject json = new JSONObject(text);
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);
114 CouchbaseService couchbaseService = new CouchbaseService();
115 couchbaseService.bucket = bucket;
116 couchbaseService.config = appConfig;
117 couchbaseService.saveJsons(topic.getTopicConfig(), jsons);
122 public void testSaveJsonsWithOutTopicId() {
123 ApplicationConfiguration appConfig = new ApplicationConfiguration();
124 appConfig.setTimestampLabel("datalake_ts_");
126 String text = "{ data: { data2 : { value : 'hello'}}}";
128 JSONObject json = new JSONObject(text);
130 Topic topic = new Topic("test getMessageId");
131 List<JSONObject> jsons = new ArrayList<>();
132 json.put(appConfig.getTimestampLabel(), 1234);
134 CouchbaseService couchbaseService = new CouchbaseService();
135 couchbaseService.bucket = bucket;
136 couchbaseService.config = appConfig;
137 couchbaseService.saveJsons(topic.getTopicConfig(), jsons);
141 public void testCleanupBucket() {
142 CouchbaseService couchbaseService = new CouchbaseService();
143 couchbaseService.bucket = bucket;
144 couchbaseService.cleanUp();