Skip to content

Commit

Permalink
feat: update tests to mockserver (#79)
Browse files Browse the repository at this point in the history
* feat: fix package name

* feat: switch tests to tenant mockserver

* fix: sonar issues
  • Loading branch information
andrejpetras authored Nov 29, 2024
1 parent 02ae3d0 commit 27333b4
Show file tree
Hide file tree
Showing 15 changed files with 543 additions and 23 deletions.
7 changes: 6 additions & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,11 @@
<artifactId>tkit-quarkus-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>io.quarkiverse.mockserver</groupId>
<artifactId>quarkus-mockserver-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down Expand Up @@ -203,7 +208,7 @@
<configuration>
<inputSpec>src/main/openapi/openapi-internal.yaml</inputSpec>
<apiPackage>gen.org.tkit.onecx.parameters.rs.internal</apiPackage>
<modelPackage>gen.org.tki.onecx.parameters.rs.internal.model</modelPackage>
<modelPackage>gen.org.tkit.onecx.parameters.rs.internal.model</modelPackage>
</configuration>
</execution>
<execution>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,9 @@
import org.tkit.onecx.parameters.rs.internal.mappers.ApplicationParameterInternalMapper;
import org.tkit.quarkus.log.cdi.LogService;

import gen.org.tki.onecx.parameters.rs.internal.model.ApplicationParameterHistoryCriteriaDTO;
import gen.org.tki.onecx.parameters.rs.internal.model.ParameterHistoryCountCriteriaDTO;
import gen.org.tkit.onecx.parameters.rs.internal.HistoriesApi;
import gen.org.tkit.onecx.parameters.rs.internal.model.ApplicationParameterHistoryCriteriaDTO;
import gen.org.tkit.onecx.parameters.rs.internal.model.ParameterHistoryCountCriteriaDTO;

@LogService
@ApplicationScoped
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import org.tkit.quarkus.jpa.exceptions.ConstraintException;
import org.tkit.quarkus.log.cdi.LogService;

import gen.org.tki.onecx.parameters.rs.internal.model.*;
import gen.org.tkit.onecx.parameters.rs.internal.ParametersApi;
import gen.org.tkit.onecx.parameters.rs.internal.model.*;

@LogService
@ApplicationScoped
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,31 @@

import org.tkit.quarkus.log.cdi.LogParam;

import gen.org.tki.onecx.parameters.rs.internal.model.ApplicationParameterCreateDTO;
import gen.org.tki.onecx.parameters.rs.internal.model.ApplicationParameterUpdateDTO;
import gen.org.tkit.onecx.parameters.rs.internal.model.*;

@ApplicationScoped
public class InternalLogParam implements LogParam {

@Override
public List<Item> getClasses() {
return List.of(
item(10, ApplicationParameterHistoryCriteriaDTO.class, x -> {
ApplicationParameterHistoryCriteriaDTO d = (ApplicationParameterHistoryCriteriaDTO) x;
return ApplicationParameterHistoryCriteriaDTO.class.getSimpleName() + "[" + d.getPageNumber() + ","
+ d.getPageSize()
+ "]";
}),
item(10, ParameterHistoryCountCriteriaDTO.class, x -> {
ParameterHistoryCountCriteriaDTO d = (ParameterHistoryCountCriteriaDTO) x;
return ParameterHistoryCountCriteriaDTO.class.getSimpleName() + "[" + d.getPageNumber() + ","
+ d.getPageSize()
+ "]";
}),
item(10, ParameterSearchCriteriaDTO.class, x -> {
ParameterSearchCriteriaDTO d = (ParameterSearchCriteriaDTO) x;
return ParameterSearchCriteriaDTO.class.getSimpleName() + "[" + d.getPageNumber() + "," + d.getPageSize()
+ "]";
}),
item(10, ApplicationParameterCreateDTO.class,
x -> x.getClass().getSimpleName() + ":" + ((ApplicationParameterCreateDTO) x).getKey()),
item(10, ApplicationParameterUpdateDTO.class,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
import org.tkit.quarkus.jpa.daos.PageResult;
import org.tkit.quarkus.rs.mappers.OffsetDateTimeMapper;

import gen.org.tki.onecx.parameters.rs.internal.model.*;
import gen.org.tkit.onecx.parameters.rs.internal.model.*;

@Mapper(uses = { OffsetDateTimeMapper.class })
public interface ApplicationParameterInternalMapper {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
import org.tkit.quarkus.log.cdi.LogService;
import org.tkit.quarkus.rs.mappers.OffsetDateTimeMapper;

import gen.org.tki.onecx.parameters.rs.internal.model.ProblemDetailInvalidParamDTO;
import gen.org.tki.onecx.parameters.rs.internal.model.ProblemDetailParamDTO;
import gen.org.tki.onecx.parameters.rs.internal.model.ProblemDetailResponseDTO;
import gen.org.tkit.onecx.parameters.rs.internal.model.ProblemDetailInvalidParamDTO;
import gen.org.tkit.onecx.parameters.rs.internal.model.ProblemDetailParamDTO;
import gen.org.tkit.onecx.parameters.rs.internal.model.ProblemDetailResponseDTO;
import lombok.extern.slf4j.Slf4j;

@Slf4j
Expand Down
14 changes: 6 additions & 8 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,16 @@ tkit.rs.context.tenant-id.enabled=true

# TEST
%test.quarkus.scheduler.enabled=false
# Enable mocking for tenant service

%test.tkit.jpa.tenant.default=tenant-100
%test.tkit.rs.context.tenant-id.enabled=true
%test.tkit.rs.context.tenant-id.mock.enabled=true
%test.tkit.rs.context.tenant-id.mock.default-tenant=tenant-100
%test.tkit.rs.context.tenant-id.mock.claim-org-id=orgId
%test.tkit.rs.context.tenant-id.mock.data.org1=tenant-100
%test.tkit.rs.context.tenant-id.mock.data.org2=tenant-200
%test.onecx.tenant.service.client.url=${quarkus.mockserver.endpoint}
%test.smallrye.jwt.verify.key.location=${keycloak.url}/realms/quarkus/protocol/openid-connect/certs
%test.tkit.rs.context.token.header-param=apm-principal-token
# set JSON console formatting extension.

%test.tkit.log.json.enabled=false
%test.quarkus.mockserver.devservices.log=false

# TEST-IT
quarkus.test.integration-test-profile=test
quarkus.test.enable-callbacks-for-integration-tests=true

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,30 @@

import static io.restassured.RestAssured.given;
import static jakarta.ws.rs.core.MediaType.APPLICATION_JSON;
import static org.mockserver.model.HttpRequest.request;
import static org.mockserver.model.HttpResponse.response;
import static org.tkit.quarkus.security.test.SecurityTestUtils.getKeycloakClientToken;

import java.util.Map;

import jakarta.ws.rs.HttpMethod;
import jakarta.ws.rs.core.Response;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockserver.client.MockServerClient;
import org.mockserver.model.JsonBody;
import org.mockserver.model.MediaType;
import org.tkit.onecx.parameters.rs.external.v1.controllers.ParameterRestControllerV1;
import org.tkit.onecx.parameters.test.AbstractTest;
import org.tkit.quarkus.security.test.GenerateKeycloakClient;
import org.tkit.quarkus.test.WithDBData;

import gen.org.tkit.onecx.parameters.rs.v1.model.ParameterInfoDTOV1;
import gen.org.tkit.onecx.parameters.rs.v1.model.ParametersBucketDTOV1;
import gen.org.tkit.onecx.tenant.client.model.TenantId;
import io.quarkiverse.mockserver.test.InjectMockServerClient;
import io.quarkus.test.common.http.TestHTTPEndpoint;
import io.quarkus.test.junit.QuarkusTest;
import io.restassured.path.json.JsonPath;
Expand All @@ -26,10 +35,27 @@
@GenerateKeycloakClient(clientName = "testClient", scopes = { "ocx-pa-ap:read", "ocx-pa-me:write" })
class ParameterRestControllerV1Test extends AbstractTest {

@InjectMockServerClient
MockServerClient mockServerClient;

@BeforeEach
void resetExpectation() {
clearExpectation(mockServerClient);
}

@Test
void shouldNotFindParametersWithGivenApplicationId() {

var apm = createToken("org1");
addExpectation(mockServerClient
.when(request().withPath("/v1/tenant").withMethod(HttpMethod.GET).withHeader("apm-principal-token", apm))
.respond(httpRequest -> response().withStatusCode(Response.Status.OK.getStatusCode())
.withContentType(MediaType.APPLICATION_JSON)
.withBody(JsonBody.json(new TenantId().tenantId("tenant-100")))));

Map<String, String> applicationParameters = given()
.auth().oauth2(getKeycloakClientToken("testClient"))
.header(HEADER_APM_TOKEN, apm)
.when()
.pathParam("productName", "not-exist")
.pathParam("appId", "not-exist")
Expand All @@ -45,8 +71,17 @@ void shouldNotFindParametersWithGivenApplicationId() {
@Test
@WithDBData(value = { "data/parameters-importdata.xml" }, deleteBeforeInsert = true, rinseAndRepeat = true)
void shouldReturnImportValueParameter() {

var apm = createToken("org1");
addExpectation(mockServerClient
.when(request().withPath("/v1/tenant").withMethod(HttpMethod.GET).withHeader("apm-principal-token", apm))
.respond(httpRequest -> response().withStatusCode(Response.Status.OK.getStatusCode())
.withContentType(MediaType.APPLICATION_JSON)
.withBody(JsonBody.json(new TenantId().tenantId("tenant-100")))));

Map<String, String> applicationParameters = given()
.auth().oauth2(getKeycloakClientToken("testClient"))
.header(HEADER_APM_TOKEN, apm)
.when()
.contentType(APPLICATION_JSON)
.pathParam("productName", "import-product")
Expand All @@ -61,11 +96,47 @@ void shouldReturnImportValueParameter() {
Assertions.assertEquals("import-value", applicationParameters.get("importParam"));
}

@Test
@WithDBData(value = { "data/parameters-importdata.xml" }, deleteBeforeInsert = true, rinseAndRepeat = true)
void shouldReturnImportValueParameterOrg2() {

var apm = createToken("org2");
addExpectation(mockServerClient
.when(request().withPath("/v1/tenant").withMethod(HttpMethod.GET).withHeader("apm-principal-token", apm))
.respond(httpRequest -> response().withStatusCode(Response.Status.OK.getStatusCode())
.withContentType(MediaType.APPLICATION_JSON)
.withBody(JsonBody.json(new TenantId().tenantId("tenant-200")))));

Map<String, String> applicationParameters = given()
.auth().oauth2(getKeycloakClientToken("testClient"))
.when()
.header("apm-principal-token", apm)
.contentType(APPLICATION_JSON)
.pathParam("productName", "import-product")
.pathParam("appId", "import-app")
.get("parameters")
.then()
.statusCode(Response.Status.OK.getStatusCode())
.contentType(APPLICATION_JSON)
.extract()
.body().jsonPath().getMap(".", String.class, String.class);
Assertions.assertEquals(1, applicationParameters.size());
Assertions.assertEquals("import-value-200", applicationParameters.get("importParam"));
}

@Test
@WithDBData(value = { "data/parameters-testdata.xml" }, deleteBeforeInsert = true, rinseAndRepeat = true)
void shouldReturnParameter() {
var apm = createToken("org1");
addExpectation(mockServerClient
.when(request().withPath("/v1/tenant").withMethod(HttpMethod.GET).withHeader("apm-principal-token", apm))
.respond(httpRequest -> response().withStatusCode(Response.Status.OK.getStatusCode())
.withContentType(MediaType.APPLICATION_JSON)
.withBody(JsonBody.json(new TenantId().tenantId("tenant-100")))));

Map<String, String> applicationParameters = given()
.auth().oauth2(getKeycloakClientToken("testClient"))
.header(HEADER_APM_TOKEN, apm)
.when()
.pathParam("productName", "access-mgmt-product")
.pathParam("appId", "access-mgmt")
Expand All @@ -82,8 +153,16 @@ void shouldReturnParameter() {
@Test
@WithDBData(value = { "data/parameters-testdata.xml" }, deleteBeforeInsert = true, rinseAndRepeat = true)
void shouldNotReturnParameterWithNullSetValue() {
var apm = createToken("org1");
addExpectation(mockServerClient
.when(request().withPath("/v1/tenant").withMethod(HttpMethod.GET).withHeader("apm-principal-token", apm))
.respond(httpRequest -> response().withStatusCode(Response.Status.OK.getStatusCode())
.withContentType(MediaType.APPLICATION_JSON)
.withBody(JsonBody.json(new TenantId().tenantId("tenant-100")))));

JsonPath applicationParameters = given()
.auth().oauth2(getKeycloakClientToken("testClient"))
.header(HEADER_APM_TOKEN, apm)
.when()
.pathParam("productName", "access-mgmt-product")
.pathParam("appId", "access-mgmt")
Expand All @@ -100,6 +179,13 @@ void shouldNotReturnParameterWithNullSetValue() {
@Test
@WithDBData(value = { "data/parameters-testdata.xml" }, deleteBeforeInsert = true, rinseAndRepeat = true)
void shouldCreateNewParameter() {
var apm = createToken("org1");
addExpectation(mockServerClient
.when(request().withPath("/v1/tenant").withMethod(HttpMethod.GET).withHeader("apm-principal-token", apm))
.respond(httpRequest -> response().withStatusCode(Response.Status.OK.getStatusCode())
.withContentType(MediaType.APPLICATION_JSON)
.withBody(JsonBody.json(new TenantId().tenantId("tenant-100")))));

ParametersBucketDTOV1 parametersBucketDTO = new ParametersBucketDTOV1();
ParameterInfoDTOV1 parameterInfoDTO1 = new ParameterInfoDTOV1();
parameterInfoDTO1.setCount(1L);
Expand All @@ -109,6 +195,7 @@ void shouldCreateNewParameter() {
parametersBucketDTO.getParameters().put("testKey", parameterInfoDTO1);
given()
.auth().oauth2(getKeycloakClientToken("testClient"))
.header(HEADER_APM_TOKEN, apm)
.contentType(APPLICATION_JSON)
.body(parametersBucketDTO)
.pathParam("productName", "new-product")
Expand All @@ -118,6 +205,7 @@ void shouldCreateNewParameter() {
.statusCode(Response.Status.NO_CONTENT.getStatusCode());
Map<String, String> applicationParameters = given()
.auth().oauth2(getKeycloakClientToken("testClient"))
.header(HEADER_APM_TOKEN, apm)
.when()
.pathParam("productName", "new-product")
.pathParam("appId", "new-application")
Expand All @@ -133,6 +221,13 @@ void shouldCreateNewParameter() {
@Test
@WithDBData(value = { "data/parameters-testdata.xml" }, deleteBeforeInsert = true, rinseAndRepeat = true)
void shouldUpdateParameters() {
var apm = createToken("org1");
addExpectation(mockServerClient
.when(request().withPath("/v1/tenant").withMethod(HttpMethod.GET).withHeader("apm-principal-token", apm))
.respond(httpRequest -> response().withStatusCode(Response.Status.OK.getStatusCode())
.withContentType(MediaType.APPLICATION_JSON)
.withBody(JsonBody.json(new TenantId().tenantId("tenant-100")))));

ParametersBucketDTOV1 parametersBucketDTO = new ParametersBucketDTOV1();
ParameterInfoDTOV1 parameterInfoDTO1 = new ParameterInfoDTOV1();
parameterInfoDTO1.setCount(2L);
Expand All @@ -142,6 +237,7 @@ void shouldUpdateParameters() {
parametersBucketDTO.getParameters().put("COUNTER", parameterInfoDTO1);
given()
.auth().oauth2(getKeycloakClientToken("testClient"))
.header(HEADER_APM_TOKEN, apm)
.contentType(APPLICATION_JSON)
.body(parametersBucketDTO)
.pathParam("productName", "access-mgmt-product")
Expand All @@ -153,8 +249,17 @@ void shouldUpdateParameters() {

@Test
void bucketRequestEmptyDTO() {

var apm = createToken("org1");
addExpectation(mockServerClient
.when(request().withPath("/v1/tenant").withMethod(HttpMethod.GET).withHeader("apm-principal-token", apm))
.respond(httpRequest -> response().withStatusCode(Response.Status.OK.getStatusCode())
.withContentType(MediaType.APPLICATION_JSON)
.withBody(JsonBody.json(new TenantId().tenantId("tenant-100")))));

given()
.auth().oauth2(getKeycloakClientToken("testClient"))
.header(HEADER_APM_TOKEN, apm)
.contentType(APPLICATION_JSON)
.pathParam("productName", "test")
.pathParam("appId", "test")
Expand All @@ -165,9 +270,18 @@ void bucketRequestEmptyDTO() {

@Test
void bucketRequestNoParametersDTO() {

var apm = createToken("org1");
addExpectation(mockServerClient
.when(request().withPath("/v1/tenant").withMethod(HttpMethod.GET).withHeader("apm-principal-token", apm))
.respond(httpRequest -> response().withStatusCode(Response.Status.OK.getStatusCode())
.withContentType(MediaType.APPLICATION_JSON)
.withBody(JsonBody.json(new TenantId().tenantId("tenant-100")))));

ParametersBucketDTOV1 parametersBucketDTO = new ParametersBucketDTOV1();
given()
.auth().oauth2(getKeycloakClientToken("testClient"))
.header(HEADER_APM_TOKEN, apm)
.contentType(APPLICATION_JSON)
.body(parametersBucketDTO)
.pathParam("productName", "test")
Expand All @@ -179,10 +293,18 @@ void bucketRequestNoParametersDTO() {

@Test
void bucketRequestNullParametersDTO() {
var apm = createToken("org1");
addExpectation(mockServerClient
.when(request().withPath("/v1/tenant").withMethod(HttpMethod.GET).withHeader("apm-principal-token", apm))
.respond(httpRequest -> response().withStatusCode(Response.Status.OK.getStatusCode())
.withContentType(MediaType.APPLICATION_JSON)
.withBody(JsonBody.json(new TenantId().tenantId("tenant-100")))));

ParametersBucketDTOV1 parametersBucketDTO = new ParametersBucketDTOV1();
parametersBucketDTO.setParameters(null);
given()
.auth().oauth2(getKeycloakClientToken("testClient"))
.header(HEADER_APM_TOKEN, apm)
.contentType(APPLICATION_JSON)
.body(parametersBucketDTO)
.pathParam("productName", "test")
Expand Down
Loading

0 comments on commit 27333b4

Please sign in to comment.