From b1a8fb54bb847ad2b3e215c82ce709e0a48a4458 Mon Sep 17 00:00:00 2001 From: ruslanTash <119006686+ruslanTash@users.noreply.github.com> Date: Mon, 14 Aug 2023 01:21:10 +0400 Subject: [PATCH 01/17] 1 --- pom.xml | 6 + .../SimpleBankingApplicationTests.java | 31 +++- .../controller/UserControllerTest.java | 159 ++++++++++++++++++ .../java/resourses/application.properties | 0 4 files changed, 195 insertions(+), 1 deletion(-) create mode 100644 src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java create mode 100644 src/test/java/resourses/application.properties diff --git a/pom.xml b/pom.xml index 3d8377c..820c17f 100644 --- a/pom.xml +++ b/pom.xml @@ -54,6 +54,12 @@ postgresql test + + org.springframework.security + spring-security-test + 6.1.0 + test + diff --git a/src/test/java/com/skypro/simplebanking/SimpleBankingApplicationTests.java b/src/test/java/com/skypro/simplebanking/SimpleBankingApplicationTests.java index 1d4dd66..8518939 100644 --- a/src/test/java/com/skypro/simplebanking/SimpleBankingApplicationTests.java +++ b/src/test/java/com/skypro/simplebanking/SimpleBankingApplicationTests.java @@ -1,9 +1,38 @@ package com.skypro.simplebanking; 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.testcontainers.containers.PostgreSQLContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; + +import javax.sql.DataSource; + @SpringBootTest +@Testcontainers class SimpleBankingApplicationTests { -} + @Container + private static final PostgreSQLContainer postgres = new PostgreSQLContainer<>("postgres:13") + .withUsername("postgres") + .withPassword("postgres"); + + @DynamicPropertySource + static void postgresProperties(DynamicPropertyRegistry registry) { + registry.add("spring.datasource.url", postgres::getJdbcUrl); + registry.add("spring.datasource.username", postgres::getUsername); + registry.add("spring.datasource.password", postgres::getPassword); + } + + @Autowired + private DataSource dataSource; + + @Test + void contextLoads() { + } + +} \ No newline at end of file diff --git a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java new file mode 100644 index 0000000..8aa5037 --- /dev/null +++ b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java @@ -0,0 +1,159 @@ +package com.skypro.simplebanking.controller; + +import com.skypro.simplebanking.dto.AccountDTO; +import com.skypro.simplebanking.dto.UserDTO; +import com.skypro.simplebanking.entity.Account; +import com.skypro.simplebanking.entity.AccountCurrency; +import com.skypro.simplebanking.entity.User; +import com.skypro.simplebanking.repository.AccountRepository; +import com.skypro.simplebanking.repository.UserRepository; +import com.skypro.simplebanking.service.UserService; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.security.crypto.password.PasswordEncoder; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; +import org.springframework.test.web.servlet.MockMvc; +import org.testcontainers.containers.PostgreSQLContainer; +import org.testcontainers.junit.jupiter.Container; +import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper; + +import javax.sql.DataSource; +import java.sql.Connection; +import java.sql.SQLException; +import java.util.ArrayList; +import java.util.List; + +import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + + +@SpringBootTest +@AutoConfigureMockMvc +@Testcontainers +public class UserControllerTest { + + @Autowired + MockMvc mockMvc; + + @Autowired + private UserRepository userRepository; + @Autowired + private UserService userService; + @Autowired + private AccountRepository accountRepository; + + private PasswordEncoder passwordEncoder; + + @Container + private static final PostgreSQLContainer postgres = new PostgreSQLContainer<>("postgres:latest") + .withUsername("postgres") + .withPassword("8258"); + + @DynamicPropertySource + static void postgresProperties(DynamicPropertyRegistry registry) { + registry.add("spring.datasource.url", postgres::getJdbcUrl); + registry.add("spring.datasource.username", postgres::getUsername); + registry.add("spring.datasource.password", postgres::getPassword); + } + + @Autowired + private DataSource dataSource; + + @Test + void testPostgresql() throws SQLException { + try (Connection conn = dataSource.getConnection()) { + assertThat(conn).isNotNull(); + } + } + + @AfterEach + void cleanRepository() { + accountRepository.deleteAll(); + userRepository.deleteAll(); + } + + @BeforeEach + void createRepository() { + User user1 = new User(); + user1.setUsername("username1"); + user1.setPassword(passwordEncoder.encode("password1")); + User user2 = new User(); + user2.setUsername("username2"); + user2.setPassword(passwordEncoder.encode("password2")); + User user3 = new User(); + user3.setUsername("username3"); + user3.setPassword(passwordEncoder.encode("password3")); + User user4 = new User(); + user4.setUsername("username4"); + user4.setPassword(passwordEncoder.encode("password4")); + User user5 = new User(); + user5.setUsername("username5"); + user5.setPassword(passwordEncoder.encode("password5")); + + Account account1 = new Account(); + account1.setId(1L); + account1.setAccountCurrency(AccountCurrency.RUB); + account1.setAmount(10000L); + account1.setUser(user1); + Account account2 = new Account(); + account2.setId(2L); + account2.setAccountCurrency(AccountCurrency.EUR); + account2.setAmount(1000L); + account2.setUser(user2); + Account account3 = new Account(); + account3.setId(3L); + account3.setAccountCurrency(AccountCurrency.USD); + account3.setAmount(2000L); + account3.setUser(user3); + Account account4 = new Account(); + account4.setId(4L); + account4.setAccountCurrency(AccountCurrency.EUR); + account4.setAmount(4000L); + account4.setUser(user4); + Account account5 = new Account(); + account4.setId(5L); + account4.setAccountCurrency(AccountCurrency.RUB); + account4.setAmount(50000L); + account4.setUser(user5); + + List users = new ArrayList<>(); + users.add(user1); + users.add(user2); + users.add(user3); + users.add(user4); + users.add(user5); + + List accounts = new ArrayList<>(); + accounts.add(account1); + accounts.add(account2); + accounts.add(account3); + accounts.add(account4); + accounts.add(account5); + + accountRepository.saveAll(accounts); + userRepository.saveAll(users); + } + + + + @Test + @WithMockUser(username = "user1", roles = "USER", password = "password1") + void givenNoBody_whenEmptyJsonArray() throws Exception { + mockMvc.perform(get("/user/list")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$").isArray()) + .andExpect(jsonPath("$").isNotEmpty()); + } + + + +} \ No newline at end of file diff --git a/src/test/java/resourses/application.properties b/src/test/java/resourses/application.properties new file mode 100644 index 0000000..e69de29 From 2c6ecad9745d6d956283636035f6269c3098ab27 Mon Sep 17 00:00:00 2001 From: ruslanTash <119006686+ruslanTash@users.noreply.github.com> Date: Mon, 14 Aug 2023 18:07:08 +0400 Subject: [PATCH 02/17] 1 --- pom.xml | 9 +++ .../controller/UserControllerTest.java | 57 ++++++++----------- 2 files changed, 33 insertions(+), 33 deletions(-) diff --git a/pom.xml b/pom.xml index 820c17f..e61a8bb 100644 --- a/pom.xml +++ b/pom.xml @@ -47,11 +47,13 @@ org.testcontainers junit-jupiter + 1.18.3 test org.testcontainers postgresql + 1.18.3 test @@ -60,6 +62,13 @@ 6.1.0 test + + org.projectlombok + lombok + 1.18.26 + + + diff --git a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java index 8aa5037..d3962f3 100644 --- a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java +++ b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java @@ -37,26 +37,30 @@ @SpringBootTest -@AutoConfigureMockMvc +//@AutoConfigureMockMvc @Testcontainers + public class UserControllerTest { @Autowired MockMvc mockMvc; - +// @Autowired private UserRepository userRepository; +// @Autowired +// private UserService userService; +// @Autowired +// private AccountRepository accountRepository; +// +// @Autowired +// private PasswordEncoder passwordEncoder; @Autowired - private UserService userService; - @Autowired - private AccountRepository accountRepository; - - private PasswordEncoder passwordEncoder; + private DataSource dataSource; @Container private static final PostgreSQLContainer postgres = new PostgreSQLContainer<>("postgres:latest") .withUsername("postgres") - .withPassword("8258"); + .withPassword("postgres"); @DynamicPropertySource static void postgresProperties(DynamicPropertyRegistry registry) { @@ -65,8 +69,7 @@ static void postgresProperties(DynamicPropertyRegistry registry) { registry.add("spring.datasource.password", postgres::getPassword); } - @Autowired - private DataSource dataSource; + @Test void testPostgresql() throws SQLException { @@ -77,7 +80,6 @@ void testPostgresql() throws SQLException { @AfterEach void cleanRepository() { - accountRepository.deleteAll(); userRepository.deleteAll(); } @@ -85,19 +87,19 @@ void cleanRepository() { void createRepository() { User user1 = new User(); user1.setUsername("username1"); - user1.setPassword(passwordEncoder.encode("password1")); + user1.setPassword("password1"); User user2 = new User(); user2.setUsername("username2"); - user2.setPassword(passwordEncoder.encode("password2")); + user2.setPassword("password2"); User user3 = new User(); user3.setUsername("username3"); - user3.setPassword(passwordEncoder.encode("password3")); + user3.setPassword("password3"); User user4 = new User(); user4.setUsername("username4"); - user4.setPassword(passwordEncoder.encode("password4")); + user4.setPassword("password4"); User user5 = new User(); user5.setUsername("username5"); - user5.setPassword(passwordEncoder.encode("password5")); + user5.setPassword("password5"); Account account1 = new Account(); account1.setId(1L); @@ -125,30 +127,19 @@ void createRepository() { account4.setAmount(50000L); account4.setUser(user5); - List users = new ArrayList<>(); - users.add(user1); - users.add(user2); - users.add(user3); - users.add(user4); - users.add(user5); - - List accounts = new ArrayList<>(); - accounts.add(account1); - accounts.add(account2); - accounts.add(account3); - accounts.add(account4); - accounts.add(account5); - - accountRepository.saveAll(accounts); + List users = List.of(user1, user2, user3, user4, user5); + List accounts = List.of(account1, account2, account3, account4, account5); + + userRepository.saveAll(users); } @Test - @WithMockUser(username = "user1", roles = "USER", password = "password1") + @WithMockUser(roles = "USER") void givenNoBody_whenEmptyJsonArray() throws Exception { - mockMvc.perform(get("/user/list")) + mockMvc.perform(get("/user/list")) .andExpect(status().isOk()) .andExpect(jsonPath("$").isArray()) .andExpect(jsonPath("$").isNotEmpty()); From afb9acf2ccfc548633b5fd892c0ea0a3a37c41a9 Mon Sep 17 00:00:00 2001 From: ruslanTash <119006686+ruslanTash@users.noreply.github.com> Date: Mon, 14 Aug 2023 20:41:42 +0400 Subject: [PATCH 03/17] =?UTF-8?q?=D0=BD=D0=B5=20=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=B5=D1=82=20=D0=B0=D0=BD=D0=BD=D0=BE=D1=82=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20@AutoConfigureMockMvc,=20=D0=BD=D0=B5=20?= =?UTF-8?q?=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D1=91=D1=82=D1=81=D1=8F=20=D0=B1?= =?UTF-8?q?=D0=B8=D0=BD=20MockMvc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/UserControllerTest.java | 76 ++++++++----------- 1 file changed, 33 insertions(+), 43 deletions(-) diff --git a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java index d3962f3..a427095 100644 --- a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java +++ b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java @@ -1,39 +1,30 @@ package com.skypro.simplebanking.controller; -import com.skypro.simplebanking.dto.AccountDTO; -import com.skypro.simplebanking.dto.UserDTO; import com.skypro.simplebanking.entity.Account; import com.skypro.simplebanking.entity.AccountCurrency; import com.skypro.simplebanking.entity.User; import com.skypro.simplebanking.repository.AccountRepository; import com.skypro.simplebanking.repository.UserRepository; -import com.skypro.simplebanking.service.UserService; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.security.crypto.password.PasswordEncoder; -import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.DynamicPropertyRegistry; import org.springframework.test.context.DynamicPropertySource; import org.springframework.test.web.servlet.MockMvc; import org.testcontainers.containers.PostgreSQLContainer; import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; -import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper; import javax.sql.DataSource; import java.sql.Connection; import java.sql.SQLException; -import java.util.ArrayList; import java.util.List; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; -import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; -import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + @SpringBootTest @@ -41,27 +32,14 @@ @Testcontainers public class UserControllerTest { - - @Autowired - MockMvc mockMvc; -// - @Autowired - private UserRepository userRepository; // @Autowired -// private UserService userService; -// @Autowired -// private AccountRepository accountRepository; -// -// @Autowired -// private PasswordEncoder passwordEncoder; +// MockMvc mockMvc; @Autowired private DataSource dataSource; - @Container private static final PostgreSQLContainer postgres = new PostgreSQLContainer<>("postgres:latest") .withUsername("postgres") .withPassword("postgres"); - @DynamicPropertySource static void postgresProperties(DynamicPropertyRegistry registry) { registry.add("spring.datasource.url", postgres::getJdbcUrl); @@ -70,18 +48,12 @@ static void postgresProperties(DynamicPropertyRegistry registry) { } + @Autowired + private UserRepository userRepository; + @Autowired + private AccountRepository accountRepository; - @Test - void testPostgresql() throws SQLException { - try (Connection conn = dataSource.getConnection()) { - assertThat(conn).isNotNull(); - } - } - @AfterEach - void cleanRepository() { - userRepository.deleteAll(); - } @BeforeEach void createRepository() { @@ -130,21 +102,39 @@ void createRepository() { List users = List.of(user1, user2, user3, user4, user5); List accounts = List.of(account1, account2, account3, account4, account5); - userRepository.saveAll(users); } - - + @AfterEach + void cleanRepository() { + userRepository.deleteAll(); + } @Test - @WithMockUser(roles = "USER") - void givenNoBody_whenEmptyJsonArray() throws Exception { - mockMvc.perform(get("/user/list")) - .andExpect(status().isOk()) - .andExpect(jsonPath("$").isArray()) - .andExpect(jsonPath("$").isNotEmpty()); + void testPostgresql() throws SQLException { + try (Connection conn = dataSource.getConnection()) { + assertThat(conn).isNotNull(); + } } +// @Test +// @WithMockUser(roles = "USER") +// void givenUsers() throws Exception { +// mockMvc.perform(get("/user/list")) +// .andExpect(status().isOk()) +// .andExpect(jsonPath("$").isArray()) +// .andExpect(jsonPath("$").isNotEmpty()) +// .andExpect(jsonPath("$.length()").value(3)); +// } +// @Test +// @WithMockUser(roles = "USER") +// void givenNoBody_whenEmptyJsonArray() throws Exception { +// userRepository.deleteAll(); +// mockMvc.perform(get("/user/list")) +// .andExpect(status().isOk()) +// .andExpect(jsonPath("$").isArray()) +// .andExpect(jsonPath("$").isEmpty()); +// } + } \ No newline at end of file From c03306f189f3e82335a7ceac35b97e6a1d1e53b9 Mon Sep 17 00:00:00 2001 From: ruslanTash <119006686+ruslanTash@users.noreply.github.com> Date: Mon, 14 Aug 2023 21:08:51 +0400 Subject: [PATCH 04/17] =?UTF-8?q?=D0=BD=D0=B5=20=D1=80=D0=B0=D0=B1=D0=BE?= =?UTF-8?q?=D1=82=D0=B0=D0=B5=D1=82=20=D0=B0=D0=BD=D0=BD=D0=BE=D1=82=D0=B0?= =?UTF-8?q?=D1=86=D0=B8=D1=8F=20@AutoConfigureMockMvc,=20=D0=BD=D0=B5=20?= =?UTF-8?q?=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D1=91=D1=82=D1=81=D1=8F=20=D0=B1?= =?UTF-8?q?=D0=B8=D0=BD=20MockMvc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../simplebanking/controller/UserControllerTest.java | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java index a427095..6f872ab 100644 --- a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java +++ b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java @@ -28,12 +28,11 @@ @SpringBootTest -//@AutoConfigureMockMvc +@AutoConfigureMockMvc @Testcontainers - public class UserControllerTest { -// @Autowired -// MockMvc mockMvc; + @Autowired + MockMvc mockMvc; @Autowired private DataSource dataSource; @Container From e47984fc7a7fe9eca88643bfee3beb500ee0926b Mon Sep 17 00:00:00 2001 From: ruslanTash <119006686+ruslanTash@users.noreply.github.com> Date: Mon, 14 Aug 2023 23:16:30 +0400 Subject: [PATCH 05/17] =?UTF-8?q?=D1=83=D0=B1=D1=80=D0=B0=D0=BB=20=D0=B2?= =?UTF-8?q?=D0=B5=D1=80=D1=81=D0=B8=D1=8E=20=D0=B8=D0=B7=20=D1=81=D1=82?= =?UTF-8?q?=D0=B0=D1=80=D1=82=D0=B5=D1=80=D0=BE=D0=B2,=20=D1=81=D0=BE?= =?UTF-8?q?=D0=B7=D0=B4=D0=B0=D0=BB=D1=81=D1=8F=20=D0=B1=D0=B8=D0=BD=20Moc?= =?UTF-8?q?kMvc?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 17 +---------------- 1 file changed, 1 insertion(+), 16 deletions(-) diff --git a/pom.xml b/pom.xml index e61a8bb..caa2163 100644 --- a/pom.xml +++ b/pom.xml @@ -47,28 +47,13 @@ org.testcontainers junit-jupiter - 1.18.3 test org.testcontainers postgresql - 1.18.3 test - - org.springframework.security - spring-security-test - 6.1.0 - test - - - org.projectlombok - lombok - 1.18.26 - - - @@ -91,4 +76,4 @@ - + \ No newline at end of file From 3c5eb9e88b837f05ed136c884ced12cdf04d1cf7 Mon Sep 17 00:00:00 2001 From: ruslanTash <119006686+ruslanTash@users.noreply.github.com> Date: Mon, 14 Aug 2023 23:32:13 +0400 Subject: [PATCH 06/17] =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B2=D1=8B=D0=B5=20?= =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=D1=8B=20(=D0=BF=D0=BE=D0=BB=D1=83?= =?UTF-8?q?=D1=87=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=81=D0=BF=D0=B8=D1=81=D0=BA?= =?UTF-8?q?=D0=B0=20=D1=8E=D0=B7=D0=B5=D1=80=D0=BE=D0=B2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 5 ++ .../controller/UserControllerTest.java | 56 +++++++++++-------- 2 files changed, 38 insertions(+), 23 deletions(-) diff --git a/pom.xml b/pom.xml index caa2163..5049a1d 100644 --- a/pom.xml +++ b/pom.xml @@ -54,6 +54,11 @@ postgresql test + + org.springframework.security + spring-security-test + test + diff --git a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java index 6f872ab..9520fcb 100644 --- a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java +++ b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java @@ -11,6 +11,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.DynamicPropertyRegistry; import org.springframework.test.context.DynamicPropertySource; import org.springframework.test.web.servlet.MockMvc; @@ -24,7 +25,9 @@ import java.util.List; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; - +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @SpringBootTest @@ -93,10 +96,10 @@ void createRepository() { account4.setAmount(4000L); account4.setUser(user4); Account account5 = new Account(); - account4.setId(5L); - account4.setAccountCurrency(AccountCurrency.RUB); - account4.setAmount(50000L); - account4.setUser(user5); + account5.setId(5L); + account5.setAccountCurrency(AccountCurrency.RUB); + account5.setAmount(50000L); + account5.setUser(user5); List users = List.of(user1, user2, user3, user4, user5); List accounts = List.of(account1, account2, account3, account4, account5); @@ -115,24 +118,31 @@ void testPostgresql() throws SQLException { } } -// @Test -// @WithMockUser(roles = "USER") -// void givenUsers() throws Exception { -// mockMvc.perform(get("/user/list")) -// .andExpect(status().isOk()) -// .andExpect(jsonPath("$").isArray()) -// .andExpect(jsonPath("$").isNotEmpty()) -// .andExpect(jsonPath("$.length()").value(3)); -// } -// @Test -// @WithMockUser(roles = "USER") -// void givenNoBody_whenEmptyJsonArray() throws Exception { -// userRepository.deleteAll(); -// mockMvc.perform(get("/user/list")) -// .andExpect(status().isOk()) -// .andExpect(jsonPath("$").isArray()) -// .andExpect(jsonPath("$").isEmpty()); -// } + @Test + @WithMockUser(roles = "USER") + void givenUsers() throws Exception { + mockMvc.perform(get("/user/list")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$").isArray()) + .andExpect(jsonPath("$").isNotEmpty()) + .andExpect(jsonPath("$.length()").value(5)); + } + @Test + @WithMockUser(roles = "USER") + void givenNoBody_whenEmptyJsonArray() throws Exception { + userRepository.deleteAll(); + mockMvc.perform(get("/user/list")) + .andExpect(status().isOk()) + .andExpect(jsonPath("$").isArray()) + .andExpect(jsonPath("$").isEmpty()); + } + + @Test + @WithMockUser(roles = "ADMIN") + void givenUsers_AdminNoAccess_Error403() throws Exception { + mockMvc.perform(get("/user/list")) + .andExpect(status().is4xxClientError()); + } From 187fc0164d7acad542dec75a9907228a7883495f Mon Sep 17 00:00:00 2001 From: ruslanTash <119006686+ruslanTash@users.noreply.github.com> Date: Mon, 14 Aug 2023 23:57:48 +0400 Subject: [PATCH 07/17] =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B2=D1=8B=D0=B5=20?= =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=D1=8B=20(=D0=BF=D0=BE=D0=BB=D1=83?= =?UTF-8?q?=D1=87=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=81=D0=BF=D0=B8=D1=81=D0=BA?= =?UTF-8?q?=D0=B0=20=D1=8E=D0=B7=D0=B5=D1=80=D0=BE=D0=B2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/UserControllerTest.java | 21 +++++++++++++++++-- 1 file changed, 19 insertions(+), 2 deletions(-) diff --git a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java index 9520fcb..fca6e11 100644 --- a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java +++ b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java @@ -5,6 +5,7 @@ import com.skypro.simplebanking.entity.User; import com.skypro.simplebanking.repository.AccountRepository; import com.skypro.simplebanking.repository.UserRepository; +import net.minidev.json.JSONObject; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -18,6 +19,7 @@ import org.testcontainers.containers.PostgreSQLContainer; import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.shaded.com.github.dockerjava.core.MediaType; import javax.sql.DataSource; import java.sql.Connection; @@ -118,9 +120,10 @@ void testPostgresql() throws SQLException { } } +// Проверка получения списка юзеров ("/user/list") @Test @WithMockUser(roles = "USER") - void givenUsers() throws Exception { + void givenUsers_OK() throws Exception { mockMvc.perform(get("/user/list")) .andExpect(status().isOk()) .andExpect(jsonPath("$").isArray()) @@ -136,7 +139,6 @@ void givenNoBody_whenEmptyJsonArray() throws Exception { .andExpect(jsonPath("$").isArray()) .andExpect(jsonPath("$").isEmpty()); } - @Test @WithMockUser(roles = "ADMIN") void givenUsers_AdminNoAccess_Error403() throws Exception { @@ -144,6 +146,21 @@ void givenUsers_AdminNoAccess_Error403() throws Exception { .andExpect(status().is4xxClientError()); } +// Проверка транзакции ("/transfer") +// @Test +// @WithMockUser(roles = "USER") +// void getTranzaction() throws Exception { +// JSONObject jsonObject = new JSONObject(); +// jsonObject.put("fromAccountId", "2"); +// jsonObject.put("toUserId", "4"); +// jsonObject.put("toAccountId", "4"); +// jsonObject.put("amount", "5000"); +// +// mockMvc.perform(post("/transfer")) +// .contentType(MediaType.APPLICATION_JSON) +// .content(jsonObject.toString())) +//.andExpect(status().isOk()); +// } } \ No newline at end of file From 2be2b5edeebb993560edcde940b05bdd1578b2b2 Mon Sep 17 00:00:00 2001 From: ruslanTash <119006686+ruslanTash@users.noreply.github.com> Date: Wed, 16 Aug 2023 00:19:26 +0400 Subject: [PATCH 08/17] =?UTF-8?q?=D0=BF=D0=B5=D1=80=D0=B2=D1=8B=D0=B5=20?= =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=D1=8B=20(=D0=BF=D0=BE=D0=BB=D1=83?= =?UTF-8?q?=D1=87=D0=B5=D0=BD=D0=B8=D0=B5=20=D1=81=D0=BF=D0=B8=D1=81=D0=BA?= =?UTF-8?q?=D0=B0=20=D1=8E=D0=B7=D0=B5=D1=80=D0=BE=D0=B2)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/UserControllerTest.java | 117 ++++++++++-------- 1 file changed, 65 insertions(+), 52 deletions(-) diff --git a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java index fca6e11..c5b9834 100644 --- a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java +++ b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java @@ -1,17 +1,19 @@ package com.skypro.simplebanking.controller; +import com.skypro.simplebanking.dto.BankingUserDetails; +import com.skypro.simplebanking.dto.TransferRequest; import com.skypro.simplebanking.entity.Account; import com.skypro.simplebanking.entity.AccountCurrency; import com.skypro.simplebanking.entity.User; import com.skypro.simplebanking.repository.AccountRepository; import com.skypro.simplebanking.repository.UserRepository; -import net.minidev.json.JSONObject; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.DynamicPropertyRegistry; import org.springframework.test.context.DynamicPropertySource; @@ -19,12 +21,14 @@ import org.testcontainers.containers.PostgreSQLContainer; import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; -import org.testcontainers.shaded.com.github.dockerjava.core.MediaType; +import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper; import javax.sql.DataSource; +import javax.validation.Valid; import java.sql.Connection; import java.sql.SQLException; import java.util.List; +import java.util.Optional; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; @@ -44,6 +48,7 @@ public class UserControllerTest { private static final PostgreSQLContainer postgres = new PostgreSQLContainer<>("postgres:latest") .withUsername("postgres") .withPassword("postgres"); + @DynamicPropertySource static void postgresProperties(DynamicPropertyRegistry registry) { registry.add("spring.datasource.url", postgres::getJdbcUrl); @@ -58,56 +63,56 @@ static void postgresProperties(DynamicPropertyRegistry registry) { private AccountRepository accountRepository; - @BeforeEach void createRepository() { User user1 = new User(); - user1.setUsername("username1"); - user1.setPassword("password1"); + user1.setUsername("username1"); + user1.setPassword("password1"); User user2 = new User(); - user2.setUsername("username2"); - user2.setPassword("password2"); + user2.setUsername("username2"); + user2.setPassword("password2"); User user3 = new User(); - user3.setUsername("username3"); - user3.setPassword("password3"); + user3.setUsername("username3"); + user3.setPassword("password3"); User user4 = new User(); - user4.setUsername("username4"); - user4.setPassword("password4"); + user4.setUsername("username4"); + user4.setPassword("password4"); User user5 = new User(); - user5.setUsername("username5"); - user5.setPassword("password5"); + user5.setUsername("username5"); + user5.setPassword("password5"); Account account1 = new Account(); - account1.setId(1L); - account1.setAccountCurrency(AccountCurrency.RUB); - account1.setAmount(10000L); - account1.setUser(user1); + account1.setId(1L); + account1.setAccountCurrency(AccountCurrency.RUB); + account1.setAmount(10000L); + account1.setUser(user1); Account account2 = new Account(); - account2.setId(2L); - account2.setAccountCurrency(AccountCurrency.EUR); - account2.setAmount(1000L); - account2.setUser(user2); + account2.setId(2L); + account2.setAccountCurrency(AccountCurrency.EUR); + account2.setAmount(1000L); + account2.setUser(user2); Account account3 = new Account(); - account3.setId(3L); - account3.setAccountCurrency(AccountCurrency.USD); - account3.setAmount(2000L); - account3.setUser(user3); + account3.setId(3L); + account3.setAccountCurrency(AccountCurrency.USD); + account3.setAmount(2000L); + account3.setUser(user3); Account account4 = new Account(); - account4.setId(4L); - account4.setAccountCurrency(AccountCurrency.EUR); - account4.setAmount(4000L); - account4.setUser(user4); + account4.setId(4L); + account4.setAccountCurrency(AccountCurrency.EUR); + account4.setAmount(4000L); + account4.setUser(user4); Account account5 = new Account(); - account5.setId(5L); - account5.setAccountCurrency(AccountCurrency.RUB); - account5.setAmount(50000L); - account5.setUser(user5); + account5.setId(5L); + account5.setAccountCurrency(AccountCurrency.RUB); + account5.setAmount(50000L); + account5.setUser(user5); List users = List.of(user1, user2, user3, user4, user5); List accounts = List.of(account1, account2, account3, account4, account5); userRepository.saveAll(users); } + @AfterEach void cleanRepository() { userRepository.deleteAll(); @@ -120,25 +125,27 @@ void testPostgresql() throws SQLException { } } -// Проверка получения списка юзеров ("/user/list") + // Проверка получения списка юзеров ("/user/list") @Test @WithMockUser(roles = "USER") void givenUsers_OK() throws Exception { - mockMvc.perform(get("/user/list")) + mockMvc.perform(get("/user/list")) .andExpect(status().isOk()) .andExpect(jsonPath("$").isArray()) .andExpect(jsonPath("$").isNotEmpty()) .andExpect(jsonPath("$.length()").value(5)); } + @Test @WithMockUser(roles = "USER") void givenNoBody_whenEmptyJsonArray() throws Exception { - userRepository.deleteAll(); - mockMvc.perform(get("/user/list")) + userRepository.deleteAll(); + mockMvc.perform(get("/user/list")) .andExpect(status().isOk()) .andExpect(jsonPath("$").isArray()) .andExpect(jsonPath("$").isEmpty()); } + @Test @WithMockUser(roles = "ADMIN") void givenUsers_AdminNoAccess_Error403() throws Exception { @@ -146,21 +153,27 @@ void givenUsers_AdminNoAccess_Error403() throws Exception { .andExpect(status().is4xxClientError()); } -// Проверка транзакции ("/transfer") -// @Test -// @WithMockUser(roles = "USER") -// void getTranzaction() throws Exception { -// JSONObject jsonObject = new JSONObject(); -// jsonObject.put("fromAccountId", "2"); -// jsonObject.put("toUserId", "4"); -// jsonObject.put("toAccountId", "4"); -// jsonObject.put("amount", "5000"); -// -// mockMvc.perform(post("/transfer")) -// .contentType(MediaType.APPLICATION_JSON) -// .content(jsonObject.toString())) -//.andExpect(status().isOk()); -// } + // Проверка транзакции ("/transfer") + + + @Test + @WithMockUser(roles = "USER", username = "username2") + void getTranzaction() throws Exception { +// Нужно передать аутенфикацию + TransferRequest transferRequest = new TransferRequest(); + transferRequest.setFromAccountId(2); + transferRequest.setToUserId(4); + transferRequest.setToAccountId(4); + transferRequest.setAmount(5000); + + ObjectMapper mapper = new ObjectMapper(); + String jsonString = mapper.writeValueAsString(transferRequest); + + mockMvc.perform(post("/transfer") + .content(jsonString)) + .andExpect(status().isOk()); + + } } \ No newline at end of file From afdd96be472b80a8c68904c0a2377f58f75b0333 Mon Sep 17 00:00:00 2001 From: ruslanTash <119006686+ruslanTash@users.noreply.github.com> Date: Wed, 16 Aug 2023 00:20:24 +0400 Subject: [PATCH 09/17] =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=20getTranzaction?= =?UTF-8?q?()=20=D0=BF=D0=B0=D0=B4=D0=B0=D0=B5=D1=82=20=D1=81=D0=BE=20?= =?UTF-8?q?=D1=81=D1=82=D0=B0=D1=82=D1=83=D1=81=D0=BE=D0=BC=20415.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/skypro/simplebanking/controller/UserControllerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java index c5b9834..ff84e66 100644 --- a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java +++ b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java @@ -160,7 +160,7 @@ void givenUsers_AdminNoAccess_Error403() throws Exception { @Test @WithMockUser(roles = "USER", username = "username2") void getTranzaction() throws Exception { -// Нужно передать аутенфикацию + // Нужно передать аутенфикацию TransferRequest transferRequest = new TransferRequest(); transferRequest.setFromAccountId(2); transferRequest.setToUserId(4); From 300e12538791daf301b4783edf88da671732df93 Mon Sep 17 00:00:00 2001 From: ruslanTash <119006686+ruslanTash@users.noreply.github.com> Date: Wed, 16 Aug 2023 15:40:02 +0400 Subject: [PATCH 10/17] =?UTF-8?q?=D0=BF=D1=80=D0=BE=D1=82=D0=B5=D1=81?= =?UTF-8?q?=D1=82=D0=B8=D1=80=D0=BE=D0=B2=D0=B0=D0=BD=20=D0=BC=D0=B5=D1=82?= =?UTF-8?q?=D0=BE=D0=B4=20=D1=81=D0=BE=D0=B7=D0=B4=D0=B0=D0=BD=D0=B8=D1=8F?= =?UTF-8?q?=20=D1=8E=D0=B7=D0=B5=D1=80=D0=BE=D0=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/UserControllerTest.java | 80 +++++++++++++++---- 1 file changed, 63 insertions(+), 17 deletions(-) diff --git a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java index ff84e66..a75da96 100644 --- a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java +++ b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java @@ -1,12 +1,14 @@ package com.skypro.simplebanking.controller; +import com.skypro.simplebanking.configuration.AdminSecurityFilter; +import com.skypro.simplebanking.dto.*; import com.skypro.simplebanking.dto.BankingUserDetails; -import com.skypro.simplebanking.dto.TransferRequest; import com.skypro.simplebanking.entity.Account; import com.skypro.simplebanking.entity.AccountCurrency; import com.skypro.simplebanking.entity.User; import com.skypro.simplebanking.repository.AccountRepository; import com.skypro.simplebanking.repository.UserRepository; +import net.minidev.json.JSONObject; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -14,13 +16,19 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; +import org.springframework.security.core.userdetails.UserDetails; import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.security.web.SecurityFilterChain; import org.springframework.test.context.DynamicPropertyRegistry; import org.springframework.test.context.DynamicPropertySource; import org.springframework.test.web.servlet.MockMvc; +import org.springframework.test.web.servlet.request.RequestPostProcessor; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.testcontainers.containers.PostgreSQLContainer; import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; +import org.testcontainers.shaded.com.fasterxml.jackson.databind.JavaType; import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper; import javax.sql.DataSource; @@ -31,6 +39,8 @@ import java.util.Optional; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; +import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; @@ -153,27 +163,63 @@ void givenUsers_AdminNoAccess_Error403() throws Exception { .andExpect(status().is4xxClientError()); } - // Проверка транзакции ("/transfer") - - - @Test - @WithMockUser(roles = "USER", username = "username2") + @WithMockUser(roles = "USER") void getTranzaction() throws Exception { - // Нужно передать аутенфикацию - TransferRequest transferRequest = new TransferRequest(); - transferRequest.setFromAccountId(2); - transferRequest.setToUserId(4); - transferRequest.setToAccountId(4); - transferRequest.setAmount(5000); - ObjectMapper mapper = new ObjectMapper(); - String jsonString = mapper.writeValueAsString(transferRequest); + JSONObject transfer = new JSONObject(); + transfer.put("fromAccountId", 2); + transfer.put("toUserId", 4); + transfer.put("toAccountId", 4); + transfer.put("amount", 5000); - mockMvc.perform(post("/transfer") - .content(jsonString)) - .andExpect(status().isOk()); + UserDetails userDetails = new BankingUserDetails(2, "username2", "password2", false); + mockMvc.perform(post("/transfer") +// .with(user("username2").password("password2").authorities()) + .with(user(userDetails)) + .contentType(MediaType.APPLICATION_JSON) + .content(transfer.toString())) +// .andExpect(status().isOk()); + .andExpect(status().is4xxClientError()); } + + + @Test + @WithMockUser(roles = "ADMIN") + void createUser_Test_OK() throws Exception { + JSONObject userRequest = new JSONObject(); + userRequest.put("username", "username"); + userRequest.put("password", "password"); + + mockMvc.perform(post("/user") + .contentType(MediaType.APPLICATION_JSON) + .content(userRequest.toString())) + .andExpect(status().isOk()); + } + @Test + @WithMockUser(roles = "ADMIN") + void createUser_Test_TrowUserAlreadyExistsException() throws Exception { + JSONObject userRequest = new JSONObject(); + userRequest.put("username", "username1"); + userRequest.put("password", "password1"); + + mockMvc.perform(post("/user") + .contentType(MediaType.APPLICATION_JSON) + .content(userRequest.toString())) + .andExpect(status().is4xxClientError()); + } + @Test + @WithMockUser(roles = "USER") + void createUser_TestWithUserRole_notOK() throws Exception { + JSONObject userRequest = new JSONObject(); + userRequest.put("username", "username"); + userRequest.put("password", "password"); + + mockMvc.perform(post("/user") + .contentType(MediaType.APPLICATION_JSON) + .content(userRequest.toString())) + .andExpect(status().isOk()); + } } \ No newline at end of file From 64756aca338d1136b9cbf446e36abc1a14d7216d Mon Sep 17 00:00:00 2001 From: ruslanTash <119006686+ruslanTash@users.noreply.github.com> Date: Sat, 19 Aug 2023 00:23:04 +0400 Subject: [PATCH 11/17] =?UTF-8?q?=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B0=2040?= =?UTF-8?q?1,=20=D0=BF=D0=B0=D1=80=D0=BE=D0=BB=D1=8C=20=D0=BD=D0=B5=20BCry?= =?UTF-8?q?pt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/UserControllerTest.java | 17 ++++++++++++----- 1 file changed, 12 insertions(+), 5 deletions(-) diff --git a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java index a75da96..a544460 100644 --- a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java +++ b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java @@ -16,7 +16,9 @@ import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.MediaType; +import org.springframework.security.core.Authentication; import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.security.web.SecurityFilterChain; import org.springframework.test.context.DynamicPropertyRegistry; @@ -30,11 +32,13 @@ import org.testcontainers.junit.jupiter.Testcontainers; import org.testcontainers.shaded.com.fasterxml.jackson.databind.JavaType; import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper; +import org.testcontainers.shaded.org.bouncycastle.crypto.generators.BCrypt; import javax.sql.DataSource; import javax.validation.Valid; import java.sql.Connection; import java.sql.SQLException; +import java.util.Base64; import java.util.List; import java.util.Optional; @@ -44,6 +48,7 @@ import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; +import static org.testcontainers.shaded.com.google.common.io.BaseEncoding.base64; @SpringBootTest @@ -135,7 +140,6 @@ void testPostgresql() throws SQLException { } } - // Проверка получения списка юзеров ("/user/list") @Test @WithMockUser(roles = "USER") void givenUsers_OK() throws Exception { @@ -163,6 +167,11 @@ void givenUsers_AdminNoAccess_Error403() throws Exception { .andExpect(status().is4xxClientError()); } + private static String getBasicAuthenticationHeader(String username, String password) { + String valueToEncode = username + ":" + password; + return "Basic " + Base64.getEncoder().encodeToString(valueToEncode.getBytes()); + } + @Test @WithMockUser(roles = "USER") void getTranzaction() throws Exception { @@ -176,12 +185,10 @@ void getTranzaction() throws Exception { UserDetails userDetails = new BankingUserDetails(2, "username2", "password2", false); mockMvc.perform(post("/transfer") -// .with(user("username2").password("password2").authorities()) - .with(user(userDetails)) + .header("Authorization", getBasicAuthenticationHeader(userDetails.getUsername(), userDetails.getPassword())) .contentType(MediaType.APPLICATION_JSON) .content(transfer.toString())) -// .andExpect(status().isOk()); - .andExpect(status().is4xxClientError()); + .andExpect(status().isOk()); } From 46ad211a88150ab2a36987a7ab0c4fe15579a4a5 Mon Sep 17 00:00:00 2001 From: ruslanTash <119006686+ruslanTash@users.noreply.github.com> Date: Sat, 19 Aug 2023 09:32:14 +0400 Subject: [PATCH 12/17] =?UTF-8?q?=D0=BE=D1=88=D0=B8=D0=B1=D0=BA=D0=B0=2040?= =?UTF-8?q?1,=20=D0=BF=D0=B0=D1=80=D0=BE=D0=BB=D1=8C=20=D0=BD=D0=B5=20BCry?= =?UTF-8?q?pt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../simplebanking/controller/UserControllerTest.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java index a544460..c7a135c 100644 --- a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java +++ b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java @@ -15,6 +15,7 @@ import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; import org.springframework.security.core.Authentication; import org.springframework.security.core.userdetails.UserDetails; @@ -25,6 +26,7 @@ import org.springframework.test.context.DynamicPropertySource; import org.springframework.test.web.servlet.MockMvc; import org.springframework.test.web.servlet.request.RequestPostProcessor; +import org.springframework.util.Base64Utils; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.testcontainers.containers.PostgreSQLContainer; @@ -36,6 +38,7 @@ import javax.sql.DataSource; import javax.validation.Valid; +import java.nio.charset.StandardCharsets; import java.sql.Connection; import java.sql.SQLException; import java.util.Base64; @@ -169,11 +172,11 @@ void givenUsers_AdminNoAccess_Error403() throws Exception { private static String getBasicAuthenticationHeader(String username, String password) { String valueToEncode = username + ":" + password; - return "Basic " + Base64.getEncoder().encodeToString(valueToEncode.getBytes()); + return "Basic " + Base64Utils.encodeToString((username + ":" + password).getBytes(StandardCharsets.UTF_8)); } @Test - @WithMockUser(roles = "USER") +// @WithMockUser(roles = "USER") void getTranzaction() throws Exception { JSONObject transfer = new JSONObject(); @@ -185,7 +188,7 @@ void getTranzaction() throws Exception { UserDetails userDetails = new BankingUserDetails(2, "username2", "password2", false); mockMvc.perform(post("/transfer") - .header("Authorization", getBasicAuthenticationHeader(userDetails.getUsername(), userDetails.getPassword())) + .header(HttpHeaders.AUTHORIZATION, getBasicAuthenticationHeader(userDetails.getUsername(), userDetails.getPassword())) .contentType(MediaType.APPLICATION_JSON) .content(transfer.toString())) .andExpect(status().isOk()); From 55a2683f2421ac15d59a1c7f4b6a1c9942b76151 Mon Sep 17 00:00:00 2001 From: ruslanTash <119006686+ruslanTash@users.noreply.github.com> Date: Sat, 19 Aug 2023 22:19:55 +0400 Subject: [PATCH 13/17] =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D1=85=D0=BE=D0=B4=D1=8F=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/UserControllerTest.java | 126 ++++++++---------- 1 file changed, 54 insertions(+), 72 deletions(-) diff --git a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java index c7a135c..5b0ad6f 100644 --- a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java +++ b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java @@ -1,13 +1,12 @@ package com.skypro.simplebanking.controller; -import com.skypro.simplebanking.configuration.AdminSecurityFilter; -import com.skypro.simplebanking.dto.*; import com.skypro.simplebanking.dto.BankingUserDetails; import com.skypro.simplebanking.entity.Account; import com.skypro.simplebanking.entity.AccountCurrency; import com.skypro.simplebanking.entity.User; import com.skypro.simplebanking.repository.AccountRepository; import com.skypro.simplebanking.repository.UserRepository; +import com.skypro.simplebanking.service.UserService; import net.minidev.json.JSONObject; import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeEach; @@ -17,41 +16,26 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; -import org.springframework.security.core.Authentication; import org.springframework.security.core.userdetails.UserDetails; -import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder; import org.springframework.security.test.context.support.WithMockUser; -import org.springframework.security.web.SecurityFilterChain; import org.springframework.test.context.DynamicPropertyRegistry; import org.springframework.test.context.DynamicPropertySource; import org.springframework.test.web.servlet.MockMvc; -import org.springframework.test.web.servlet.request.RequestPostProcessor; import org.springframework.util.Base64Utils; -import org.springframework.web.bind.annotation.PostMapping; -import org.springframework.web.bind.annotation.RequestBody; import org.testcontainers.containers.PostgreSQLContainer; import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; -import org.testcontainers.shaded.com.fasterxml.jackson.databind.JavaType; -import org.testcontainers.shaded.com.fasterxml.jackson.databind.ObjectMapper; -import org.testcontainers.shaded.org.bouncycastle.crypto.generators.BCrypt; import javax.sql.DataSource; -import javax.validation.Valid; import java.nio.charset.StandardCharsets; import java.sql.Connection; import java.sql.SQLException; -import java.util.Base64; import java.util.List; -import java.util.Optional; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; -import static org.assertj.core.api.AssertionsForClassTypes.assertThatExceptionOfType; -import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.user; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; -import static org.testcontainers.shaded.com.google.common.io.BaseEncoding.base64; @SpringBootTest @@ -79,61 +63,23 @@ static void postgresProperties(DynamicPropertyRegistry registry) { private UserRepository userRepository; @Autowired private AccountRepository accountRepository; + @Autowired + private UserService userService; @BeforeEach void createRepository() { - User user1 = new User(); - user1.setUsername("username1"); - user1.setPassword("password1"); - User user2 = new User(); - user2.setUsername("username2"); - user2.setPassword("password2"); - User user3 = new User(); - user3.setUsername("username3"); - user3.setPassword("password3"); - User user4 = new User(); - user4.setUsername("username4"); - user4.setPassword("password4"); - User user5 = new User(); - user5.setUsername("username5"); - user5.setPassword("password5"); - - Account account1 = new Account(); - account1.setId(1L); - account1.setAccountCurrency(AccountCurrency.RUB); - account1.setAmount(10000L); - account1.setUser(user1); - Account account2 = new Account(); - account2.setId(2L); - account2.setAccountCurrency(AccountCurrency.EUR); - account2.setAmount(1000L); - account2.setUser(user2); - Account account3 = new Account(); - account3.setId(3L); - account3.setAccountCurrency(AccountCurrency.USD); - account3.setAmount(2000L); - account3.setUser(user3); - Account account4 = new Account(); - account4.setId(4L); - account4.setAccountCurrency(AccountCurrency.EUR); - account4.setAmount(4000L); - account4.setUser(user4); - Account account5 = new Account(); - account5.setId(5L); - account5.setAccountCurrency(AccountCurrency.RUB); - account5.setAmount(50000L); - account5.setUser(user5); - - List users = List.of(user1, user2, user3, user4, user5); - List accounts = List.of(account1, account2, account3, account4, account5); - - userRepository.saveAll(users); + userService.createUser("username1","password1" ); + userService.createUser("username2","password2" ); + userService.createUser("username3","password3" ); + userService.createUser("username4","password4" ); + userService.createUser("username5","password5" ); } @AfterEach void cleanRepository() { userRepository.deleteAll(); + accountRepository.deleteAll(); } @Test @@ -171,29 +117,65 @@ void givenUsers_AdminNoAccess_Error403() throws Exception { } private static String getBasicAuthenticationHeader(String username, String password) { - String valueToEncode = username + ":" + password; return "Basic " + Base64Utils.encodeToString((username + ":" + password).getBytes(StandardCharsets.UTF_8)); } @Test -// @WithMockUser(roles = "USER") - void getTranzaction() throws Exception { + void getTranzaction_Test_OK() throws Exception { JSONObject transfer = new JSONObject(); - transfer.put("fromAccountId", 2); - transfer.put("toUserId", 4); + transfer.put("fromAccountId", 1); + transfer.put("toUserId", 2); transfer.put("toAccountId", 4); - transfer.put("amount", 5000); - - UserDetails userDetails = new BankingUserDetails(2, "username2", "password2", false); + transfer.put("amount", 1); mockMvc.perform(post("/transfer") - .header(HttpHeaders.AUTHORIZATION, getBasicAuthenticationHeader(userDetails.getUsername(), userDetails.getPassword())) + .header(HttpHeaders.AUTHORIZATION, getBasicAuthenticationHeader("username1", "password1")) .contentType(MediaType.APPLICATION_JSON) .content(transfer.toString())) .andExpect(status().isOk()); } + @Test + void getTranzaction_Test_WrongAccountCurrency_Status400() throws Exception { + + JSONObject transfer = new JSONObject(); + transfer.put("fromAccountId", 1); + transfer.put("toUserId", 2); + transfer.put("toAccountId", 5); + transfer.put("amount", 1); + + mockMvc.perform(post("/transfer") + .header(HttpHeaders.AUTHORIZATION, getBasicAuthenticationHeader("username1", "password1")) + .contentType(MediaType.APPLICATION_JSON) + .content(transfer.toString())) + .andExpect(status().is4xxClientError()); + } + + @Test + void getTranzaction_Test_AccountNotFoundException() throws Exception { + + JSONObject transfer = new JSONObject(); + transfer.put("fromAccountId", 1); + transfer.put("toUserId", 2); + transfer.put("toAccountId", 44); + transfer.put("amount", 1); + + mockMvc.perform(post("/transfer") + .header(HttpHeaders.AUTHORIZATION, getBasicAuthenticationHeader("username1", "password1")) + .contentType(MediaType.APPLICATION_JSON) + .content(transfer.toString())) + .andExpect(status().is4xxClientError()); + } + + @Test + void getMyProfile_Test_OK() throws Exception { + mockMvc.perform(get("/user/me") + .header(HttpHeaders.AUTHORIZATION, getBasicAuthenticationHeader("username2", "password2"))) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.username").value("username2")) + .andExpect(jsonPath("$.accounts.length()").value(3)); + } @Test From 624d25091c5f3a2f8718053c0d323ef5211ffa65 Mon Sep 17 00:00:00 2001 From: ruslanTash <119006686+ruslanTash@users.noreply.github.com> Date: Sat, 19 Aug 2023 23:11:26 +0400 Subject: [PATCH 14/17] =?UTF-8?q?=D1=82=D0=B5=D1=81=D1=82=D1=8B=20=D0=BF?= =?UTF-8?q?=D1=80=D0=BE=D1=85=D0=BE=D0=B4=D1=8F=D1=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../controller/UserControllerTest.java | 43 +++++++++++++------ 1 file changed, 29 insertions(+), 14 deletions(-) diff --git a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java index 5b0ad6f..288756c 100644 --- a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java +++ b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java @@ -1,9 +1,8 @@ package com.skypro.simplebanking.controller; +import com.skypro.simplebanking.dto.AccountDTO; +import com.skypro.simplebanking.dto.BalanceChangeRequest; import com.skypro.simplebanking.dto.BankingUserDetails; -import com.skypro.simplebanking.entity.Account; -import com.skypro.simplebanking.entity.AccountCurrency; -import com.skypro.simplebanking.entity.User; import com.skypro.simplebanking.repository.AccountRepository; import com.skypro.simplebanking.repository.UserRepository; import com.skypro.simplebanking.service.UserService; @@ -16,12 +15,15 @@ import org.springframework.boot.test.context.SpringBootTest; import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; -import org.springframework.security.core.userdetails.UserDetails; +import org.springframework.security.core.Authentication; import org.springframework.security.test.context.support.WithMockUser; import org.springframework.test.context.DynamicPropertyRegistry; import org.springframework.test.context.DynamicPropertySource; import org.springframework.test.web.servlet.MockMvc; import org.springframework.util.Base64Utils; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.RequestBody; import org.testcontainers.containers.PostgreSQLContainer; import org.testcontainers.junit.jupiter.Container; import org.testcontainers.junit.jupiter.Testcontainers; @@ -30,7 +32,6 @@ import java.nio.charset.StandardCharsets; import java.sql.Connection; import java.sql.SQLException; -import java.util.List; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; @@ -177,7 +178,6 @@ void getMyProfile_Test_OK() throws Exception { .andExpect(jsonPath("$.accounts.length()").value(3)); } - @Test @WithMockUser(roles = "ADMIN") void createUser_Test_OK() throws Exception { @@ -202,16 +202,31 @@ void createUser_Test_TrowUserAlreadyExistsException() throws Exception { .content(userRequest.toString())) .andExpect(status().is4xxClientError()); } + + @Test - @WithMockUser(roles = "USER") - void createUser_TestWithUserRole_notOK() throws Exception { - JSONObject userRequest = new JSONObject(); - userRequest.put("username", "username"); - userRequest.put("password", "password"); + void depositToAccount_Test_OK() throws Exception { + JSONObject balanceChangeRequest = new JSONObject(); + balanceChangeRequest.put("amount", 10000L); - mockMvc.perform(post("/user") + mockMvc.perform(post("/account/deposit/{id}", 1) + .header(HttpHeaders.AUTHORIZATION, getBasicAuthenticationHeader("username1", "password1")) .contentType(MediaType.APPLICATION_JSON) - .content(userRequest.toString())) - .andExpect(status().isOk()); + .content(balanceChangeRequest.toString())) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.amount").value(10001)); + + } + @Test + void withdrawFromAccount_Test_notOK_TrowInsufficientFundsException() throws Exception { + JSONObject balanceChangeRequest = new JSONObject(); + balanceChangeRequest.put("amount", 10000L); + + mockMvc.perform(post("/account/withdraw/{id}", 1) + .header(HttpHeaders.AUTHORIZATION, getBasicAuthenticationHeader("username1", "password1")) + .contentType(MediaType.APPLICATION_JSON) + .content(balanceChangeRequest.toString())) + .andExpect(status().is4xxClientError()); + } } \ No newline at end of file From 173a5656b7967fea7872f2cb387e921553406519 Mon Sep 17 00:00:00 2001 From: ruslanTash <119006686+ruslanTash@users.noreply.github.com> Date: Sat, 19 Aug 2023 23:29:42 +0400 Subject: [PATCH 15/17] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20maven-compiler-plugin=20pom.xml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 10 ++++++++++ .../simplebanking/controller/UserControllerTest.java | 6 ++---- 2 files changed, 12 insertions(+), 4 deletions(-) diff --git a/pom.xml b/pom.xml index 5049a1d..5733dc4 100644 --- a/pom.xml +++ b/pom.xml @@ -78,6 +78,16 @@ org.springframework.boot spring-boot-maven-plugin + + org.apache.maven.plugins + maven-compiler-plugin + 3.1 + + 1.8 + 1.8 + UTF-8 + + diff --git a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java index 288756c..8832d64 100644 --- a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java +++ b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java @@ -123,7 +123,6 @@ private static String getBasicAuthenticationHeader(String username, String passw @Test void getTranzaction_Test_OK() throws Exception { - JSONObject transfer = new JSONObject(); transfer.put("fromAccountId", 1); transfer.put("toUserId", 2); @@ -172,9 +171,9 @@ void getTranzaction_Test_AccountNotFoundException() throws Exception { @Test void getMyProfile_Test_OK() throws Exception { mockMvc.perform(get("/user/me") - .header(HttpHeaders.AUTHORIZATION, getBasicAuthenticationHeader("username2", "password2"))) + .header(HttpHeaders.AUTHORIZATION, getBasicAuthenticationHeader("username1", "password1"))) .andExpect(status().isOk()) - .andExpect(jsonPath("$.username").value("username2")) + .andExpect(jsonPath("$.username").value("username1")) .andExpect(jsonPath("$.accounts.length()").value(3)); } @@ -203,7 +202,6 @@ void createUser_Test_TrowUserAlreadyExistsException() throws Exception { .andExpect(status().is4xxClientError()); } - @Test void depositToAccount_Test_OK() throws Exception { JSONObject balanceChangeRequest = new JSONObject(); From 326e0e6831c0a074de3ae0c853e64ede172b32e9 Mon Sep 17 00:00:00 2001 From: ruslanTash <119006686+ruslanTash@users.noreply.github.com> Date: Sun, 20 Aug 2023 01:58:16 +0400 Subject: [PATCH 16/17] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20maven-compiler-plugin=20pom.xml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pom.xml | 4 +- .../controller/UserControllerTest.java | 53 +++++++++++++------ 2 files changed, 38 insertions(+), 19 deletions(-) diff --git a/pom.xml b/pom.xml index 5733dc4..d940a6d 100644 --- a/pom.xml +++ b/pom.xml @@ -83,8 +83,8 @@ maven-compiler-plugin 3.1 - 1.8 - 1.8 + 10 + 10 UTF-8 diff --git a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java index 8832d64..9ad0fc2 100644 --- a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java +++ b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java @@ -3,6 +3,8 @@ import com.skypro.simplebanking.dto.AccountDTO; import com.skypro.simplebanking.dto.BalanceChangeRequest; import com.skypro.simplebanking.dto.BankingUserDetails; +import com.skypro.simplebanking.entity.Account; +import com.skypro.simplebanking.entity.User; import com.skypro.simplebanking.repository.AccountRepository; import com.skypro.simplebanking.repository.UserRepository; import com.skypro.simplebanking.service.UserService; @@ -32,6 +34,9 @@ import java.nio.charset.StandardCharsets; import java.sql.Connection; import java.sql.SQLException; +import java.util.ArrayList; +import java.util.Collection; +import java.util.List; import static org.assertj.core.api.AssertionsForClassTypes.assertThat; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; @@ -90,6 +95,21 @@ void testPostgresql() throws SQLException { } } + @Test + void getTranzaction_Test_OK() throws Exception { + JSONObject transfer = new JSONObject(); + transfer.put("fromAccountId", getAccountIdByUsername("username1")); + transfer.put("toUserId", getUserIdByUserName("username2")); + transfer.put("toAccountId", getAccountIdByUsername("username2")); + transfer.put("amount", 1); + + mockMvc.perform(post("/transfer") + .header(HttpHeaders.AUTHORIZATION, getBasicAuthenticationHeader("username1", "password1")) + .contentType(MediaType.APPLICATION_JSON) + .content(transfer.toString())) + .andExpect(status().isOk()); + } + @Test @WithMockUser(roles = "USER") void givenUsers_OK() throws Exception { @@ -117,25 +137,10 @@ void givenUsers_AdminNoAccess_Error403() throws Exception { .andExpect(status().is4xxClientError()); } - private static String getBasicAuthenticationHeader(String username, String password) { + private String getBasicAuthenticationHeader(String username, String password) { return "Basic " + Base64Utils.encodeToString((username + ":" + password).getBytes(StandardCharsets.UTF_8)); } - @Test - void getTranzaction_Test_OK() throws Exception { - JSONObject transfer = new JSONObject(); - transfer.put("fromAccountId", 1); - transfer.put("toUserId", 2); - transfer.put("toAccountId", 4); - transfer.put("amount", 1); - - mockMvc.perform(post("/transfer") - .header(HttpHeaders.AUTHORIZATION, getBasicAuthenticationHeader("username1", "password1")) - .contentType(MediaType.APPLICATION_JSON) - .content(transfer.toString())) - .andExpect(status().isOk()); - } - @Test void getTranzaction_Test_WrongAccountCurrency_Status400() throws Exception { @@ -207,7 +212,7 @@ void depositToAccount_Test_OK() throws Exception { JSONObject balanceChangeRequest = new JSONObject(); balanceChangeRequest.put("amount", 10000L); - mockMvc.perform(post("/account/deposit/{id}", 1) + mockMvc.perform(post("/account/deposit/{id}", getAccountIdByUsername("username1")) .header(HttpHeaders.AUTHORIZATION, getBasicAuthenticationHeader("username1", "password1")) .contentType(MediaType.APPLICATION_JSON) .content(balanceChangeRequest.toString())) @@ -227,4 +232,18 @@ void withdrawFromAccount_Test_notOK_TrowInsufficientFundsException() throws Exce .andExpect(status().is4xxClientError()); } + + + private User getUserByName(String username){ + return userRepository.findByUsername(username).orElseThrow(); + } + private long getUserIdByUserName(String username){ + return getUserByName(username).getId(); + } + private long getAccountIdByUsername(String username){ + long userId = getUserIdByUserName(username); + long ost = userId % userRepository.count(); + long count = (userId - ost) / userRepository.count(); + return count * 3 * userRepository.count() + ost*3; + } } \ No newline at end of file From 44e08af6b344c068dd868f95b9d0be0bb26a4f65 Mon Sep 17 00:00:00 2001 From: ruslanTash <119006686+ruslanTash@users.noreply.github.com> Date: Sun, 20 Aug 2023 01:58:26 +0400 Subject: [PATCH 17/17] =?UTF-8?q?=D0=B4=D0=BE=D0=B1=D0=B0=D0=B2=D0=B8?= =?UTF-8?q?=D0=BB=20maven-compiler-plugin=20pom.xml?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../com/skypro/simplebanking/controller/UserControllerTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java index 9ad0fc2..af0af29 100644 --- a/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java +++ b/src/test/java/com/skypro/simplebanking/controller/UserControllerTest.java @@ -120,6 +120,7 @@ void givenUsers_OK() throws Exception { .andExpect(jsonPath("$.length()").value(5)); } + @Test @WithMockUser(roles = "USER") void givenNoBody_whenEmptyJsonArray() throws Exception {