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