Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[TEST/#299] 배너 생성 컨트롤러 테스트코드 작성 #301

Merged
merged 5 commits into from
Jan 10, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import org.sopt.makers.operation.web.banner.dto.request.*;
import org.sopt.makers.operation.web.banner.dto.response.BannerResponse;
import org.sopt.makers.operation.web.banner.dto.response.BannerResponse.BannerDetail;
import org.sopt.makers.operation.web.banner.dto.response.BannerResponse.BannerImageUrl;

public interface BannerService {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
package org.sopt.makers.operation.web.banner.api;

import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import com.fasterxml.jackson.databind.*;
import org.junit.jupiter.api.*;

import org.sopt.makers.operation.code.success.web.BannerSuccessCode;
import org.sopt.makers.operation.filter.JwtAuthenticationFilter;
import org.sopt.makers.operation.filter.JwtExceptionFilter;
import org.sopt.makers.operation.jwt.JwtTokenProvider;
import org.sopt.makers.operation.web.banner.dto.request.BannerRequest.*;
import org.sopt.makers.operation.web.banner.dto.response.BannerResponse;
import org.sopt.makers.operation.web.banner.service.BannerService;

import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.context.annotation.FilterType;
import org.springframework.context.annotation.ComponentScan;
Expand All @@ -23,12 +22,15 @@
import java.security.Principal;
import java.time.LocalDate;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import static org.springframework.http.MediaType.APPLICATION_JSON;
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;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

Expand All @@ -45,6 +47,8 @@ class BannerApiControllerTest {
private BannerService bannerService;
@Autowired
private MockMvc mockMvc;
@Autowired
ObjectMapper objectMapper;

@BeforeEach
void setMockBanner() {
Expand Down Expand Up @@ -89,7 +93,7 @@ void getBannerDetail() throws Exception {
@DisplayName("(DELETE) Banner Delete")
void deleteBanner() throws Exception {
//given
BannerResponse.BannerDetail mockBannerDetail = bannerService.getBannerDetail(MOCK_BANNER_ID);
doNothing().when(bannerService).deleteBanner(MOCK_BANNER_ID);

this.mockMvc.perform(
//when
Expand All @@ -100,7 +104,6 @@ void deleteBanner() throws Exception {
//then
.andExpect(status().isNoContent())
.andExpect(jsonPath("$.success").value("true"));

}

@Test
Expand All @@ -121,4 +124,39 @@ void getExternalBanners() throws Exception {
.andExpect(status().isOk())
.andExpect(jsonPath("$.success").value("true"));
}

@Nested
class CreateBannerTests {
@Test
@DisplayName("(POST) New Banner")
void createNewBanner() throws Exception {
// given
BannerCreate bannerCreate = new BannerCreate("pg_community", "product", "publisher",
LocalDate.of(2024, 1, 1), LocalDate.of(2024, 12, 31), "link", "image-url-pc", "image-url-mobile");
BannerResponse.BannerDetail mockBannerDetail = new BannerResponse.BannerDetail(
MOCK_BANNER_ID, "in_progress", "pg_community", "product", "publisher", "link",
LocalDate.of(2024, 1, 1), LocalDate.of(2024, 12, 31), "image-url-pc", "image-url-mobile");
String request = objectMapper.writeValueAsString(bannerCreate);
when(bannerService.createBanner(any(BannerCreate.class))).thenReturn(mockBannerDetail);

// when
mockMvc.perform(post("/api/v1/banners")
.contentType(APPLICATION_JSON)
.content(request))
// then
.andExpect(status().isCreated())
.andExpect(jsonPath("$.success").value(true))
.andExpect(jsonPath("$.message").value(BannerSuccessCode.SUCCESS_CREATE_BANNER.getMessage()))
.andExpect(jsonPath("$.data.id").value(mockBannerDetail.bannerId()))
.andExpect(jsonPath("$.data.status").value(mockBannerDetail.bannerStatus()))
.andExpect(jsonPath("$.data.location").value(mockBannerDetail.bannerLocation()))
.andExpect(jsonPath("$.data.content_type").value(mockBannerDetail.bannerType()))
.andExpect(jsonPath("$.data.publisher").value(mockBannerDetail.publisher()))
.andExpect(jsonPath("$.data.link").value(mockBannerDetail.link()))
.andExpect(jsonPath("$.data.start_date").value(mockBannerDetail.startDate().toString()))
.andExpect(jsonPath("$.data.end_date").value(mockBannerDetail.endDate().toString()))
.andExpect(jsonPath("$.data.image_url_pc").value(mockBannerDetail.pcImageUrl()))
.andExpect(jsonPath("$.data.image_url_mobile").value(mockBannerDetail.mobileImageUrl()));
}
}
}
Loading