c4a8d1235f4f929435660464f8b1f524c4a4f691
[ccsdk/cds.git] / ms / blueprintsprocessor / modules / inbounds / health-api-common / src / test / kotlin / org / onap / ccsdk / cds / blueprintsprocessor / healthapi / HealthCheckServiceTest.kt
1 /*
2  * Copyright © 2019-2020 Orange.
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.healthapi
18
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
25
26 import java.util.Arrays
27 import org.junit.Assert
28 import org.junit.Before
29 import org.junit.Test
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
47
48 @RunWith(MockitoJUnitRunner::class)
49 class HealthCheckServiceTest {
50
51     @Mock
52     private val basicAuthRestClientService: BasicAuthRestClientService? = null
53
54     @Mock
55     private val restClientProperties: BasicAuthRestClientProperties? = null
56
57     @Mock
58     private val healthCheckProperties: HealthCheckProperties? = null
59
60     @InjectMocks
61     private var endPointExecution: EndPointExecution? = null
62
63     private var bluePrintProcessorHealthCheck: BluePrintProcessorHealthCheck? = null
64
65
66     @Before
67     fun setup() {
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")
72         ))
73
74         bluePrintProcessorHealthCheck = BluePrintProcessorHealthCheck(endPointExecution!!, healthCheckProperties)
75     }
76
77   @Test
78     fun testSystemIsCompletelyDown() {
79
80         Mockito.`when`(basicAuthRestClientService!!.exchangeResource(
81                         anyString(),
82                         anyString(),
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)
90
91         }
92
93     }
94
95     @Test
96     fun testSystemIsUPAndRunning() {
97
98         Mockito.`when`(basicAuthRestClientService!!
99         .exchangeResource(
100                         anyString(),
101                         anyString(),
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)
109
110         }
111
112     }
113
114     @Test
115     fun testServiceIsNotFound() {
116         Mockito.`when`(basicAuthRestClientService!!.exchangeResource(
117                         anyString(),
118                         anyString(),
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)
126
127         }
128
129     }
130
131
132     @Test
133     fun testServiceInternalServerError() {
134         Mockito.`when`(basicAuthRestClientService!!.exchangeResource(
135                         anyString(),
136                         anyString(),
137                        anyString()))
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)
145
146         }
147
148     }
149
150     @Test
151     fun testServiceIsRedirected() {
152         Mockito.`when`(basicAuthRestClientService!!.
153                 exchangeResource(
154                         anyString(),
155                         anyString(),
156                        anyString()))
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)
164
165         }
166     }
167 }