Skip to content

Commit

Permalink
Test: #117 베이스 기타 매물 수정 api test 코드 작성
Browse files Browse the repository at this point in the history
  • Loading branch information
tinon1004 authored and Wo-ogie committed May 20, 2024
1 parent 7fa578c commit 255aedb
Show file tree
Hide file tree
Showing 3 changed files with 149 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@
import com.ajou.hertz.domain.instrument.bass_guitar.constant.BassGuitarPreAmplifier;
import com.ajou.hertz.domain.instrument.bass_guitar.dto.BassGuitarDto;
import com.ajou.hertz.domain.instrument.bass_guitar.dto.request.BassGuitarFilterConditions;
import com.ajou.hertz.domain.instrument.bass_guitar.dto.request.BassGuitarUpdateRequest;
import com.ajou.hertz.domain.instrument.bass_guitar.dto.request.CreateNewBassGuitarRequest;
import com.ajou.hertz.domain.instrument.constant.GuitarColor;
import com.ajou.hertz.domain.instrument.constant.InstrumentCategory;
Expand Down Expand Up @@ -753,6 +754,47 @@ public InstrumentControllerTest(MockMvc mvc) {
verifyEveryMocksShouldHaveNoMoreInteractions();
}

@Test
void 수정할_베이스_기타_정보가_주어지고_주어진_정보로_매물_정보를_수정한다() throws Exception {
// given
long userId = 1L;
long bassGuitarId = 2L;
BassGuitarUpdateRequest updateRequest = createBassGuitarUpdateRequest();
BassGuitarDto expectedResult = createBassGuitarDto(bassGuitarId, userId);
given(instrumentCommandService.updateBassGuitar(
eq(userId),
eq(bassGuitarId),
any(BassGuitarUpdateRequest.class)
)).willReturn(expectedResult);

// when & then
mvc.perform(
multipart("/api/instruments/bass-guitars/{bassGuitarId}", bassGuitarId)
.header(API_VERSION_HEADER_NAME, 1)
.param("title", updateRequest.getTitle())
.param("progressStatus", String.valueOf(updateRequest.getProgressStatus()))
.param("qualityStatus", String.valueOf(updateRequest.getQualityStatus()))
.param("price", String.valueOf(updateRequest.getPrice()))
.param("hasAnomaly", String.valueOf(updateRequest.getHasAnomaly()))
.param("description", updateRequest.getDescription())
.param("brand", String.valueOf(updateRequest.getBrand()))
.param("pickUp", String.valueOf(updateRequest.getPickUp()))
.param("preAmplifier", String.valueOf(updateRequest.getPreAmplifier()))
.param("color", String.valueOf(updateRequest.getColor()))
.with(user(createTestUser(userId)))
.with(request -> {
request.setMethod("PATCH");
return request;
})
)
.andExpect(status().isOk())
.andExpect(jsonPath("$.id").value(expectedResult.getId()));
then(instrumentCommandService)
.should()
.updateBassGuitar(eq(userId), eq(bassGuitarId), any(BassGuitarUpdateRequest.class));
verifyEveryMocksShouldHaveNoMoreInteractions();
}

@Test
void 수정할_앰프_정보가_주어지고_주어진_정보로_매물_정보를_수정한다() throws Exception {
// given
Expand Down Expand Up @@ -1289,4 +1331,24 @@ private AudioEquipmentUpdateRequest createAudioEquipmentUpdateRequest() throws E
);
}

