Skip to content

Commit

Permalink
[Webhook] Write Test for Webhook module (#969)
Browse files Browse the repository at this point in the history
  • Loading branch information
minhtranq-nashtechglobal committed Sep 3, 2024
1 parent 97de4bc commit b5371e5
Show file tree
Hide file tree
Showing 10 changed files with 2,030 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import com.yas.media.config.IntegrationTestConfiguration;
import com.yas.media.config.FilesystemConfig;
import com.yas.media.config.YasConfig;
import com.yas.media.model.Media;
import com.yas.media.repository.FileSystemRepository;
import com.yas.media.repository.MediaRepository;
import com.yas.media.service.MediaService;
import com.yas.media.repository.MediaRepository;
import com.yas.media.viewmodel.MediaPostVm;
import com.yas.media.config.IntegrationTestConfiguration;
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,18 @@
package com.yas.media.controller;

import com.yas.media.model.Media;
import com.yas.media.model.dto.MediaDto;
import com.yas.media.service.MediaService;
import com.yas.media.viewmodel.MediaVm;
import com.yas.media.model.dto.MediaDto;
import com.yas.media.viewmodel.ErrorVm;
import com.yas.media.viewmodel.MediaPostVm;
import com.yas.media.viewmodel.MediaVm;
import com.yas.media.viewmodel.NoFileMediaVm;
import io.swagger.v3.oas.annotations.Hidden;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import jakarta.validation.Valid;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import lombok.RequiredArgsConstructor;
import org.springframework.core.io.InputStreamResource;
import org.springframework.http.HttpHeaders;
Expand Down
5 changes: 5 additions & 0 deletions webhook/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@
<groupId>org.liquibase</groupId>
<artifactId>liquibase-core</artifactId>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
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;
}
}
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");
}
}
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();
}
}
25 changes: 25 additions & 0 deletions webhook/src/it/resources/application.properties
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
16 changes: 16 additions & 0 deletions webhook/src/it/resources/logback-spring.xml
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>

Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
mock-maker-inline

Loading

0 comments on commit b5371e5

Please sign in to comment.