Skip to content

Commit

Permalink
Merge pull request #12 from AleksandraKrasteva/dev
Browse files Browse the repository at this point in the history
Merge integration test
  • Loading branch information
AleksandraKrasteva authored Apr 7, 2024
2 parents 8ce0466 + 393daf4 commit e4acc92
Show file tree
Hide file tree
Showing 4 changed files with 99 additions and 145 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -14,15 +14,10 @@


@AllArgsConstructor
//@EnableRabbit
@Component
//@Service
//@AllArgsConstructor
public class RabbitMQReceiver {

private final PostService postService;
// @RabbitHandler

@RabbitListener(queues = "delete-profile-posts")
public void receiveMessage(long userId) {
System.out.println("Received delete posts for user: " + userId);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,138 +1,97 @@
//package com.skipass.postmanagement.integration;
//
//import com.skipass.postmanagement.PostManagementApplication;
//import com.skipass.postmanagement.messaging.RabbitMQProducer;
//import com.skipass.postmanagement.persistance.PostEntity;
//import com.skipass.postmanagement.persistance.PostRepository;
//import org.junit.Rule;
//import org.junit.jupiter.api.BeforeAll;
//import org.junit.jupiter.api.BeforeEach;
//import org.junit.jupiter.api.Test;
//import org.junit.runner.RunWith;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.boot.test.context.SpringBootTest;
//import org.springframework.core.io.ClassPathResource;
//import org.springframework.test.context.DynamicPropertyRegistry;
//import org.springframework.test.context.DynamicPropertySource;
//import org.springframework.test.context.event.annotation.BeforeTestExecution;
//import org.springframework.test.context.junit4.SpringRunner;
//import org.springframework.test.web.reactive.server.WebTestClient;
//import org.springframework.web.client.RestTemplate;
//import org.testcontainers.containers.GenericContainer;
//import org.testcontainers.containers.Network;
//import org.testcontainers.containers.PostgreSQLContainer;
//import org.testcontainers.containers.RabbitMQContainer;
//import org.testcontainers.containers.wait.strategy.Wait;
//import org.testcontainers.images.builder.ImageFromDockerfile;
//import org.testcontainers.junit.jupiter.Container;
//import org.testcontainers.junit.jupiter.Testcontainers;
//import org.testcontainers.shaded.com.google.common.graph.NetworkBuilder;
//import org.testcontainers.utility.DockerImageName;
//import java.nio.file.Paths;
//import java.util.List;
//
//import static org.junit.jupiter.api.Assertions.assertEquals;
//import static org.mockito.Mockito.times;
//import static org.mockito.Mockito.verify;
//
//@Testcontainers
//
//@RunWith(SpringRunner.class)
//@SpringBootTest(
// webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
// classes = PostManagementApplication.class)
//public class UserServiceIntegration {
//
// @Autowired
// private PostRepository repository;
// private static final String rabbitMQImage = "rabbitmq:management";
// public static Network network = Network.newNetwork();
// static RabbitMQContainer rabbit = new RabbitMQContainer(DockerImageName.parse(rabbitMQImage));
// static PostgreSQLContainer userPostgres = new PostgreSQLContainer(DockerImageName.parse("postgres"));
//
// static PostgreSQLContainer postsPostgres = new PostgreSQLContainer(DockerImageName.parse("postgres")).withDatabaseName("posts");
//
// static GenericContainer userService = new GenericContainer((DockerImageName.parse("ghcr.io/aleksandrakrasteva/user-service:main")));
//
// @DynamicPropertySource
// static void registerProperties(DynamicPropertyRegistry registry) {
// registry.add("spring.rabbitmq.host", rabbit::getHost);
// registry.add("spring.rabbitmq.port", rabbit::getAmqpPort);
// registry.add("spring.rabbitmq.username", rabbit::getAdminUsername);
// registry.add("spring.rabbitmq.password", rabbit::getAdminPassword);
// registry.add("spring.datasource.url", postsPostgres::getJdbcUrl);
// registry.add("spring.datasource.username", postsPostgres::getUsername);
// registry.add("spring.datasource.password", postsPostgres::getPassword);
// }
//
//
//// GenericContainer userService = new GenericContainer(
//// new ImageFromDockerfile()
//// .withFileFromPath("Dockerfile", Paths.get("../../../../../../../../UserService/Dockerfile")))
//// .withEnv("spring.rabbitmq.host", rabbit.getHost())
//// .withEnv("spring.rabbitmq.port", String.valueOf(rabbit.getAmqpPort()))
//// .withEnv("spring.rabbitmq.username", rabbit.getAdminUsername())
//// .withEnv("spring.rabbitmq.password", rabbit.getAdminPassword())
//// .withEnv("spring.datasource.url", postgres.getJdbcUrl())
//// .withEnv("spring.datasource.username", postgres.getUsername())
//// .withEnv("spring.datasource.password", postgres.getPassword());
//
//// new GenericContainer((DockerImageName.parse("ghcr.io/aleksandrakrasteva/user-service:main")))
//
//
// @BeforeAll
// private static void setUserService (){
// postsPostgres.withNetwork(network).start();
// userPostgres.withNetwork(network).start();
// rabbit.withNetwork(network).start();
//
// userService.withEnv("spring.rabbitmq.host", rabbit.getContainerName().substring(1))
//// .withEnv("spring.rabbitmq.port", String.valueOf(5672))
// .withEnv("spring.rabbitmq.username", rabbit.getAdminUsername())
// .withEnv("spring.rabbitmq.password", rabbit.getAdminPassword())
// .withEnv("spring.datasource.url", "jdbc:postgresql:/"+ userPostgres.getContainerName() + ":5432/test?loggerLevel=OFF")
// .withEnv("spring.datasource.username", userPostgres.getUsername())
// .withEnv("spring.datasource.password", userPostgres.getPassword())
// .withExposedPorts(8080)
// .withNetwork(network)
// .start();
// }
//
// @BeforeEach
// void insertTestData() {
// PostEntity post = PostEntity.builder().text("").userId(1l).build();
// repository.save(post);
// }
//
// @Test
// void connectToStuff() throws InterruptedException {
// List<PostEntity> before = repository.getPostEntitiesByUserIdIs(1);
//
// RestTemplate restTemplate = new RestTemplate();
//
// String deleteUserProfileEndpoint = "http://"
// + userService.getHost()
// + ":" + userService.getMappedPort(8080)
// + "/delete/1";
//
// System.out.println(deleteUserProfileEndpoint);
//
// restTemplate.delete(deleteUserProfileEndpoint);
// restTemplate.delete(deleteUserProfileEndpoint);
// restTemplate.delete(deleteUserProfileEndpoint);
// restTemplate.delete(deleteUserProfileEndpoint);
// restTemplate.delete(deleteUserProfileEndpoint);
//
//
// List<PostEntity> posts = repository.getPostEntitiesByUserIdIs(1);
//
// assertEquals(1, before.size());
//
// assertEquals(0, posts.size());
// }
//
// // user service tries to connect to rabbitmq
// // CachingConnectionFactory : Attempting to connect to: [/wonderful_moore:5672]
// // userService tries to connect to rabbitmq and cant resolve the name
//
//}
package com.skipass.postmanagement.integration;

import com.skipass.postmanagement.PostManagementApplication;
import com.skipass.postmanagement.persistance.PostEntity;
import com.skipass.postmanagement.persistance.PostRepository;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.DynamicPropertyRegistry;
import org.springframework.test.context.DynamicPropertySource;
import org.springframework.web.client.RestTemplate;
import org.testcontainers.containers.*;
import org.testcontainers.junit.jupiter.Testcontainers;
import org.testcontainers.utility.DockerImageName;
import java.util.List;

import static org.junit.jupiter.api.Assertions.assertEquals;

@Testcontainers()
@SpringBootTest(
webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT,
classes = PostManagementApplication.class)
public class UserServiceIntegration {

@Autowired
private PostRepository repository;
public static Network network = Network.newNetwork();

static RabbitMQContainer rabbit = new RabbitMQContainer(DockerImageName
.parse("ghcr.io/aleksandrakrasteva/rabbitmq:main")
.asCompatibleSubstituteFor("rabbitmq"));
static PostgreSQLContainer userPostgres = new PostgreSQLContainer(DockerImageName
.parse("postgres")).withDatabaseName("users");
static PostgreSQLContainer postsPostgres = new PostgreSQLContainer(DockerImageName
.parse("postgres")).withDatabaseName("posts");
static GenericContainer userService = new GenericContainer((DockerImageName
.parse("ghcr.io/aleksandrakrasteva/user-service:main")));


@DynamicPropertySource
static void registerProperties(DynamicPropertyRegistry registry) {
registry.add("spring.rabbitmq.host", rabbit::getHost);
registry.add("spring.rabbitmq.port", rabbit::getAmqpPort);
registry.add("spring.rabbitmq.username", rabbit::getAdminUsername);
registry.add("spring.rabbitmq.password", rabbit::getAdminPassword);
registry.add("spring.datasource.url", postsPostgres::getJdbcUrl);
registry.add("spring.datasource.username", postsPostgres::getUsername);
registry.add("spring.datasource.password", postsPostgres::getPassword);
}

@BeforeAll
public static void setUserService(){
postsPostgres.withNetwork(network).start();
userPostgres.withNetwork(network).start();
rabbit.withNetwork(network).start();
userService
.withEnv("spring.profiles.active", "test")
.withEnv("spring.datasource.url", "jdbc:postgresql:/"+ userPostgres.getContainerName() + ":5432/users")
.withEnv("spring.datasource.username", userPostgres.getUsername())
.withEnv("spring.datasource.password", userPostgres.getPassword())
.withEnv("spring.rabbitmq.host", rabbit.getContainerName().substring(1))
.withEnv("spring.rabbitmq.username", rabbit.getAdminUsername())
.withEnv("spring.rabbitmq.password", rabbit.getAdminPassword())
.withExposedPorts(8080)
.withNetwork(network)
.start();
}

@BeforeEach
void insertTestData() {
PostEntity post = PostEntity.builder().text("").userId(1l).build();
repository.save(post);
}

@Test
void deletePostsForUserRabbitMQListenerTest() throws InterruptedException {
List<PostEntity> before = repository.getPostEntitiesByUserIdIs(1);

RestTemplate restTemplate = new RestTemplate();

String deleteUserProfileEndpoint = "http://"
+ userService.getHost()
+ ":" + userService.getMappedPort(8080)
+ "/delete/1";

restTemplate.delete(deleteUserProfileEndpoint);

List<PostEntity> posts = repository.getPostEntitiesByUserIdIs(1);

assertEquals(1, before.size());

assertEquals(0, posts.size());
}

}
3 changes: 2 additions & 1 deletion PostManagement/src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
spring.jpa.hibernate.ddl-auto=update
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.PostgreSQLDialect
spring.profiles.active=test
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,5 @@ public Binding journeyBinding() {
return BindingBuilder.bind(journeyQueue()).to(deleteProfileExchange());
}


}

0 comments on commit e4acc92

Please sign in to comment.