private BassGuitarUpdateRequest createBassGuitarUpdateRequest() throws Exception {
return ReflectionUtils.createBassGuitarUpdateRequest(
"Test Bass Guitar",
InstrumentProgressStatus.SOLD_OUT,
createAddressRequest(),
(short)3,
550000,
true,
"description",
null,
null,
null,
null,
BassGuitarBrand.FENDER,
BassGuitarPickUp.JAZZ,
BassGuitarPreAmplifier.ACTIVE,
GuitarColor.RED
);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import com.ajou.hertz.domain.instrument.bass_guitar.constant.BassGuitarPickUp;
import com.ajou.hertz.domain.instrument.bass_guitar.constant.BassGuitarPreAmplifier;
import com.ajou.hertz.domain.instrument.bass_guitar.dto.BassGuitarDto;
import com.ajou.hertz.domain.instrument.bass_guitar.dto.request.BassGuitarUpdateRequest;
import com.ajou.hertz.domain.instrument.bass_guitar.dto.request.CreateNewBassGuitarRequest;
import com.ajou.hertz.domain.instrument.bass_guitar.entity.BassGuitar;
import com.ajou.hertz.domain.instrument.constant.GuitarColor;
Expand Down Expand Up @@ -487,6 +488,36 @@ class InstrumentCommandServiceTest {
assertThat(result.getImages()).hasSize(newImages.size());
}

@Test
void 추가할_이미지들이_주어지고_베이스_기타_정보를_수정하면_새로운_악기_이미지들이_추가된다() throws Exception {
// given
long userId = 1L;
long instrumentId = 2L;
BassGuitar bassGuitar = createBassGuitar(instrumentId, createUser(userId));
List<MultipartFile> newImages = List.of(
createMultipartFile(),
createMultipartFile()
);
List<InstrumentImage> newInstrumentImages = List.of(
createInstrumentImage(3L, bassGuitar),
createInstrumentImage(4L, bassGuitar)
);
BassGuitarUpdateRequest updateRequest = createBassGuitarUpdateRequest(List.of(), newImages, null, null);
given(instrumentQueryService.getInstrumentById(instrumentId))
.willReturn(bassGuitar);
given(instrumentImageCommandService.saveImages(bassGuitar, updateRequest.getNewImages()))
.willReturn(newInstrumentImages);

// when
BassGuitarDto result = sut.updateBassGuitar(userId, instrumentId, updateRequest);

// then
then(instrumentQueryService).should().getInstrumentById(instrumentId);
then(instrumentImageCommandService).should().saveImages(bassGuitar, updateRequest.getNewImages());
verifyEveryMocksShouldHaveNoMoreInteractions();
assertThat(result.getImages()).hasSize(newImages.size());
}

@Test
void 삭제할_해시태그들의_id_리스트가_주어지고_매물_정보를_수정하면_해시태그가_삭제된다() throws Exception {
long userId = 1L;
Expand Down Expand Up @@ -924,4 +955,17 @@ private AudioEquipmentUpdateRequest createAudioEquipmentUpdateRequest(
null
);
}

private BassGuitarUpdateRequest createBassGuitarUpdateRequest(
@Nullable List<Long> deletedImageIds,
@Nullable List<MultipartFile> newImages,
@Nullable List<Long> deletedHashtagIds,
@Nullable List<String> newHashtags
) throws Exception {
return ReflectionUtils.createBassGuitarUpdateRequest(
null, null, null, null, null, null, null,
deletedImageIds, newImages, deletedHashtagIds, newHashtags,
null, null, null, null
);
}
}
43 changes: 43 additions & 0 deletions src/test/java/com/ajou/hertz/util/ReflectionUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import com.ajou.hertz.domain.instrument.bass_guitar.constant.BassGuitarPreAmplifier;
import com.ajou.hertz.domain.instrument.bass_guitar.dto.BassGuitarDto;
import com.ajou.hertz.domain.instrument.bass_guitar.dto.request.BassGuitarFilterConditions;
import com.ajou.hertz.domain.instrument.bass_guitar.dto.request.BassGuitarUpdateRequest;
import com.ajou.hertz.domain.instrument.bass_guitar.dto.request.CreateNewBassGuitarRequest;
import com.ajou.hertz.domain.instrument.bass_guitar.entity.BassGuitar;
import com.ajou.hertz.domain.instrument.constant.GuitarColor;
Expand Down Expand Up @@ -1176,6 +1177,48 @@ public static AudioEquipmentUpdateRequest createAudioEquipmentUpdateRequest(
);
}

public static BassGuitarUpdateRequest createBassGuitarUpdateRequest(
@Nullable String title,
@Nullable InstrumentProgressStatus progressStatus,
@Nullable AddressRequest tradeAddress,
@Nullable Short qualityStatus,
@Nullable Integer price,
@Nullable Boolean hasAnomaly,
@Nullable String description,
@Nullable List<Long> deletedImageIds,
@Nullable List<MultipartFile> newImages,
@Nullable List<Long> deletedHashtagIds,
@Nullable List<String> newHashtags,
@Nullable BassGuitarBrand brand,
@Nullable BassGuitarPickUp pickUp,
@Nullable BassGuitarPreAmplifier preAmplifier,
@Nullable GuitarColor color
) throws Exception {
Constructor<BassGuitarUpdateRequest> constructor = BassGuitarUpdateRequest.class.getDeclaredConstructor(
String.class, InstrumentProgressStatus.class, AddressRequest.class, Short.class,
Integer.class, Boolean.class, String.class, List.class, List.class, List.class, List.class,
BassGuitarBrand.class, BassGuitarPickUp.class, BassGuitarPreAmplifier.class, GuitarColor.class
);
constructor.setAccessible(true);
return constructor.newInstance(
title,
progressStatus,
tradeAddress,
qualityStatus,
price,
hasAnomaly,
description,
deletedImageIds,
newImages,
deletedHashtagIds,
newHashtags,
brand,
pickUp,
preAmplifier,
color
);
}

public static SendUserAuthCodeRequest createSendUserAuthCodeRequest(String phoneNumber) throws Exception {
Constructor<SendUserAuthCodeRequest> constructor =
SendUserAuthCodeRequest.class.getDeclaredConstructor(String.class);
Expand Down

0 comments on commit 255aedb

Please sign in to comment.