2a7745b47056eac0735a4cbcccaf1dfeb4a0a453
[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.db;
22
23 import java.util.ArrayList;
24 import java.util.List;
25
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;
38
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;
46
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;
56
57         protected void getPortInfo(String bucket) throws Exception {
58                 httpPort = couchbaseMock.getHttpPort();
59                 carrierPort = couchbaseMock.getCarrierPort(bucket);
60         }
61
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();
74         }
75
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");
79         }
80
81         @Before
82         public void setUp() throws Exception {
83                 createMock("default", "");
84                 getPortInfo("default");
85                 createClient();
86         }
87
88         @After
89         public void tearDown() {
90                 if (cluster != null) {
91                         cluster.disconnect();
92                 }
93                 if (couchbaseMock != null) {
94                         couchbaseMock.stop();
95                 }
96                 if (mockClient != null) {
97                         mockClient.shutdown();
98                 }
99         }
100
101         @Test
102         public void testSaveJsonsWithTopicId() {
103                 ApplicationConfiguration appConfig = new ApplicationConfiguration();
104                 appConfig.setTimestampLabel("datalake_ts_");
105
106                 String text = "{ data: { data2 : { value : 'hello'}}}";
107
108                 JSONObject json = new JSONObject(text);
109
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);
114                 jsons.add(json);
115                 CouchbaseService couchbaseService = new CouchbaseService(new Db());
116                 couchbaseService.bucket = bucket;
117                 couchbaseService.config = appConfig;
118
119                 couchbaseService.init();
120                 EffectiveTopic effectiveTopic = new EffectiveTopic(topic, "test");
121                 couchbaseService.saveJsons(effectiveTopic, jsons);
122
123         }
124
125         @Test
126         public void testSaveJsonsWithOutTopicId() {
127                 ApplicationConfiguration appConfig = new ApplicationConfiguration();
128                 appConfig.setTimestampLabel("datalake_ts_");
129
130                 String text = "{ data: { data2 : { value : 'hello'}}}";
131
132                 JSONObject json = new JSONObject(text);
133
134                 Topic topic = TestUtil.newTopic("test getMessageId");
135                 List<JSONObject> jsons = new ArrayList<>();
136                 json.put(appConfig.getTimestampLabel(), 1234);
137                 jsons.add(json);
138                 CouchbaseService couchbaseService = new CouchbaseService(new Db());
139                 couchbaseService.bucket = bucket;
140                 couchbaseService.config = appConfig;
141
142                 couchbaseService.init();
143                 EffectiveTopic effectiveTopic = new EffectiveTopic(topic, "test");
144                 couchbaseService.saveJsons(effectiveTopic, jsons);
145         }
146
147         @Test
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();
154         }
155
156 }