Skip to content

Commit

Permalink
Wonkey test fixed
Browse files Browse the repository at this point in the history
  • Loading branch information
oharsta committed Dec 7, 2023
1 parent d14d756 commit a358a59
Show file tree
Hide file tree
Showing 5 changed files with 92 additions and 40 deletions.
3 changes: 1 addition & 2 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,12 @@
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.13</version>
<version>2.7.17</version>
</parent>

<modules>
<module>teams-server</module>
<module>teams-gui</module>
<!-- <module>teams-old</module>-->
</modules>

<properties>
Expand Down
9 changes: 6 additions & 3 deletions teams-server/src/main/java/teams/api/InviteController.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import org.apache.commons.codec.binary.Base64;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.HttpStatus;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.util.CollectionUtils;
Expand Down Expand Up @@ -77,10 +79,11 @@ private ResponseEntity<Map<String, Integer>> doMigrate(Map<String, Long> teamIde
if (CollectionUtils.isEmpty(applications)) {
throw new IllegalArgumentException("No applications");
}
restTemplate.put(inviteUrl, team);
ResponseEntity<Map<String, Integer>> responseEntity = restTemplate.exchange(inviteUrl, HttpMethod.PUT, new HttpEntity<>(team), new ParameterizedTypeReference<>() {
});
teamRepository.delete(team);

return ResponseEntity.status(HttpStatus.CREATED).body(Map.of("status", HttpStatus.CREATED.value()));
return responseEntity;
}

private static void confirmFederatedUser(FederatedUser federatedUser) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public abstract class AbstractApplicationTest implements Seed {
protected ObjectMapper objectMapper;

@LocalServerPort
private int serverPort;
protected int serverPort;

@Before
public void before() throws Exception {
Expand Down
27 changes: 27 additions & 0 deletions teams-server/src/test/java/teams/WireMockExtension.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package teams;

import com.github.tomakehurst.wiremock.WireMockServer;
import com.github.tomakehurst.wiremock.client.WireMock;
import org.junit.jupiter.api.extension.AfterEachCallback;
import org.junit.jupiter.api.extension.BeforeEachCallback;
import org.junit.jupiter.api.extension.ExtensionContext;

public class WireMockExtension extends WireMockServer implements BeforeEachCallback, AfterEachCallback {

public WireMockExtension(int port) {
super(port);
}

@Override
public void beforeEach(ExtensionContext context) {
this.start();
WireMock.configureFor("localhost", port());
}

@Override
public void afterEach(ExtensionContext context) {
this.stop();
this.resetAll();
}

}
91 changes: 57 additions & 34 deletions teams-server/src/test/java/teams/api/InviteControllerTest.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
package teams.api;

import com.github.tomakehurst.wiremock.junit.WireMockRule;
import io.restassured.common.mapper.TypeRef;
import io.restassured.http.ContentType;
import org.junit.Rule;
import org.junit.Test;
import lombok.SneakyThrows;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.springframework.http.HttpHeaders;
import teams.AbstractApplicationTest;
import teams.WireMockExtension;

import java.util.List;
import java.util.Map;
Expand All @@ -14,44 +17,31 @@
import static io.restassured.RestAssured.given;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.springframework.util.MimeTypeUtils.APPLICATION_JSON_VALUE;

@SuppressWarnings("unchecked")
public class InviteControllerTest extends AbstractApplicationTest {
class InviteControllerTest extends AbstractApplicationTest {

@Rule
public WireMockRule wireMockRule = new WireMockRule(8888);
@RegisterExtension
WireMockExtension mockServer = new WireMockExtension(8888);

@Test
public void teamDetails() {
Map map = given()
.header("name-id", "urn:collab:person:surfnet.nl:super_admin")
.when()
.pathParams("id", String.valueOf(2))
.get("/api/teams/invite-app/{id}")
.as(new TypeRef<>() {
});
assertEquals(2, ((List) map.get("applications")).size());
}

@Test
public void teamDetailsExternal() {
Map map = given()
.auth()
.preemptive()
.basic("teams", "secret")
.when()
.pathParams("id", String.valueOf(2))
.get("/api/v1/external/invite-app/{id}")
.as(new TypeRef<>() {
});
assertEquals(2, ((List) map.get("applications")).size());
@SneakyThrows
@BeforeEach
void beforeEach() {
super.before();
}

@SneakyThrows
@Test
public void migrateTeam() {
void migrateTeam() {
String body = super.objectMapper.writeValueAsString(Map.of("status", 201));
stubFor(put(
urlEqualTo("/api/teams"))
.willReturn(aResponse().withStatus(200)));
.willReturn(aResponse()
.withBody(body)
.withHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON_VALUE)
.withHeader(HttpHeaders.CONNECTION, "close")
.withStatus(201)));

assertTrue(super.teamRepository.findById(2L).isPresent());

Expand All @@ -65,11 +55,17 @@ public void migrateTeam() {
.statusCode(201);
}

@SneakyThrows
@Test
public void migrateTeamExternal() {
void migrateTeamExternal() {
String body = super.objectMapper.writeValueAsString(Map.of("status", 201));
stubFor(put(
urlEqualTo("/api/teams"))
.willReturn(aResponse().withStatus(200)));
.willReturn(aResponse()
.withBody(body)
.withHeader(HttpHeaders.CONTENT_TYPE, APPLICATION_JSON_VALUE)
.withHeader(HttpHeaders.CONNECTION, "close")
.withStatus(201)));

assertTrue(super.teamRepository.findById(2L).isPresent());

Expand All @@ -84,4 +80,31 @@ public void migrateTeamExternal() {
.then()
.statusCode(201);
}

@Test
void teamDetails() {
Map map = given()
.header("name-id", "urn:collab:person:surfnet.nl:super_admin")
.when()
.pathParams("id", String.valueOf(2))
.get("/api/teams/invite-app/{id}")
.as(new TypeRef<>() {
});
assertEquals(2, ((List) map.get("applications")).size());
}

@Test
void teamDetailsExternal() {
Map map = given()
.auth()
.preemptive()
.basic("teams", "secret")
.when()
.pathParams("id", String.valueOf(2))
.get("/api/v1/external/invite-app/{id}")
.as(new TypeRef<>() {
});
assertEquals(2, ((List) map.get("applications")).size());
}

}

0 comments on commit a358a59

Please sign in to comment.