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

feature/#122 연구실 테스트 코드 작성 #124

Merged
merged 8 commits into from
Dec 3, 2024
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public class LabService {
@Transactional
public LabPersistResponse createLab(LabRequest request) {
Lab lab = Lab.create(request.name(), request.loc(), request.site());
labRepository.save(lab);
Lab createLab = labRepository.save(lab);

return LabPersistResponse.of(lab.getId());
return LabPersistResponse.of(createLab.getId());
}

@Transactional(readOnly = true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.Pattern;
import jakarta.validation.constraints.Size;
import lombok.Builder;

@Builder
public record LabRequest(
@Schema(description = "연구실 이름", example = "인공지능연구실", requiredMode = REQUIRED)
@NotBlank
Expand Down
128 changes: 128 additions & 0 deletions aics-api/src/testFixtures/java/lab/application/LabServiceTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
package lab.application;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import kgu.developers.api.lab.application.LabService;
import kgu.developers.api.lab.presentation.exception.LabNotFoundException;
import kgu.developers.api.lab.presentation.request.LabRequest;
import kgu.developers.api.lab.presentation.response.LabDetailResponse;
import kgu.developers.api.lab.presentation.response.LabListResponse;
import kgu.developers.api.lab.presentation.response.LabPersistResponse;
import kgu.developers.domain.lab.domain.Lab;
import mock.FakeLabRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

import java.util.List;

public class LabServiceTest {
private LabService labService;

@BeforeEach
public void init() {
FakeLabRepository fakeLabRepository = new FakeLabRepository();
this.labService = new LabService(fakeLabRepository);

fakeLabRepository.save(Lab.builder()
.name("Lab A")
.loc("8500")
.site("http://lab1.kyonggi.ac.kr")
.build()
);

fakeLabRepository.save(Lab.builder()
.name("Lab B")
.loc("8520")
.site("https://lab2.kyonggi.ac.kr")
.build()
);
}

@Test
@DisplayName("createLab은 Lab을 생성할 수 있다")
public void createLab_Success() {
//given
LabRequest request = LabRequest.builder()
.name("Lab C")
.loc("제2공학관 200")
.site("http://lab3.kyonggi.ac.kr")
.build();

//when
LabPersistResponse response = labService.createLab(request);

//then
assertNotNull(response);
assertEquals(3, response.id());
}
LeeShinHaeng marked this conversation as resolved.
Show resolved Hide resolved

@Test
@DisplayName("getLabs은 Lab 리스트를 조회할 수 있다")
public void getLabs_Success() {
//given
//when
LabListResponse labs = labService.getLabs();
List<LabDetailResponse> contents = labs.contents();

//then
assertEquals(2, contents.size());
assertEquals("Lab A", contents.get(0).name());
assertEquals("Lab B", contents.get(1).name());
}

@Test
@DisplayName("getById로 존재하지 않는 ID로 조회시 LabNotFoundException을 발생시킨다")
public void getById_Throws_LabNotFoundException() {
//given
Long id = 0L;

//when - getById는 private 이기 때문에 delete를 이용해 접근
//then
assertThatThrownBy(
() -> labService.deleteLab(id)
).isInstanceOf(LabNotFoundException.class);
}
LeeShinHaeng marked this conversation as resolved.
Show resolved Hide resolved


@Test
@DisplayName("updateLab은 Lab을 수정할 수 있다")
public void updateLab_Success() {
//given
Long id = 1L;
String newName = "Lab C";
String newLoc = "제2공학관 200";
String newSite = "https://lab3.kyonggi.ac.kr";
LabRequest request = LabRequest.builder()
.name(newName)
.loc(newLoc)
.site(newSite)
.build();

//when
labService.updateLab(id, request);
List<LabDetailResponse> contents = labService.getLabs().contents();

//then - 이름순이기 때문에 순서까지 바뀌어서 리턴
assertEquals(newName, contents.get(1).name());
assertEquals(newLoc, contents.get(1).loc());
assertEquals(newSite, contents.get(1).site());
}

@Test
@DisplayName("deleteLab은 Lab을 삭제할 수 있다")
public void deleteLab_Success() {
//given
Long id = 1L;

//when
labService.deleteLab(id);
List<LabDetailResponse> contents = labService.getLabs().contents();

//then
assertEquals(1, contents.size());
assertEquals("Lab B", contents.get(0).name());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package lab.presentation;

public class LabControllerTest {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package lab.application;

public class LabServiceTest {
}
84 changes: 84 additions & 0 deletions aics-domain/src/testFixtures/java/lab/domain/LabDomainTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package lab.domain;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

import kgu.developers.domain.lab.domain.Lab;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;

public class LabDomainTest {
@Test
@DisplayName("LAB 객체를 생성할 수 있다")
public void createLab_Success() {
//given
String name = "Lab A";
String loc = "8500";
String site = "http://lab1.kyonggi.ac.kr";

//when
Lab lab = Lab.create(name, loc, site);

//then
assertNotNull(lab);
assertEquals(name, lab.getName());
assertEquals(loc, lab.getLoc());
assertEquals(site, lab.getSite());
}


@Test
@DisplayName("LAB 얀구실명 수정할 수 있다")
public void updateName_Success() {
//given
String name = "Lab A";
String loc = "8500";
String site = "http://lab1.kyonggi.ac.kr";
Lab lab = Lab.create(name, loc, site);

String newName = "Updated Lab A";

//when
lab.updateName(newName);

//then
assertEquals(newName, lab.getName());
}

@Test
@DisplayName("LAB 위치를 수정할 수 있다")
public void updateLoc_Success() {
//given
String name = "Lab A";
String loc = "8500";
String site = "http://lab1.kyonggi.ac.kr";
Lab lab = Lab.create(name, loc, site);

String newLoc = "8601";

//when
lab.updateLoc(newLoc);

//then
assertEquals(newLoc, lab.getLoc());
}

@Test
@DisplayName("LAB 사이트를 수정할 수 있다")
public void updateSite_Success() {
//given
String name = "Lab A";
String loc = "8500";
String site = "http://lab1.kyonggi.ac.kr";
Lab lab = Lab.create(name, loc, site);

String newSite = "http://new.kyonggi.ac.kr";

//when
lab.updateSite(newSite);

//then
assertEquals(newSite, lab.getSite());
}
LeeShinHaeng marked this conversation as resolved.
Show resolved Hide resolved

}
57 changes: 57 additions & 0 deletions aics-domain/src/testFixtures/java/mock/FakeLabRepository.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package mock;

import kgu.developers.domain.lab.domain.Lab;
import kgu.developers.domain.lab.domain.LabRepository;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
import java.util.Optional;
import java.util.concurrent.atomic.AtomicLong;

public class FakeLabRepository implements LabRepository {
private final List<Lab> data = Collections.synchronizedList(new ArrayList<>());
private final AtomicLong autoGeneratedId = new AtomicLong(0);

@Override
public Lab save(Lab lab) {
if (lab == null) {
throw new IllegalArgumentException("Lab은 null일 수 없습니다.");
}
if (lab.getName() == null
|| lab.getLoc() == null
|| lab.getSite() == null) {
throw new IllegalArgumentException("이름, 위치, 사이트는 필수 입력값입니다.");
}

Lab newLab = Lab.builder()
.id(autoGeneratedId.incrementAndGet())
.name(lab.getName())
.loc(lab.getLoc())
.site(lab.getSite())
.build();

data.add(newLab);
return newLab;
}
LeeShinHaeng marked this conversation as resolved.
Show resolved Hide resolved

@Override
public Optional<Lab> findById(Long id) {
return data.stream()
.filter(lab -> lab.getId().equals(id))
.findFirst();
}

@Override
public List<Lab> findAllByOrderByName() {
return data.stream()
.sorted(Comparator.comparing(Lab::getName))
.toList();
}

@Override
public void delete(Lab lab) {
data.remove(lab);
}
}
LeeShinHaeng marked this conversation as resolved.
Show resolved Hide resolved
Loading