Skip to content

Commit

Permalink
done
Browse files Browse the repository at this point in the history
  • Loading branch information
byzaya committed Mar 17, 2024
1 parent c3976d4 commit 2a4454a
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/main/resources/database-content.sql
Original file line number Diff line number Diff line change
Expand Up @@ -259,8 +259,8 @@ VALUES (10, -1, -1, -1),
(5, -50, -19, -5),
(5, -51, -20, -5);

CREATE
EXTENSION pgcrypto;
CREATE EXTENSION IF NOT EXISTS pgcrypto;

INSERT INTO user_account (id, full_name, password_hash, phone, role_id, speciality_id)
VALUES (-1, 'Иванов Иван Иванович', digest('123456', 'sha256'), '+79260567450', -1, NULL),
(-2, 'Глазов Степан Фёдорович', digest('654321', 'sha256'), '+79310367450', -2, -5),
Expand Down
10 changes: 10 additions & 0 deletions src/test/java/org/example/controller/TestObjects.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,16 +57,20 @@ public class TestObjects {
public static RoleDto pharmacistRole;

public static UserAccountDto simpleUser;
public static UserAccountDto testUser;
public static UserAccountDto test2User;
public static UserAccountDto doctor;
public static UserAccountDto pharmacist;

public static CartDto cart;
public static CartDto cartSecUser;
public static CartDto cartTestUser;
public static CartDto cartTest2User;
public static CartToItemDto cartItem1;
public static CartToItemDto cartItem2;
public static CartToItemDto[] cartItems;
public static CartToItemDto[] cartItemsSecUser;
public static CartToItemDto[] cartItemsTestUser;

public static Integer badRequest;
public static Integer notFoundCode;
Expand Down Expand Up @@ -138,6 +142,10 @@ public class TestObjects {
new UserAccountDto(-2L, "Глазов Степан Фёдорович", "+79310367450", doctorRole, speciality);
pharmacist =
new UserAccountDto(-3L, "Главный Пётр Петрович", "+79510367450", pharmacistRole, null);
testUser =
new UserAccountDto(-4L, "Я тестовый пользователь", "+79510367457", simpleUserRole, null);
test2User =
new UserAccountDto(-5L, "Я тоже пользователь", "+79510367458", simpleUserRole, null);
itemsFirstOrder = new ItemDto[] {receipt};
itemsSecondOrder = new ItemDto[] {special};
itemsThirdOrder = new ItemDto[] {receipt, special};
Expand Down Expand Up @@ -196,10 +204,12 @@ public class TestObjects {
cartItem2 = new CartToItemDto(special, 1);
cartItems = new CartToItemDto[] {cartItem2, cartItem1};
cartItemsSecUser = new CartToItemDto[] {cartItem2};
cartItemsTestUser = new CartToItemDto[] {cartItem1};

cart = new CartDto(-1L, userId, Arrays.stream(cartItems).toList());
cartSecUser = new CartDto(-2L, -2L, Arrays.stream(cartItemsSecUser).toList());
cartTestUser = new CartDto(-4L, -4L, Arrays.stream(cartItemsSecUser).toList());
cartTest2User = new CartDto(-5L, -5L, Arrays.stream(cartItemsTestUser).toList());

badRequest = 400;
notFoundCode = 404;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ void logoutSuccess() {
@SneakyThrows
void logoutFailed() {
mockMvc
.perform(post("/api/accounts/logout").param("user_id", "-5"))
.perform(post("/api/accounts/logout").param("user_id", "-6"))
.andExpectAll(
status().isNotFound(),
jsonPath("$.*", hasSize(3)),
Expand Down
33 changes: 30 additions & 3 deletions src/test/java/org/example/controller/cart/CartControllerTest.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package org.example.controller.cart;

import static org.example.controller.TestObjects.cartSecUser;
import static org.example.controller.TestObjects.cartTest2User;
import static org.example.controller.TestObjects.cartTestUser;
import static org.example.controller.TestObjects.creationDate;
import static org.example.controller.TestObjects.creationDateStr;
Expand All @@ -9,11 +10,15 @@
import static org.example.controller.TestObjects.notFound;
import static org.example.controller.TestObjects.notFoundCode;
import static org.example.controller.TestObjects.receipt;
import static org.example.controller.TestObjects.simpleUser;
import static org.example.controller.TestObjects.special;
import static org.example.controller.TestObjects.sumPrice;
import static org.example.controller.TestObjects.test2User;
import static org.example.controller.TestObjects.testUser;
import static org.example.controller.TestObjects.userId;
import static org.example.controller.TestObjects.userNotFound;
import static org.hamcrest.Matchers.hasSize;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
Expand All @@ -23,6 +28,7 @@
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import org.example.controller.MvcUtil;
import org.example.dto.user.UserAccountDto;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
Expand Down Expand Up @@ -51,7 +57,7 @@ void getAllItemsInCart() {
@SneakyThrows
void getAllItemsInCart_UserNotFound() {
mockMvc
.perform(get("/api/cart/-5"))
.perform(get("/api/cart/-6"))
.andExpectAll(status().isNotFound(), jsonPath("$.*", hasSize(3)))
.andExpect(jsonPath("$.code").value(userNotFound))
.andExpect(jsonPath("$.status").value(notFoundCode))
Expand All @@ -77,7 +83,7 @@ void addItemToCart() {
void addItemToCart_UserNotFound() {
mockMvc
.perform(
post("/api/cart/add").param("item_id", "-1").param("user_id", "-5").param("count", "1"))
post("/api/cart/add").param("item_id", "-1").param("user_id", "-6").param("count", "1"))
.andExpectAll(status().isNotFound(), jsonPath("$.*", hasSize(3)))
.andExpect(jsonPath("$.code").value(userNotFound))
.andExpect(jsonPath("$.status").value(notFoundCode))
Expand Down Expand Up @@ -110,7 +116,7 @@ void deleteItemFromCart() {
@SneakyThrows
void deleteItemFromCart_UserNotFound() {
mockMvc
.perform(delete("/api/cart/delete").param("item_id", "-1").param("user_id", "-5"))
.perform(delete("/api/cart/delete").param("item_id", "-1").param("user_id", "-6"))
.andExpectAll(status().isNotFound(), jsonPath("$.*", hasSize(3)))
.andExpect(jsonPath("$.code").value(userNotFound))
.andExpect(jsonPath("$.status").value(notFoundCode))
Expand Down Expand Up @@ -169,6 +175,27 @@ void addItemIntoCartAndDeleteItemFromCart() {
@Test
@SneakyThrows
void authUserAddItemIntoCartAndCheckCart() {
ResultActions result =
mockMvc
.perform(
post("/api/accounts/login")
.param("phone", "+79510367458")
.param("password", "12345"))
.andExpect(status().isOk());
UserAccountDto resultDto = mvcUtil.readResponseValue(UserAccountDto.class, result);
assertEquals(test2User, resultDto);

ResultActions result2 =
mockMvc
.perform(
post("/api/cart/add")
.param("item_id", "-1")
.param("user_id", "-5")
.param("count", "2"))
.andExpect(status().isOk());
mvcUtil.assertContentEquals(result2, objectMapper.writeValueAsString(receipt));

ResultActions result3 = mockMvc.perform(get("/api/cart/-5")).andExpect(status().isOk());
mvcUtil.assertContentEquals(result3, objectMapper.writeValueAsString(cartTest2User));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ void getUserInfoSuccess() {
@SneakyThrows
void getUserInfoFailed() {
mockMvc
.perform(get("/api/info/-5"))
.perform(get("/api/info/-6"))
.andExpectAll(
status().isNotFound(),
jsonPath("$.*", hasSize(3)),
Expand Down
6 changes: 4 additions & 2 deletions src/test/resources/test-database-content.sql
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ INSERT INTO user_account (id, full_name, password_hash, phone, role_id, speciali
VALUES (-1, 'Иванов Иван Иванович', digest('123456', 'sha256'), '+79260567450', -1, NULL),
(-2, 'Глазов Степан Фёдорович', digest('654321', 'sha256'), '+79310367450', -2, -1),
(-3, 'Главный Пётр Петрович', digest('12345', 'sha256'), '+79510367450', -3, NULL),
(-4, 'Я тестовый пользователь', digest('12345', 'sha256'), '+79510367457', -1, NULL);
(-4, 'Я тестовый пользователь', digest('12345', 'sha256'), '+79510367457', -1, NULL),
(-5, 'Я тоже пользователь', digest('12345', 'sha256'), '+79510367458', -1, NULL);;

INSERT INTO orders (creation_date, delivery_date, sum_price, id, pharmacy_id, user_id)
VALUES ('2024-01-11', '2024-01-16', 100500, -1, -1, -1),
Expand All @@ -61,7 +62,8 @@ INSERT INTO cart (id, user_id)
VALUES (-1, -1),
(-2, -2),
(-3, -3),
(-4, -4);
(-4, -4),
(-5, -5);

INSERT INTO cart_to_item (quantity, cart_id, id, item_id)
VALUES (2, -1, -1, -1),
Expand Down

0 comments on commit 2a4454a

Please sign in to comment.