Skip to content

Commit

Permalink
[BE] Test : 단위 테스트 추가 (#430)
Browse files Browse the repository at this point in the history
* - 이미지 리사이징로직 테스트 추가

* - 이미지 리사이징로직 테스트 추가

* github 환경변수 추가

* Update testAfterPR.yml

testAfterPR 수정

* Update testAfterPR.yml

testAfterPR 다시 수정

* - kakaoBookInfoFetcher 주석처리

* Update testAfterPR.yml

testAfterPR 초기화
  • Loading branch information
thwn40 authored Apr 6, 2023
1 parent 718a275 commit 12281f1
Show file tree
Hide file tree
Showing 22 changed files with 455 additions and 128 deletions.
5 changes: 2 additions & 3 deletions .github/workflows/testAfterPR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ on:

permissions: write-all


jobs:
build:
runs-on: ubuntu-latest
Expand All @@ -39,7 +38,7 @@ jobs:
- name: Grant execute permission for gradlew
run: chmod +x gradlew
working-directory: ./backend/

- name: Test with Gradle
run: ./gradlew test --stacktrace
working-directory: ./backend/
Expand All @@ -66,4 +65,4 @@ jobs:
run: |
rm -f ~/.gradle/caches/modules-2/modules-2.lock
rm -f ~/.gradle/caches/modules-2/gc.properties
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import org.springframework.data.redis.connection.Message;
import org.springframework.data.redis.connection.MessageListener;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.messaging.simp.SimpMessageSendingOperations;
import org.springframework.stereotype.Service;

Expand All @@ -25,7 +26,10 @@ public class RedisSubscriber implements MessageListener {
public void onMessage(Message message, byte[] pattern) {
try {
//레디스 에서 받은 데이터를 역직렬화
String publishMessage = String.valueOf(redisTemplate.getStringSerializer().deserialize(message.getBody()));
RedisSerializer<String> stringSerializer = redisTemplate
.getStringSerializer();
String publishMessage = String
.valueOf(stringSerializer.deserialize(message.getBody()));
RedisChat redisChat = objectMapper.readValue(publishMessage, RedisChat.class);
messagingTemplate.convertAndSend("/sub/room/" + redisChat.getRoomId(), redisChat);
log.info("레디스에서 받아옴");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
import java.io.IOException;
import java.util.UUID;

import javax.imageio.ImageIO;

import org.apache.tomcat.util.http.fileupload.ByteArrayOutputStream;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
Expand All @@ -31,6 +29,7 @@ public class AwsS3Service implements ImageUploadService {
private final ImageResizer imageResizer;
@Value("${cloud.aws.s3.bucket}")
private String bucketName;
private final ImageIOService imageIOService;



Expand All @@ -39,11 +38,11 @@ public String storeImage(MultipartFile beforeImage) throws IOException {
validateFileExists(beforeImage);
String originalFilename = beforeImage.getOriginalFilename();
String storeFileName = createStoreFileName(originalFilename);
BufferedImage bufferedImage = ImageIO.read(beforeImage.getInputStream());
BufferedImage bufferedImage = imageIOService.read(beforeImage.getInputStream());
BufferedImage resizedImage = imageResizer.resizeWithOriginalAspectRatio(bufferedImage);

try (ByteArrayOutputStream baos = new ByteArrayOutputStream()) {
ImageIO.write(resizedImage, "jpg", baos);
imageIOService.write(resizedImage, "jpg", baos);
baos.flush();
byte[] bytes = baos.toByteArray();
ObjectMetadata objectMetadata = new ObjectMetadata();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.dongnebook.domain.model.image.infrastructure;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

import javax.imageio.ImageIO;

import org.apache.tomcat.util.http.fileupload.ByteArrayOutputStream;
import org.springframework.stereotype.Component;

@Component
public class DefaultImageIOService implements ImageIOService {
@Override
public BufferedImage read(InputStream inputStream) throws IOException {
return ImageIO.read(inputStream);
}

@Override
public void write(BufferedImage resizedImage, String jpg, ByteArrayOutputStream baos) throws IOException {
ImageIO.write(resizedImage, jpg, baos);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.dongnebook.domain.model.image.infrastructure;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

import org.apache.tomcat.util.http.fileupload.ByteArrayOutputStream;

public interface ImageIOService {
BufferedImage read(InputStream inputStream) throws IOException;

void write(BufferedImage resizedImage, String jpg, ByteArrayOutputStream baos) throws IOException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package com.dongnebook.domain.model.image.infrastructure;

import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;

import org.apache.tomcat.util.http.fileupload.ByteArrayOutputStream;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class TestImageIOService implements ImageIOService{
@Override
public BufferedImage read(InputStream inputStream) throws IOException {
return new BufferedImage(200,200 ,BufferedImage.TYPE_INT_RGB );
}

@Override
public void write(BufferedImage resizedImage, String jpg, ByteArrayOutputStream baos) throws IOException {
log.info("do Nothing");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dongnebook.domain.model.image.infrastructure.AwsS3Service;
import com.dongnebook.domain.model.image.application.ImageUploadService;
import com.dongnebook.domain.model.image.dto.ImageUploadRequest;

Expand All @@ -16,7 +15,7 @@
public class ImageUploadController {
private final ImageUploadService awsS3Service;

public ImageUploadController(AwsS3Service awsS3Service) {
public ImageUploadController(ImageUploadService awsS3Service) {
this.awsS3Service = awsS3Service;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
import com.dongnebook.global.security.auth.handler.MemberAuthenticationEntryPoint;
import com.dongnebook.global.security.auth.handler.MemberAuthenticationFailureHandler;
import com.dongnebook.global.security.auth.handler.MemberAuthenticationSuccessHandler;
import com.dongnebook.global.security.auth.oauth.OAuthService;

import lombok.RequiredArgsConstructor;

Expand All @@ -35,7 +34,7 @@
public class SecurityConfig {
private final RefreshTokenRepository refreshTokenRepository;
private final TokenProvider tokenProvider;
private final OAuthService oAuthService;


@Bean
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
Expand All @@ -56,10 +55,7 @@ public SecurityFilterChain filterChain(HttpSecurity http) throws Exception {
.apply(new CustomFilterConfigurer())
.and()
.authorizeHttpRequests(authorize -> authorize
.anyRequest().permitAll())
.oauth2Login() // OAuth2 로그인 설정 시작점
.userInfoEndpoint() // OAuth2 로그인 성공 이후 사용자 정보를 가져올 때 설정 담당
.userService(oAuthService);
.anyRequest().permitAll());
return http.build();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@ public class KaKaoBookInfoFetcher implements BookInfoFetcher<String> {
@Value("${KAKAO_KEY}")
private String kakaoKey;

public KaKaoBookInfoFetcher(String kakaoKey) {
this.kakaoKey = kakaoKey;
}

@Override
public ResponseEntity<String> getBookInfo(String bookTitle) {
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
package com.dongnebook.domain.chat.controller;

import static org.mockito.BDDMockito.*;

import java.time.LocalDateTime;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.listener.ChannelTopic;

import com.dongnebook.domain.chat.domain.RedisChat;
import com.dongnebook.domain.chat.ui.RedisPublisher;

@ExtendWith(MockitoExtension.class)
class RedisPublisherTest {
@InjectMocks
private RedisPublisher redisPublisher;

@Mock
private RedisTemplate<String, Object> redisTemplate;

@Test
@DisplayName("레디스로 채팅을 보낸다.")
void publish(){
// Given
Long roomId = 1L;
Long senderId = 2L;
String message = "hello";
LocalDateTime createdAt = LocalDateTime.now();
RedisChat redisChat = new RedisChat(roomId, senderId, message, createdAt);
doNothing().when(redisTemplate).convertAndSend(anyString(), eq(redisChat));
ChannelTopic topic = ChannelTopic.of("room" + roomId);

// When
redisPublisher.publish(topic,redisChat);

// Then
verify(redisTemplate,times(1)).convertAndSend(String.valueOf(topic),redisChat);
}
}
Loading

0 comments on commit 12281f1

Please sign in to comment.