Renaming Files having BluePrint to have Blueprint
[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 io.micrometer.core.instrument.MeterRegistry
25 import org.junit.Assert
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.test.mock.mockito.MockBean
38 import org.springframework.test.annotation.DirtiesContext
39 import org.springframework.test.context.ContextConfiguration
40 import org.springframework.test.context.TestPropertySource
41 import org.springframework.test.context.junit4.SpringRunner
42 import kotlin.test.BeforeTest
43
44 @RunWith(SpringRunner::class)
45 @DirtiesContext
46 @ContextConfiguration(
47     classes = [
48         SelfServiceApiTestConfiguration::class, TestDatabaseConfiguration::class,
49         ErrorCatalogTestConfiguration::class
50     ]
51 )
52 @TestPropertySource(locations = ["classpath:application-test.properties"])
53 class BlueprintProcessingGRPCHandlerTest {
54
55     private val log = LoggerFactory.getLogger(BlueprintProcessingGRPCHandlerTest::class.java)
56
57     @MockBean
58     lateinit var meterRegistry: MeterRegistry
59
60     @get:Rule
61     val grpcServerRule = GrpcServerRule().directExecutor()
62
63     @Autowired
64     lateinit var bluePrintProcessingGRPCHandler: BlueprintProcessingGRPCHandler
65
66     lateinit var requestObs: StreamObserver<ExecutionServiceInput>
67
68     @BeforeTest
69     fun init() {
70         grpcServerRule.serviceRegistry.addService(bluePrintProcessingGRPCHandler)
71
72         val blockingStub = BlueprintProcessingServiceGrpc.newStub(grpcServerRule.channel)
73
74         requestObs = blockingStub.process(object : StreamObserver<ExecutionServiceOutput> {
75             override fun onNext(executionServiceOuput: ExecutionServiceOutput) {
76                 log.debug("onNext {}", executionServiceOuput)
77                 if ("1234" == executionServiceOuput.commonHeader.requestId) {
78                     Assert.assertEquals(
79                         "Failed to process request, \'actionIdentifiers.mode\' not specified. Valid value are: \'sync\' or \'async\'.",
80                         executionServiceOuput.status.errorMessage
81                     )
82                 }
83             }
84
85             override fun onError(error: Throwable) {
86                 log.debug("Fail to process message", error)
87                 Assert.assertEquals("INTERNAL: Could not find blueprint : from database", error.message)
88             }
89
90             override fun onCompleted() {
91                 log.info("Done")
92             }
93         })
94     }
95
96     @Test
97     fun testSelfServiceGRPCHandler() {
98         val commonHeader = CommonHeader.newBuilder()
99             .setTimestamp("2012-04-23T18:25:43.511Z")
100             .setOriginatorId("System")
101             .setRequestId("1234")
102             .setSubRequestId("1234-56").build()
103
104         val jsonContent = JacksonUtils.getClassPathFileContent("execution-input/sample-payload.json")
105         val payloadBuilder = ExecutionServiceInput.newBuilder().payloadBuilder
106         JsonFormat.parser().merge(jsonContent, payloadBuilder)
107
108         val input = ExecutionServiceInput.newBuilder()
109             .setCommonHeader(commonHeader)
110             .setPayload(payloadBuilder.build())
111             .build()
112
113         requestObs.onNext(input)
114
115         val commonHeader2 = CommonHeader.newBuilder()
116             .setTimestamp("2012-04-23T18:25:43.511Z")
117             .setOriginatorId("System")
118             .setRequestId("2345")
119             .setSubRequestId("1234-56").build()
120
121         val actionIdentifier = ActionIdentifiers.newBuilder().setMode("sync").build()
122
123         val input2 = ExecutionServiceInput.newBuilder()
124             .setCommonHeader(commonHeader2)
125             .setActionIdentifiers(actionIdentifier)
126             .setPayload(payloadBuilder.build())
127             .build()
128
129         requestObs.onNext(input2)
130
131         requestObs.onCompleted()
132     }
133 }