2 * Copyright © 2019-2020 Orange.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.onap.ccsdk.cds.blueprintsprocessor.healthapi
19 import org.junit.Assert.assertEquals
20 import org.junit.Assert.assertNotNull
21 import org.mockito.ArgumentMatchers.any
22 import org.mockito.ArgumentMatchers.eq
23 import org.mockito.Mockito.anyString
24 import org.mockito.Mockito.mock
26 import java.util.Arrays
27 import org.junit.Assert
28 import org.junit.Before
30 import org.junit.runner.RunWith
31 import org.mockito.ArgumentMatchers
32 import org.mockito.InjectMocks
33 import org.mockito.Mock
34 import org.mockito.Mockito
35 import org.mockito.junit.MockitoJUnitRunner
36 import org.onap.ccsdk.cds.blueprintsprocessor.healthapi.configuration.HealthCheckProperties
37 import org.onap.ccsdk.cds.blueprintsprocessor.healthapi.domain.HealthApiResponse
38 import org.onap.ccsdk.cds.blueprintsprocessor.healthapi.domain.HealthCheckStatus
39 import org.onap.ccsdk.cds.blueprintsprocessor.healthapi.domain.ServiceEndpoint
40 import org.onap.ccsdk.cds.blueprintsprocessor.healthapi.service.EndPointExecution
41 import org.onap.ccsdk.cds.blueprintsprocessor.healthapi.service.health.BluePrintProcessorHealthCheck
42 import org.onap.ccsdk.cds.blueprintsprocessor.rest.BasicAuthRestClientProperties
43 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BasicAuthRestClientService
44 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
45 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService.WebClientResponse
46 import org.springframework.http.HttpMethod
48 @RunWith(MockitoJUnitRunner::class)
49 class HealthCheckServiceTest {
52 private val basicAuthRestClientService: BasicAuthRestClientService? = null
55 private val restClientProperties: BasicAuthRestClientProperties? = null
58 private val healthCheckProperties: HealthCheckProperties? = null
61 private var endPointExecution: EndPointExecution? = null
63 private var bluePrintProcessorHealthCheck: BluePrintProcessorHealthCheck? = null
68 endPointExecution = Mockito.spy(endPointExecution!!)
69 Mockito.`when`(healthCheckProperties!!.getBluePrintServiceInformation()).thenReturn(Arrays.asList(
70 ServiceEndpoint("Execution service ", "http://cds-blueprints-processor-http:8080/api/v1/execution-service/health-check"),
71 ServiceEndpoint("Resources service", "http://cds-blueprints-processor-http:8080/api/v1/resources/health-check"), ServiceEndpoint("Template service", "http://cds-blueprints-processor-http:8080/api/v1/template/health-check")
74 bluePrintProcessorHealthCheck = BluePrintProcessorHealthCheck(endPointExecution!!, healthCheckProperties)
78 fun testSystemIsCompletelyDown() {
80 Mockito.`when`(basicAuthRestClientService!!.exchangeResource(
83 anyString())).thenThrow(RuntimeException())
84 val healthApiResponse = bluePrintProcessorHealthCheck!!.retrieveEndpointExecutionStatus()
85 assertNotNull(healthApiResponse)
86 Assert.assertEquals(HealthCheckStatus.DOWN, healthApiResponse.status)
87 healthApiResponse.checks.forEach { serviceEndpoint ->
88 assertNotNull(serviceEndpoint)
89 assertEquals(HealthCheckStatus.DOWN, serviceEndpoint.status)
96 fun testSystemIsUPAndRunning() {
98 Mockito.`when`(basicAuthRestClientService!!
102 anyString())).thenReturn(BlueprintWebClientService.WebClientResponse(200, "Success"))
103 val healthApiResponse = bluePrintProcessorHealthCheck!!.retrieveEndpointExecutionStatus()
104 assertNotNull(healthApiResponse)
105 assertEquals(HealthCheckStatus.UP, healthApiResponse.status)
106 healthApiResponse.checks.forEach { serviceEndpoint ->
107 assertNotNull(serviceEndpoint)
108 assertEquals(HealthCheckStatus.UP, serviceEndpoint.status)
115 fun testServiceIsNotFound() {
116 Mockito.`when`(basicAuthRestClientService!!.exchangeResource(
119 anyString())).thenReturn(BlueprintWebClientService.WebClientResponse(404, "failure"))
120 val healthApiResponse = bluePrintProcessorHealthCheck!!.retrieveEndpointExecutionStatus()
121 assertNotNull(healthApiResponse)
122 assertEquals(HealthCheckStatus.DOWN, healthApiResponse.status)
123 healthApiResponse.checks.forEach { serviceEndpoint ->
124 assertNotNull(serviceEndpoint)
125 assertEquals(HealthCheckStatus.DOWN, serviceEndpoint.status)
133 fun testServiceInternalServerError() {
134 Mockito.`when`(basicAuthRestClientService!!.exchangeResource(
138 .thenReturn(BlueprintWebClientService.WebClientResponse(500, "failure"))
139 val healthApiResponse = bluePrintProcessorHealthCheck!!.retrieveEndpointExecutionStatus()
140 assertNotNull(healthApiResponse)
141 assertEquals(HealthCheckStatus.DOWN, healthApiResponse.status)
142 healthApiResponse.checks.forEach { serviceEndpoint ->
143 assertNotNull(serviceEndpoint)
144 assertEquals(HealthCheckStatus.DOWN, serviceEndpoint.status)
151 fun testServiceIsRedirected() {
152 Mockito.`when`(basicAuthRestClientService!!.
157 .thenReturn(BlueprintWebClientService.WebClientResponse(300, "failure"))
158 val healthApiResponse = bluePrintProcessorHealthCheck!!.retrieveEndpointExecutionStatus()
159 assertNotNull(healthApiResponse)
160 assertEquals(HealthCheckStatus.DOWN, healthApiResponse.status)
161 healthApiResponse.checks.forEach { serviceEndpoint ->
162 assertNotNull(serviceEndpoint)
163 assertEquals(HealthCheckStatus.DOWN, serviceEndpoint.status)