Formatting Code base with ktlint
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / selfservice-api / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / selfservice / api / BluePrintProcessingGRPCHandlerTest.kt
1 /*
2  * Copyright © 2017-2018 AT&T Intellectual Property.
3  * Modifications Copyright © 2019 Bell Canada.
4  * Modifications Copyright © 2019 IBM.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *     http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  */
18
19 package org.onap.ccsdk.cds.blueprintsprocessor.selfservice.api
20
21 import com.google.protobuf.util.JsonFormat
22 import io.grpc.stub.StreamObserver
23 import io.grpc.testing.GrpcServerRule
24 import org.junit.Assert
25 import org.junit.Rule
26 import org.junit.Test
27 import org.junit.runner.RunWith
28 import org.onap.ccsdk.cds.controllerblueprints.common.api.ActionIdentifiers
29 import org.onap.ccsdk.cds.controllerblueprints.common.api.CommonHeader
30 import org.onap.ccsdk.cds.controllerblueprints.core.utils.JacksonUtils
31 import org.onap.ccsdk.cds.controllerblueprints.processing.api.BluePrintProcessingServiceGrpc
32 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceInput
33 import org.onap.ccsdk.cds.controllerblueprints.processing.api.ExecutionServiceOutput
34 import org.slf4j.LoggerFactory
35 import org.springframework.beans.factory.annotation.Autowired
36 import org.springframework.boot.autoconfigure.EnableAutoConfiguration
37 import org.springframework.context.annotation.ComponentScan
38 import org.springframework.test.annotation.DirtiesContext
39 import org.springframework.test.context.TestPropertySource
40 import org.springframework.test.context.junit4.SpringRunner
41 import kotlin.test.BeforeTest
42
43 @RunWith(SpringRunner::class)
44 @DirtiesContext
45 @EnableAutoConfiguration
46 @ComponentScan(
47     basePackages = ["org.onap.ccsdk.cds.blueprintsprocessor",
48         "org.onap.ccsdk.cds.controllerblueprints"]
49 )
50 @TestPropertySource(locations = ["classpath:application-test.properties"])
51 class BluePrintProcessingGRPCHandlerTest {
52
53     private val log = LoggerFactory.getLogger(BluePrintProcessingGRPCHandlerTest::class.java)
54
55     @get:Rule
56     val grpcServerRule = GrpcServerRule().directExecutor()
57
58     @Autowired
59     lateinit var bluePrintProcessingGRPCHandler: BluePrintProcessingGRPCHandler
60
61     lateinit var requestObs: StreamObserver<ExecutionServiceInput>
62
63     @BeforeTest
64     fun init() {
65         grpcServerRule.serviceRegistry.addService(bluePrintProcessingGRPCHandler)
66
67         val blockingStub = BluePrintProcessingServiceGrpc.newStub(grpcServerRule.channel)
68
69         requestObs = blockingStub.process(object : StreamObserver<ExecutionServiceOutput> {
70             override fun onNext(executionServiceOuput: ExecutionServiceOutput) {
71                 log.debug("onNext {}", executionServiceOuput)
72                 if ("1234".equals(executionServiceOuput.commonHeader.requestId)) {
73                     Assert.assertEquals(
74                         "Failed to process request, \'actionIdentifiers.mode\' not specified. Valid value are: \'sync\' or \'async\'.",
75                         executionServiceOuput.status.errorMessage
76                     )
77                 }
78             }
79
80             override fun onError(error: Throwable) {
81                 log.debug("Fail to process message", error)
82                 Assert.assertEquals("INTERNAL: Could not find blueprint : from database", error.message)
83             }
84
85             override fun onCompleted() {
86                 log.info("Done")
87             }
88         })
89     }
90
91     @Test
92     fun testSelfServiceGRPCHandler() {
93         val commonHeader = CommonHeader.newBuilder()
94             .setTimestamp("2012-04-23T18:25:43.511Z")
95             .setOriginatorId("System")
96             .setRequestId("1234")
97             .setSubRequestId("1234-56").build()
98
99         val jsonContent = JacksonUtils.getClassPathFileContent("execution-input/sample-payload.json")
100         val payloadBuilder = ExecutionServiceInput.newBuilder().payloadBuilder
101         JsonFormat.parser().merge(jsonContent, payloadBuilder)
102
103         val input = ExecutionServiceInput.newBuilder()
104             .setCommonHeader(commonHeader)
105             .setPayload(payloadBuilder.build())
106             .build()
107
108         requestObs.onNext(input)
109
110         val commonHeader2 = CommonHeader.newBuilder()
111             .setTimestamp("2012-04-23T18:25:43.511Z")
112             .setOriginatorId("System")
113             .setRequestId("2345")
114             .setSubRequestId("1234-56").build()
115
116         val actionIdentifier = ActionIdentifiers.newBuilder().setMode("sync").build()
117
118         val input2 = ExecutionServiceInput.newBuilder()
119             .setCommonHeader(commonHeader2)
120             .setActionIdentifiers(actionIdentifier)
121             .setPayload(payloadBuilder.build())
122             .build()
123
124         requestObs.onNext(input2)
125
126         requestObs.onCompleted()
127     }
128 }