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.domain.Topic;
39 import java.util.ArrayList;
40 import java.util.List;
42 @RunWith(MockitoJUnitRunner.class)
43 public class CouchbaseServiceTest {
44 protected final BucketConfiguration bucketConfiguration = new BucketConfiguration();
45 protected MockClient mockClient;
46 protected CouchbaseMock couchbaseMock;
47 protected Cluster cluster;
48 protected com.couchbase.client.java.Bucket bucket;
49 protected int carrierPort;
50 protected int httpPort;
52 protected void getPortInfo(String bucket) throws Exception {
53 httpPort = couchbaseMock.getHttpPort();
54 carrierPort = couchbaseMock.getCarrierPort(bucket);
57 protected void createMock(@NotNull String name, @NotNull String password) throws Exception {
58 bucketConfiguration.numNodes = 1;
59 bucketConfiguration.numReplicas = 1;
60 bucketConfiguration.numVBuckets = 1024;
61 bucketConfiguration.name = name;
62 bucketConfiguration.type = Bucket.BucketType.COUCHBASE;
63 bucketConfiguration.password = password;
64 ArrayList<BucketConfiguration> configList = new ArrayList<BucketConfiguration>();
65 configList.add(bucketConfiguration);
66 couchbaseMock = new CouchbaseMock(0, configList);
67 couchbaseMock.start();
68 couchbaseMock.waitForStartup();
71 protected void createClient() {
72 cluster = CouchbaseCluster.create(DefaultCouchbaseEnvironment.builder()
73 .bootstrapCarrierDirectPort(carrierPort)
74 .bootstrapHttpDirectPort(httpPort)
75 .build(), "couchbase://127.0.0.1");
76 bucket = cluster.openBucket("default");
80 public void setUp() throws Exception {
81 createMock("default", "");
82 getPortInfo("default");
87 public void tearDown() {
88 if (cluster != null) {
91 if (couchbaseMock != null) {
94 if (mockClient != null) {
95 mockClient.shutdown();
100 public void testSaveJsonsWithTopicId() {
102 String text = "{ data: { data2 : { value : 'hello'}}}";
104 JSONObject json = new JSONObject(text);
106 Topic topic = new Topic("test getMessageId");
107 topic.setMessageIdPath("/data/data2/value");
108 List<JSONObject> jsons = new ArrayList<>();
109 json.put("_ts", 1234);
111 CouchbaseService couchbaseService = new CouchbaseService();
112 couchbaseService.bucket = bucket;
113 couchbaseService.saveJsons(topic, jsons);
118 public void testSaveJsonsWithOutTopicId() {
120 String text = "{ data: { data2 : { value : 'hello'}}}";
122 JSONObject json = new JSONObject(text);
124 Topic topic = new Topic("test getMessageId");
125 List<JSONObject> jsons = new ArrayList<>();
126 json.put("_ts", 1234);
128 CouchbaseService couchbaseService = new CouchbaseService();
129 couchbaseService.bucket = bucket;
130 couchbaseService.saveJsons(topic, jsons);
134 public void testCleanupBucket() {
135 CouchbaseService couchbaseService = new CouchbaseService();
136 couchbaseService.bucket = bucket;
137 couchbaseService.cleanUp();