|
| 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.notification; |
| 18 | + |
| 19 | +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; |
| 20 | +import static com.github.tomakehurst.wiremock.client.WireMock.equalTo; |
| 21 | +import static com.github.tomakehurst.wiremock.client.WireMock.putRequestedFor; |
| 22 | +import static com.github.tomakehurst.wiremock.client.WireMock.urlPathEqualTo; |
| 23 | +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig; |
| 24 | + |
| 25 | +import com.fasterxml.jackson.core.JsonProcessingException; |
| 26 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 27 | +import com.github.tomakehurst.wiremock.client.WireMock; |
| 28 | +import com.github.tomakehurst.wiremock.junit5.WireMockExtension; |
| 29 | +import com.netflix.spinnaker.echo.api.events.Event; |
| 30 | +import com.netflix.spinnaker.echo.api.events.Metadata; |
| 31 | +import com.netflix.spinnaker.echo.model.pubsub.MessageDescription; |
| 32 | +import com.netflix.spinnaker.echo.services.IgorService; |
| 33 | +import com.netflix.spinnaker.echo.util.RetrofitUtils; |
| 34 | +import com.netflix.spinnaker.kork.core.RetrySupport; |
| 35 | +import com.netflix.spinnaker.kork.retrofit.ErrorHandlingExecutorCallAdapterFactory; |
| 36 | +import java.util.HashMap; |
| 37 | +import java.util.Map; |
| 38 | +import okhttp3.OkHttpClient; |
| 39 | +import org.junit.jupiter.api.BeforeAll; |
| 40 | +import org.junit.jupiter.api.Test; |
| 41 | +import org.junit.jupiter.api.extension.RegisterExtension; |
| 42 | +import retrofit2.Retrofit; |
| 43 | +import retrofit2.converter.jackson.JacksonConverterFactory; |
| 44 | + |
| 45 | +public class GoogleCloudBuildNotificationAgentTest { |
| 46 | + public static final String ACCOUNT = "my-account"; |
| 47 | + public static final String BUILD_ID = "1a9ea355-eb3d-4148-b81b-875d07ea118b"; |
| 48 | + public static final String BUILD_STATUS = "QUEUED"; |
| 49 | + |
| 50 | + @RegisterExtension |
| 51 | + static final WireMockExtension wmIgor = |
| 52 | + WireMockExtension.newInstance().options(wireMockConfig().dynamicPort()).build(); |
| 53 | + |
| 54 | + static GoogleCloudBuildNotificationAgent notificationAgent; |
| 55 | + static ObjectMapper mapper = new ObjectMapper(); |
| 56 | + |
| 57 | + @BeforeAll |
| 58 | + public static void setup() { |
| 59 | + // Create IgorService |
| 60 | + IgorService igorService = |
| 61 | + new Retrofit.Builder() |
| 62 | + .baseUrl(RetrofitUtils.getBaseUrl(wmIgor.baseUrl())) |
| 63 | + .client(new OkHttpClient()) |
| 64 | + .addCallAdapterFactory(ErrorHandlingExecutorCallAdapterFactory.getInstance()) |
| 65 | + .addConverterFactory(JacksonConverterFactory.create()) |
| 66 | + .build() |
| 67 | + .create(IgorService.class); |
| 68 | + |
| 69 | + notificationAgent = new GoogleCloudBuildNotificationAgent(igorService, new RetrySupport()); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testGoogleCloudBuildNotificationAgent() throws JsonProcessingException { |
| 74 | + String updateBuildStatusUrl = "/gcb/builds/" + ACCOUNT + "/" + BUILD_ID; |
| 75 | + |
| 76 | + wmIgor.stubFor( |
| 77 | + WireMock.put(urlPathEqualTo(updateBuildStatusUrl)) |
| 78 | + .withQueryParam("status", equalTo(BUILD_STATUS)) |
| 79 | + .willReturn(aResponse().withStatus(200).withBody("[]"))); |
| 80 | + |
| 81 | + notificationAgent.processEvent(createEvent()); |
| 82 | + |
| 83 | + wmIgor.verify( |
| 84 | + 1, |
| 85 | + putRequestedFor(urlPathEqualTo(updateBuildStatusUrl)) |
| 86 | + .withQueryParam("status", equalTo(BUILD_STATUS))); |
| 87 | + } |
| 88 | + |
| 89 | + private Event createEvent() throws JsonProcessingException { |
| 90 | + MessageDescription messageDescription = |
| 91 | + MessageDescription.builder() |
| 92 | + .subscriptionName(ACCOUNT) |
| 93 | + .messagePayload(mapper.writeValueAsString(Map.of("key", "value"))) |
| 94 | + .messageAttributes(Map.of("buildId", BUILD_ID, "status", BUILD_STATUS)) |
| 95 | + .build(); |
| 96 | + Map<String, Object> content = new HashMap<>(); |
| 97 | + content.put("messageDescription", messageDescription); |
| 98 | + |
| 99 | + Metadata details = new Metadata(); |
| 100 | + details.setType("googleCloudBuild"); |
| 101 | + |
| 102 | + Event event = new Event(); |
| 103 | + event.setContent(content); |
| 104 | + event.setDetails(details); |
| 105 | + return event; |
| 106 | + } |
| 107 | +} |
0 commit comments