Skip to content

Commit

Permalink
Add test
Browse files Browse the repository at this point in the history
  • Loading branch information
rnc committed Dec 13, 2024
1 parent c5b3a1b commit 5d27dad
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 1 deletion.
6 changes: 6 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,12 @@
<artifactId>quarkus-test-kubernetes-client</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock</artifactId>
<version>3.10.0</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
29 changes: 28 additions & 1 deletion src/test/java/org/jboss/pnc/konfluxbuilddriver/DriverTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,30 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.List;
import java.util.logging.LogRecord;

import javax.ws.rs.core.MediaType;

import jakarta.inject.Inject;

import org.eclipse.microprofile.rest.client.inject.RestClient;
import org.jboss.pnc.api.constants.HttpHeaders;
import org.jboss.pnc.api.dto.Request;
import org.jboss.pnc.konfluxbuilddriver.clients.IndyService;
import org.jboss.pnc.konfluxbuilddriver.clients.IndyTokenRequestDTO;
import org.jboss.pnc.konfluxbuilddriver.clients.IndyTokenResponseDTO;
import org.jboss.pnc.konfluxbuilddriver.dto.BuildRequest;
import org.jboss.pnc.konfluxbuilddriver.dto.BuildResponse;
import org.jboss.pnc.konfluxbuilddriver.dto.CancelRequest;
import org.jboss.pnc.konfluxbuilddriver.dto.PipelineNotification;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import com.github.tomakehurst.wiremock.WireMockServer;

import io.fabric8.kubernetes.client.KubernetesClient;
import io.fabric8.kubernetes.client.server.mock.KubernetesServer;
import io.fabric8.tekton.client.TektonClient;
Expand All @@ -34,10 +43,13 @@
@WithKubernetesTestServer
@QuarkusTest
@QuarkusTestResource(value = LogCollectingTestResource.class, restrictToAnnotatedClass = true, initArgs = @ResourceArg(name = LogCollectingTestResource.LEVEL, value = "FINE"))
@QuarkusTestResource(WireMockExtensions.class)
public class DriverTest {

private static final String namespace = "test-namespace";

private WireMockServer wireMockServer;

@KubernetesTestServer
KubernetesServer mockServer;

Expand All @@ -60,7 +72,7 @@ public void setup() {
}

@Test
void verify() {
void cancel() {
BuildRequest request = BuildRequest.builder().namespace(namespace).podMemoryOverride("1Gi").build();
BuildResponse response = driver.create(request);

Expand All @@ -78,4 +90,19 @@ void verify() {
assertTrue(logRecords.stream().anyMatch(r -> LogCollectingTestResource.format(r)
.contains("Retrieved pipeline run-mw-pipeline--00000000-0000-0000-0000-000000000005")));
}

@Test
public void testCompleted() throws URISyntaxException {

Request request = Request.builder()
.method(Request.Method.PUT)
.header(new Request.Header(HttpHeaders.CONTENT_TYPE_STRING, MediaType.APPLICATION_JSON))
.attachment(null)
.uri(new URI(wireMockServer.baseUrl() + "/invoker"))
.build();

driver.completed(
PipelineNotification.builder().completionCallback(request).buildId("1234").status("Succeeded").build());
assertEquals(200, wireMockServer.getServeEvents().getServeEvents().getFirst().getResponse().getStatus());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package org.jboss.pnc.konfluxbuilddriver;

import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson;
import static com.github.tomakehurst.wiremock.client.WireMock.put;
import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;

import java.util.Map;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.common.ConsoleNotifier;

import io.quarkus.test.common.QuarkusTestResourceLifecycleManager;

public class WireMockExtensions implements QuarkusTestResourceLifecycleManager {
private WireMockServer wireMockServer;

@Override
public Map<String, String> start() {
wireMockServer = new WireMockServer(wireMockConfig().notifier(new ConsoleNotifier(true)));
wireMockServer.start();

wireMockServer.stubFor(
put(urlEqualTo("/invoker"))
.withRequestBody(
equalToJson("{\"status\":\"Succeeded\",\"buildId\":\"1234\"}"))
.willReturn(aResponse()
.withStatus(200)));

return Map.of("quarkus.rest-client.wiremockextensions.url", wireMockServer.baseUrl());
}

@Override
public void stop() {
if (wireMockServer != null) {
wireMockServer.stop();
}
}

@Override
public void inject(TestInjector testInjector) {
testInjector.injectIntoFields(wireMockServer, new TestInjector.MatchesType(WireMockServer.class));
}
}

0 comments on commit 5d27dad

Please sign in to comment.