Revert "Renaming Files having BluePrint to have Blueprint"
[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
20 import org.junit.Assert.assertEquals
21 import org.junit.Assert.assertNotNull
22 import org.junit.Before
23 import org.junit.Test
24 import org.junit.runner.RunWith
25 import org.mockito.InjectMocks
26 import org.mockito.Mock
27 import org.mockito.Mockito
28 import org.mockito.Mockito.anyString
29 import org.mockito.junit.MockitoJUnitRunner
30 import org.onap.ccsdk.cds.blueprintsprocessor.healthapi.configuration.HealthCheckProperties
31 import org.onap.ccsdk.cds.blueprintsprocessor.healthapi.domain.HealthCheckStatus
32 import org.onap.ccsdk.cds.blueprintsprocessor.healthapi.domain.ServiceEndpoint
33 import org.onap.ccsdk.cds.blueprintsprocessor.healthapi.service.EndPointExecution
34 import org.onap.ccsdk.cds.blueprintsprocessor.healthapi.service.health.BluePrintProcessorHealthCheck
35 import org.onap.ccsdk.cds.blueprintsprocessor.rest.BasicAuthRestClientProperties
36 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BasicAuthRestClientService
37 import org.onap.ccsdk.cds.blueprintsprocessor.rest.service.BlueprintWebClientService
38 import java.util.Arrays
39
40 @RunWith(MockitoJUnitRunner::class)
41 class HealthCheckServiceTest {
42
43     @Mock
44     private val basicAuthRestClientService: BasicAuthRestClientService? = null
45
46     @Mock
47     private val restClientProperties: BasicAuthRestClientProperties? = null
48
49     @Mock
50     private val healthCheckProperties: HealthCheckProperties? = null
51
52     @InjectMocks
53     private var endPointExecution: EndPointExecution? = null
54
55     private var bluePrintProcessorHealthCheck: BluePrintProcessorHealthCheck? = null
56
57     @Before
58     fun setup() {
59         endPointExecution = Mockito.spy(endPointExecution!!)
60         Mockito.`when`(healthCheckProperties!!.getBluePrintServiceInformation()).thenReturn(
61             Arrays.asList(
62                 ServiceEndpoint("Execution service ", "http://cds-blueprints-processor-http:8080/api/v1/execution-service/health-check"),
63                 ServiceEndpoint("Resources service", "http://cds-blueprints-processor-http:8080/api/v1/resources/health-check"),
64                 ServiceEndpoint("Template service", "http://cds-blueprints-processor-http:8080/api/v1/template/health-check")
65             )
66         )
67
68         bluePrintProcessorHealthCheck = BluePrintProcessorHealthCheck(endPointExecution!!, healthCheckProperties)
69     }
70
71     @Test
72     fun testSystemIsCompletelyDown() {
73
74         Mockito.`when`(
75             basicAuthRestClientService!!.exchangeResource(
76                 anyString(),
77                 anyString(),
78                 anyString()
79             )
80         ).thenThrow(RuntimeException())
81         val healthApiResponse = bluePrintProcessorHealthCheck!!.retrieveEndpointExecutionStatus()
82         assertNotNull(healthApiResponse)
83         Assert.assertEquals(HealthCheckStatus.DOWN, healthApiResponse.status)
84         healthApiResponse.checks.forEach { serviceEndpoint ->
85             assertNotNull(serviceEndpoint)
86             assertEquals(HealthCheckStatus.DOWN, serviceEndpoint.status)
87         }
88     }
89
90     @Test
91     fun testSystemIsUPAndRunning() {
92
93         Mockito.`when`(
94             basicAuthRestClientService!!
95                 .exchangeResource(
96                     anyString(),
97                     anyString(),
98                     anyString()
99                 )
100         ).thenReturn(BlueprintWebClientService.WebClientResponse(200, "Success"))
101         val healthApiResponse = bluePrintProcessorHealthCheck!!.retrieveEndpointExecutionStatus()
102         assertNotNull(healthApiResponse)
103         assertEquals(HealthCheckStatus.UP, healthApiResponse.status)
104         healthApiResponse.checks.forEach { serviceEndpoint ->
105             assertNotNull(serviceEndpoint)
106             assertEquals(HealthCheckStatus.UP, serviceEndpoint.status)
107         }
108     }
109
110     @Test
111     fun testServiceIsNotFound() {
112         Mockito.`when`(
113             basicAuthRestClientService!!.exchangeResource(
114                 anyString(),
115                 anyString(),
116                 anyString()
117             )
118         ).thenReturn(BlueprintWebClientService.WebClientResponse(404, "failure"))
119         val healthApiResponse = bluePrintProcessorHealthCheck!!.retrieveEndpointExecutionStatus()
120         assertNotNull(healthApiResponse)
121         assertEquals(HealthCheckStatus.DOWN, healthApiResponse.status)
122         healthApiResponse.checks.forEach { serviceEndpoint ->
123             assertNotNull(serviceEndpoint)
124             assertEquals(HealthCheckStatus.DOWN, serviceEndpoint.status)
125         }
126     }
127
128     @Test
129     fun testServiceInternalServerError() {
130         Mockito.`when`(
131             basicAuthRestClientService!!.exchangeResource(
132                 anyString(),
133                 anyString(),
134                 anyString()
135             )
136         )
137             .thenReturn(BlueprintWebClientService.WebClientResponse(500, "failure"))
138         val healthApiResponse = bluePrintProcessorHealthCheck!!.retrieveEndpointExecutionStatus()
139         assertNotNull(healthApiResponse)
140         assertEquals(HealthCheckStatus.DOWN, healthApiResponse.status)
141         healthApiResponse.checks.forEach { serviceEndpoint ->
142             assertNotNull(serviceEndpoint)
143             assertEquals(HealthCheckStatus.DOWN, serviceEndpoint.status)
144         }
145     }
146
147     @Test
148     fun testServiceIsRedirected() {
149         Mockito.`when`(
150             basicAuthRestClientService!!
151                 .exchangeResource(
152                     anyString(),
153                     anyString(),
154                     anyString()
155                 )
156         )
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 }