#26 pharmacy: jpa dirty checking test

This commit is contained in:
haerong22
2022-12-14 00:41:27 +09:00
parent 171fb33296
commit 6d1307f2ae
3 changed files with 106 additions and 0 deletions

View File

@@ -25,4 +25,8 @@ public class Pharmacy {
private String pharmacyAddress;
private double latitude;
private double longitude;
public void changePharmacyAddress(String address) {
this.pharmacyAddress = address;
}
}

View File

@@ -0,0 +1,41 @@
package com.example.road.pharmacy.service;
import com.example.road.pharmacy.entity.Pharmacy;
import com.example.road.pharmacy.repository.PharmacyRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.Objects;
@Slf4j
@Service
@RequiredArgsConstructor
public class PharmacyRepositoryService {
private final PharmacyRepository pharmacyRepository;
@Transactional
public void updateAddress(Long id, String address) {
Pharmacy entity = pharmacyRepository.findById(id).orElse(null);
if (Objects.isNull(entity)) {
log.info("[PharmacyRepositoryService updateAddress] not found id: {}", id);
return;
}
entity.changePharmacyAddress(address);
}
public void updateAddressWithoutTransactional(Long id, String address) {
Pharmacy entity = pharmacyRepository.findById(id).orElse(null);
if (Objects.isNull(entity)) {
log.info("[PharmacyRepositoryService updateAddress] not found id: {}", id);
return;
}
entity.changePharmacyAddress(address);
}
}

View File

@@ -0,0 +1,61 @@
package com.example.road.pharmacy.service
import com.example.road.AbstractIntegrationContainerBaseTest
import com.example.road.pharmacy.entity.Pharmacy
import com.example.road.pharmacy.repository.PharmacyRepository
import org.springframework.beans.factory.annotation.Autowired
class PharmacyRepositoryServiceTest extends AbstractIntegrationContainerBaseTest {
@Autowired
private PharmacyRepositoryService pharmacyRepositoryService;
@Autowired
private PharmacyRepository pharmacyRepository;
def setup() {
pharmacyRepository.deleteAll();
}
def "PharmacyRepository update - dirty checking success"() {
given:
String inputAddress = "서울 특별시 성북구 종암동";
String modifiedAddress = "서울 광진구 구의동";
String name = "은혜 약국";
def pharmacy = Pharmacy.builder()
.pharmacyAddress(inputAddress)
.pharmacyName(name)
.build();
when:
def entity = pharmacyRepository.save(pharmacy);
pharmacyRepositoryService.updateAddress(entity.getId(), modifiedAddress);
def result = pharmacyRepository.findAll();
then:
result.get(0).getPharmacyAddress() == modifiedAddress;
}
def "PharmacyRepository update - dirty checking fail"() {
given:
String inputAddress = "서울 특별시 성북구 종암동";
String modifiedAddress = "서울 광진구 구의동";
String name = "은혜 약국";
def pharmacy = Pharmacy.builder()
.pharmacyAddress(inputAddress)
.pharmacyName(name)
.build();
when:
def entity = pharmacyRepository.save(pharmacy);
pharmacyRepositoryService.updateAddressWithoutTransactional(entity.getId(), modifiedAddress);
def result = pharmacyRepository.findAll();
then:
result.get(0).getPharmacyAddress() == inputAddress;
}
}