Merge "adding new designer component, sort and configuration dashboard"
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / commons / atomix-lib / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / atomix / AtomixBluePrintClusterServiceTest.kt
1 /*
2  * Copyright © 2018-2019 AT&T Intellectual Property.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package org.onap.ccsdk.cds.blueprintsprocessor.atomix
18
19 import com.fasterxml.jackson.databind.JsonNode
20 import kotlinx.coroutines.Dispatchers
21 import kotlinx.coroutines.async
22 import kotlinx.coroutines.awaitAll
23 import kotlinx.coroutines.delay
24 import kotlinx.coroutines.runBlocking
25 import org.junit.Before
26 import org.junit.Test
27 import org.onap.ccsdk.cds.blueprintsprocessor.atomix.service.AtomixBluePrintClusterService
28 import org.onap.ccsdk.cds.blueprintsprocessor.atomix.utils.AtomixLibUtils
29 import org.onap.ccsdk.cds.blueprintsprocessor.core.service.ClusterInfo
30 import org.onap.ccsdk.cds.controllerblueprints.core.asJsonPrimitive
31 import org.onap.ccsdk.cds.controllerblueprints.core.deleteNBDir
32 import org.onap.ccsdk.cds.controllerblueprints.core.logger
33 import kotlin.test.assertNotNull
34
35 class AtomixBluePrintClusterServiceTest {
36     val log = logger(AtomixBluePrintClusterServiceTest::class)
37
38     @Before
39     fun init() {
40         runBlocking {
41             deleteNBDir("target/cluster")
42         }
43     }
44
45     /** Testing two cluster with distributed map store creation, This is time consuming test casetake around 10s **/
46     @Test
47     fun testClusterJoin() {
48         runBlocking {
49             val members = arrayListOf("node-5679", "node-5680")
50             val deferred = arrayListOf(5679, 5680).map { port ->
51                 async(Dispatchers.IO) {
52                     val nodeId = "node-$port"
53                     log.info("********** Starting node($nodeId) on port($port)")
54                     val clusterInfo = ClusterInfo(
55                         id = "test-cluster", nodeId = nodeId,
56                         clusterMembers = members, nodeAddress = "localhost:$port", storagePath = "target/cluster"
57                     )
58                     val atomixClusterService = AtomixBluePrintClusterService()
59                     atomixClusterService.startCluster(clusterInfo)
60                     atomixClusterService.atomix
61                 }
62             }
63             val atomix = deferred.awaitAll()
64             /** Test Distributed store creation */
65             repeat(2) { storeId ->
66                 val store = AtomixLibUtils.distributedMapStore<JsonNode>(atomix.get(0), "blueprint-runtime-$storeId")
67                 assertNotNull(store, "failed to get store")
68                 val store1 = AtomixLibUtils.distributedMapStore<JsonNode>(atomix.get(1), "blueprint-runtime-$storeId")
69                 store1.addListener {
70                     log.info("Received map event : $it")
71                 }
72                 repeat(10) {
73                     store["key-$storeId-$it"] = "value-$it".asJsonPrimitive()
74                 }
75                 delay(100)
76                 store.close()
77             }
78         }
79     }
80 }