#31 loan: get counsel

This commit is contained in:
haerong22
2023-01-11 00:46:52 +09:00
parent 3917ffb7ba
commit 434fe64941
4 changed files with 51 additions and 8 deletions

View File

@@ -4,10 +4,7 @@ import com.example.loan.dto.CounselDto;
import com.example.loan.dto.ResponseDto;
import com.example.loan.service.CounselService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;
@RestController
@RequiredArgsConstructor
@@ -21,4 +18,9 @@ public class CounselController extends AbstractController {
return ok(counselService.create(request));
}
@GetMapping("/{counselId}")
public ResponseDto<CounselDto.Response> get(@PathVariable Long counselId) {
return ok(counselService.get(counselId));
}
}

View File

@@ -5,4 +5,6 @@ import com.example.loan.dto.CounselDto;
public interface CounselService {
CounselDto.Response create(CounselDto.Request request);
CounselDto.Response get(Long counselId);
}

View File

@@ -2,6 +2,8 @@ package com.example.loan.service;
import com.example.loan.domain.Counsel;
import com.example.loan.dto.CounselDto;
import com.example.loan.exception.BaseException;
import com.example.loan.exception.ResultType;
import com.example.loan.repository.CounselRepository;
import lombok.RequiredArgsConstructor;
import org.modelmapper.ModelMapper;
@@ -25,4 +27,12 @@ public class CounselServiceImpl implements CounselService {
Counsel created = counselRepository.save(counsel);
return modelMapper.map(created, CounselDto.Response.class);
}
@Override
public CounselDto.Response get(Long counselId) {
Counsel counsel = counselRepository.findById(counselId)
.orElseThrow(() -> new BaseException(ResultType.SYSTEM_ERROR));
return modelMapper.map(counsel, CounselDto.Response.class);
}
}

View File

@@ -1,12 +1,10 @@
package com.example.loan.service;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.when;
import com.example.loan.domain.Counsel;
import com.example.loan.dto.CounselDto;
import com.example.loan.exception.BaseException;
import com.example.loan.exception.ResultType;
import com.example.loan.repository.CounselRepository;
import org.assertj.core.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentMatchers;
@@ -16,6 +14,12 @@ import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension;
import org.modelmapper.ModelMapper;
import java.util.Optional;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.Mockito.when;
@ExtendWith(MockitoExtension.class)
class CounselServiceTest {
@@ -57,4 +61,29 @@ class CounselServiceTest {
assertThat(actual.getName()).isSameAs(entity.getName());
}
@Test
void Should_ReturnResponseOfExistCounselEntity_When_RequestExistCounselId() {
Long findId = 1L;
Counsel entity = Counsel.builder()
.counselId(1L)
.build();
when(counselRepository.findById(findId)).thenReturn(Optional.ofNullable(entity));
CounselDto.Response actual = counselService.get(findId);
assertThat(actual.getCounselId()).isSameAs(findId);
}
@Test
void Should_ThrowException_When_RequestNotExistCounselId() {
Long findId = 2L;
when(counselRepository.findById(findId)).thenThrow(new BaseException(ResultType.SYSTEM_ERROR));
assertThatThrownBy(() -> counselService.get(findId))
.isInstanceOf(BaseException.class);
}
}