9e40a2b144efb1fc8d2994670da8848389e6eef4
[dcaegen2/services.git] /
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.domain.Topic;
38
39 import java.util.ArrayList;
40 import java.util.List;
41
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;
51
52     protected void getPortInfo(String bucket) throws Exception {
53         httpPort = couchbaseMock.getHttpPort();
54         carrierPort = couchbaseMock.getCarrierPort(bucket);
55     }
56
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();
69     }
70
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");
77     }
78
79     @Before
80     public void setUp() throws Exception {
81         createMock("default", "");
82         getPortInfo("default");
83         createClient();
84     }
85
86     @After
87     public void tearDown() {
88         if (cluster != null) {
89             cluster.disconnect();
90         }
91         if (couchbaseMock != null) {
92             couchbaseMock.stop();
93         }
94         if (mockClient != null) {
95             mockClient.shutdown();
96         }
97     }
98
99     @Test
100     public void testSaveJsonsWithTopicId() {
101
102         String text = "{ data: { data2 : { value : 'hello'}}}";
103
104         JSONObject json = new JSONObject(text);
105
106         Topic topic = new Topic("test getMessageId");
107         topic.setMessageIdPath("/data/data2/value");
108         List<JSONObject> jsons = new ArrayList<>();
109         json.put("_ts", 1234);
110         jsons.add(json);
111         CouchbaseService couchbaseService = new CouchbaseService();
112         couchbaseService.bucket = bucket;
113         couchbaseService.saveJsons(topic, jsons);
114
115     }
116
117     @Test
118     public void testSaveJsonsWithOutTopicId() {
119
120         String text = "{ data: { data2 : { value : 'hello'}}}";
121
122         JSONObject json = new JSONObject(text);
123
124         Topic topic = new Topic("test getMessageId");
125         List<JSONObject> jsons = new ArrayList<>();
126         json.put("_ts", 1234);
127         jsons.add(json);
128         CouchbaseService couchbaseService = new CouchbaseService();
129         couchbaseService.bucket = bucket;
130         couchbaseService.saveJsons(topic, jsons);
131     }
132
133     @Test
134     public void testCleanupBucket() {
135         CouchbaseService couchbaseService = new CouchbaseService();
136         couchbaseService.bucket = bucket;
137         couchbaseService.cleanUp();
138     }
139
140 }