|
| 1 | +/* |
| 2 | + * Copyright 2025 OpsMx, Inc. |
| 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 com.netflix.spinnaker.echo.events; |
| 18 | + |
| 19 | +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
| 20 | +import static com.github.tomakehurst.wiremock.client.WireMock.post; |
| 21 | +import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor; |
| 22 | +import static com.github.tomakehurst.wiremock.client.WireMock.stubFor; |
| 23 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo; |
| 24 | +import static com.github.tomakehurst.wiremock.client.WireMock.verify; |
| 25 | + |
| 26 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 27 | +import com.github.tomakehurst.wiremock.WireMockServer; |
| 28 | +import com.github.tomakehurst.wiremock.client.WireMock; |
| 29 | +import com.github.tomakehurst.wiremock.core.WireMockConfiguration; |
| 30 | +import com.netflix.spinnaker.echo.api.events.Event; |
| 31 | +import com.netflix.spinnaker.echo.config.RestProperties; |
| 32 | +import com.netflix.spinnaker.echo.config.RestUrls; |
| 33 | +import com.netflix.spinnaker.echo.rest.RestService; |
| 34 | +import com.netflix.spinnaker.echo.util.RetrofitUtils; |
| 35 | +import com.netflix.spinnaker.kork.core.RetrySupport; |
| 36 | +import com.netflix.spinnaker.kork.retrofit.ErrorHandlingExecutorCallAdapterFactory; |
| 37 | +import io.github.resilience4j.circuitbreaker.CircuitBreakerConfig; |
| 38 | +import io.github.resilience4j.circuitbreaker.CircuitBreakerRegistry; |
| 39 | +import java.util.Map; |
| 40 | +import okhttp3.OkHttpClient; |
| 41 | +import org.junit.jupiter.api.AfterAll; |
| 42 | +import org.junit.jupiter.api.BeforeAll; |
| 43 | +import org.junit.jupiter.api.Test; |
| 44 | +import retrofit2.Retrofit; |
| 45 | +import retrofit2.converter.jackson.JacksonConverterFactory; |
| 46 | + |
| 47 | +public class RestEventServiceTest { |
| 48 | + private static Map<String, Object> eventMap; |
| 49 | + private static RestEventService restEventService; |
| 50 | + private static RestUrls.Service service; |
| 51 | + |
| 52 | + static WireMockServer wireMockServer; |
| 53 | + static int port; |
| 54 | + static String baseUrl = "http://localhost:PORT/api"; |
| 55 | + |
| 56 | + @BeforeAll |
| 57 | + static void setup() { |
| 58 | + wireMockServer = new WireMockServer(WireMockConfiguration.options().dynamicPort()); |
| 59 | + wireMockServer.start(); |
| 60 | + port = wireMockServer.port(); |
| 61 | + WireMock.configureFor("localhost", port); |
| 62 | + |
| 63 | + stubFor(post(urlEqualTo("/api/")).willReturn(aResponse().withStatus(200))); |
| 64 | + |
| 65 | + baseUrl = baseUrl.replaceFirst("PORT", String.valueOf(port)); |
| 66 | + |
| 67 | + // Create the RestService |
| 68 | + RestService restService = |
| 69 | + new Retrofit.Builder() |
| 70 | + .baseUrl(RetrofitUtils.getBaseUrl(baseUrl)) |
| 71 | + .client(new OkHttpClient()) |
| 72 | + .addCallAdapterFactory(ErrorHandlingExecutorCallAdapterFactory.getInstance()) |
| 73 | + .addConverterFactory(JacksonConverterFactory.create()) |
| 74 | + .build() |
| 75 | + .create(RestService.class); |
| 76 | + |
| 77 | + // Create a custom configuration for a CircuitBreaker |
| 78 | + CircuitBreakerConfig circuitBreakerConfig = |
| 79 | + CircuitBreakerConfig.custom() |
| 80 | + .failureRateThreshold(1) |
| 81 | + .permittedNumberOfCallsInHalfOpenState(1) |
| 82 | + .minimumNumberOfCalls(1) |
| 83 | + .build(); |
| 84 | + |
| 85 | + // Create a CircuitBreakerRegistry with a custom configuration |
| 86 | + CircuitBreakerRegistry circuitBreakerRegistry = CircuitBreakerRegistry.of(circuitBreakerConfig); |
| 87 | + |
| 88 | + // Get the CircuitBreaker from the CircuitBreakerRegistry with a custom configuration |
| 89 | + circuitBreakerRegistry.circuitBreaker("circuitBreakerTest", circuitBreakerConfig); |
| 90 | + |
| 91 | + restEventService = new RestEventService(new RetrySupport(), circuitBreakerRegistry); |
| 92 | + |
| 93 | + Event event = new Event(); |
| 94 | + event.setContent(Map.of("uno", "dos")); |
| 95 | + ObjectMapper mapper = new ObjectMapper(); |
| 96 | + eventMap = mapper.convertValue(event, Map.class); |
| 97 | + |
| 98 | + service = |
| 99 | + RestUrls.Service.builder() |
| 100 | + .client(restService) |
| 101 | + .config(new RestProperties.RestEndpointConfiguration()) |
| 102 | + .build(); |
| 103 | + } |
| 104 | + |
| 105 | + @AfterAll |
| 106 | + static void cleanup() { |
| 107 | + wireMockServer.stop(); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + void testSendEvent() { |
| 112 | + restEventService.sendEvent(eventMap, service); |
| 113 | + verify(1, postRequestedFor(urlEqualTo("/api/"))); |
| 114 | + } |
| 115 | +} |
0 commit comments