-
Notifications
You must be signed in to change notification settings - Fork 416
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Webhook] Write Test for Webhook module (#969)
- Loading branch information
1 parent
97de4bc
commit b5371e5
Showing
10 changed files
with
2,030 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 2 additions & 4 deletions
6
media/src/main/java/com/yas/media/controller/MediaController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
webhook/src/it/java/com/yas/webhook/config/IntegrationTestConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
package com.yas.webhook.config; | ||
|
||
import dasniko.testcontainers.keycloak.KeycloakContainer; | ||
import org.springframework.boot.test.context.TestConfiguration; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.test.context.DynamicPropertyRegistry; | ||
import org.testcontainers.containers.PostgreSQLContainer; | ||
|
||
@TestConfiguration | ||
public class IntegrationTestConfiguration { | ||
|
||
@Bean(destroyMethod = "stop") | ||
public PostgreSQLContainer<?> postgresContainer() { | ||
return new PostgreSQLContainer<>("postgres:16") | ||
.withReuse(true); | ||
} | ||
|
||
@Bean(destroyMethod = "stop") | ||
public KeycloakContainer keycloakContainer(DynamicPropertyRegistry registry) { | ||
KeycloakContainer keycloak = new KeycloakContainer() | ||
.withRealmImportFiles("/test-realm.json") | ||
.withReuse(true); | ||
|
||
registry.add("spring.security.oauth2.resourceserver.jwt.issuer-uri", | ||
() -> keycloak.getAuthServerUrl() + "/realms/quarkus"); | ||
registry.add("spring.security.oauth2.resourceserver.jwt.jwk-set-uri", | ||
() -> keycloak.getAuthServerUrl() + "/realms/quarkus/protocol/openid-connect/certs"); | ||
return keycloak; | ||
} | ||
} |
48 changes: 48 additions & 0 deletions
48
webhook/src/it/java/com/yas/webhook/controller/AbstractControllerIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package com.yas.webhook.controller; | ||
|
||
import static io.restassured.RestAssured.given; | ||
|
||
import io.restassured.RestAssured; | ||
import io.restassured.builder.RequestSpecBuilder; | ||
import io.restassured.specification.RequestSpecification; | ||
import java.util.Map; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.boot.test.web.server.LocalServerPort; | ||
import org.springframework.http.MediaType; | ||
import org.testcontainers.shaded.com.google.common.net.HttpHeaders; | ||
|
||
public class AbstractControllerIT { | ||
|
||
@Value("${spring.security.oauth2.resourceserver.jwt.issuer-uri}") | ||
protected String authServerUrl; | ||
|
||
@LocalServerPort | ||
private int port; | ||
|
||
protected RequestSpecification getRequestSpecification() { | ||
RestAssured.enableLoggingOfRequestAndResponseIfValidationFails(); | ||
return new RequestSpecBuilder() | ||
.setPort(port) | ||
.addHeader( | ||
HttpHeaders.CONTENT_TYPE, | ||
MediaType.APPLICATION_JSON_VALUE | ||
) | ||
.build(); | ||
} | ||
|
||
protected String getAccessToken(String username, String password) { | ||
return given() | ||
.contentType("application/x-www-form-urlencoded") | ||
.formParams(Map.of( | ||
"username", username, | ||
"password", password, | ||
"scope", "openid", | ||
"grant_type", "password", | ||
"client_id", "quarkus-service", | ||
"client_secret", "secret" | ||
)) | ||
.post(authServerUrl + "/protocol/openid-connect/token") | ||
.then().assertThat().statusCode(200) | ||
.extract().path("access_token"); | ||
} | ||
} |
43 changes: 43 additions & 0 deletions
43
webhook/src/it/java/com/yas/webhook/controller/WebhookControllerIT.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
package com.yas.webhook.controller; | ||
|
||
import static io.restassured.RestAssured.given; | ||
|
||
import com.yas.webhook.config.IntegrationTestConfiguration; | ||
import com.yas.webhook.config.constants.ApiConstant; | ||
import com.yas.webhook.model.viewmodel.webhook.WebhookDetailVm; | ||
import com.yas.webhook.service.WebhookService; | ||
import io.restassured.http.ContentType; | ||
import org.junit.jupiter.api.Test; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.jdbc.AutoConfigureTestDatabase; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.context.annotation.Import; | ||
import org.springframework.context.annotation.PropertySource; | ||
import org.springframework.http.HttpStatus; | ||
|
||
@Import(IntegrationTestConfiguration.class) | ||
@AutoConfigureTestDatabase(replace = AutoConfigureTestDatabase.Replace.NONE) | ||
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
@PropertySource("classpath:application.properties") | ||
public class WebhookControllerIT extends AbstractControllerIT { | ||
|
||
@Autowired | ||
private WebhookService service; | ||
|
||
@Test | ||
public void test_createWebhook_shouldSuccess() { | ||
WebhookDetailVm webhookDetailVm = new WebhookDetailVm(); | ||
given(getRequestSpecification()) | ||
.auth().oauth2(getAccessToken("admin", "admin")) | ||
.contentType(ContentType.JSON) | ||
.body(""" | ||
{"id":1,"payloadUrl":"","secret":"","contentType":"","isActive":true | ||
,"events":[{"id":1,"name":"ON_PRODUCT_UPDATED"}]} | ||
""") | ||
.when() | ||
.post(ApiConstant.WEBHOOK_URL) | ||
.then() | ||
.statusCode(HttpStatus.OK.value()) | ||
.log().ifValidationFails(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
# Setting Spring context path & port | ||
server.servlet.context-path=/webhook | ||
server.port=8092 | ||
|
||
# Setting Spring profile | ||
spring.profiles.active=test | ||
|
||
spring.jpa.hibernate.ddl-auto=update | ||
spring.liquibase.enabled=false | ||
|
||
spring.security.oauth2.resourceserver.jwt.issuer-uri=test | ||
springdoc.oauthflow.authorization-url=test | ||
springdoc.oauthflow.token-url=test | ||
spring.jpa.open-in-view=false | ||
file.directory=images | ||
|
||
spring.kafka.bootstrap-servers=kafka:9092 | ||
spring.kafka.consumer.group-id=webhook | ||
spring.kafka.consumer.key-deserializer=org.apache.kafka.common.serialization.StringDeserializer | ||
spring.kafka.consumer.value-deserializer=org.apache.kafka.common.serialization.ByteArrayDeserializer | ||
|
||
spring.kafka.consumer.properties.spring.json.use.type.headers=false | ||
|
||
webhook.integration.kafka.product.topic-name=dbproduct.public.product | ||
webhook.integration.kafka.order.topic-name=dborder.public.order |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<configuration> | ||
<include resource="org/springframework/boot/logging/logback/base.xml" /> | ||
<springProperty scope="context" name="appName" source="spring.application.name"/> | ||
|
||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender"> | ||
<encoder> | ||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n</pattern> | ||
</encoder> | ||
</appender> | ||
|
||
<root level="INFO"> | ||
<appender-ref ref="STDOUT"/> | ||
</root> | ||
</configuration> | ||
|
2 changes: 2 additions & 0 deletions
2
webhook/src/it/resources/mockito-extensions/org.mockito.plugins.MockMaker
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
mock-maker-inline | ||
|
Oops, something went wrong.