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

[5기 손가현] Shorten-URL 과제 제출합니다. #72

Open
wants to merge 14 commits into
base: hyun2371
Choose a base branch
from
Open
Prev Previous commit
Next Next commit
[Feat] 서비스 테스트 코드 작성
hyun2371 committed Dec 18, 2023
commit 045ff56197e41bd3318a106bad1bdeecd29d0876
Original file line number Diff line number Diff line change
@@ -32,9 +32,9 @@ public String getShortenUrl(@Valid @ModelAttribute ShortenUrlRequest request, Mo
return "index";
}

@GetMapping("/url/{shortenUrl}")
public String redirectOriginUrl(@PathVariable String shortenUrl) {
String originUrl = urlService.getOriginUrl(shortenUrl);
@GetMapping("/url/{shortUri}")
public String redirectOriginUrl(@PathVariable String shortUri) {
String originUrl = urlService.getOriginUrl(shortUri);
return "redirect:" + originUrl;
}
}
Original file line number Diff line number Diff line change
@@ -31,8 +31,8 @@ public ShortenUrlResponse getShortUrl(ShortenUrlRequest request) {
}

@Transactional(readOnly = true)
public String getOriginUrl(String shortenUrl) {
Long urlId = Base62Algorithm.decode(shortenUrl);
public String getOriginUrl(String shortUri) {
Long urlId = Base62Algorithm.decode(shortUri);
Url url = urlRepository.findById(urlId).orElseThrow(() -> {
throw new CustomException("존재하지 않는 URL입니다.");
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.prgrms.url_shortener.service;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;

import com.prgrms.url_shortener.algorithm.Base62Algorithm;
import com.prgrms.url_shortener.dto.ShortenUrlRequest;
import com.prgrms.url_shortener.dto.ShortenUrlResponse;
import com.prgrms.url_shortener.entity.Url;
import com.prgrms.url_shortener.repository.UrlRepository;
import java.util.Optional;
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.test.util.ReflectionTestUtils;


@ExtendWith(MockitoExtension.class)
class UrlServiceTest {
@Mock
private UrlRepository repository;

@InjectMocks
private UrlService service;

@DisplayName("Base62인코딩으로 shortenUrl을 생성할 수 있다.")
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

테스트 작성해주신 부분은 좋지만 추가되면 좋을 부분이 있습니다.
테스트만 봤을 때는 요구사항을 만족하는지 알 수가 없습니다.

  1. 8글자 이내로 생성되었는지?
  2. 요청 수가 저장되었는지?

@Test
void create_url() {
//given
String originUrl = "https://daum.net";
ShortenUrlRequest request = new ShortenUrlRequest(originUrl);

Url url = new Url(originUrl);
ReflectionTestUtils.setField(url, "id", 1L);

when(repository.existsByOriginUrl(originUrl)).thenReturn(false);
when(repository.save(any(Url.class))).thenReturn(url);
String shortUri = Base62Algorithm.encode(1L);

//when
ShortenUrlResponse response = service.getShortUrl(request);

//then
assertThat(response.shortenUrl()).endsWith(shortUri);
}


@DisplayName("shortUri을 디코딩하여 DB에 저장된 originUrl을 가져올 수 있다.")
@Test
void get_origin_url() {
//given
String expectedOriginUrl = "https://daum.net";
Url url = new Url(expectedOriginUrl);
ReflectionTestUtils.setField(url, "id", 1L);

String shortUri = Base62Algorithm.encode(1L);

when(repository.findById(any(Long.class))).thenReturn(Optional.of(url));

//when
String actualOriginUrl = service.getOriginUrl(shortUri);

//then
assertThat(actualOriginUrl).isEqualTo(expectedOriginUrl);
}
}