Loading harvest-service/src/main/java/cz/muni/fi/pa165/harvestservice/data/repository/GrapeRepository.java +1 −1 Original line number Diff line number Diff line Loading @@ -2,7 +2,7 @@ package cz.muni.fi.pa165.harvestservice.data.repository; import cz.muni.fi.pa165.harvestservice.data.model.Grape; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; // Import this import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; import java.util.List; Loading harvest-service/src/main/java/cz/muni/fi/pa165/harvestservice/service/GrapeService.java +23 −1 Original line number Diff line number Diff line package cz.muni.fi.pa165.harvestservice.service; import cz.muni.fi.pa165.harvestservice.data.model.Grape; import cz.muni.fi.pa165.harvestservice.data.model.GrowthConditions; import cz.muni.fi.pa165.harvestservice.data.repository.GrapeRepository; import cz.muni.fi.pa165.harvestservice.data.repository.GrowthConditionsRepository; import cz.muni.fi.pa165.common.exceptions.ResourceAlreadyExistsException; import cz.muni.fi.pa165.common.exceptions.ResourceNotFoundException; import org.springframework.beans.factory.annotation.Autowired; Loading @@ -14,10 +16,13 @@ import java.util.List; public class GrapeService { private final GrapeRepository grapeRepository; private final GrowthConditionsRepository growthConditionsRepository; @Autowired public GrapeService(GrapeRepository grapeRepository) { public GrapeService(GrapeRepository grapeRepository, GrowthConditionsRepository growthConditionsRepository) { this.grapeRepository = grapeRepository; this.growthConditionsRepository = growthConditionsRepository; } @Transactional Loading @@ -29,6 +34,23 @@ public class GrapeService { throw new ResourceAlreadyExistsException( "A grape with name '" + grape.getName() + "' already exists"); } GrowthConditions incomingGc = grape.getGrowthConditions(); if (incomingGc == null) { throw new IllegalArgumentException("Missing growthConditions block"); } GrowthConditions managedGc; if (incomingGc.getId() == null) { managedGc = growthConditionsRepository.save(incomingGc); } else { managedGc = growthConditionsRepository.findById(incomingGc.getId()) .orElseThrow(() -> new ResourceNotFoundException( "GrowthConditions with ID " + incomingGc.getId() + " was not found")); } grape.setGrowthConditions(managedGc); return grapeRepository.save(grape); } Loading harvest-service/src/test/java/cz/muni/fi/pa165/harvestservice/service/GrapeServiceTests.java +13 −0 Original line number Diff line number Diff line package cz.muni.fi.pa165.harvestservice.service; import cz.muni.fi.pa165.harvestservice.data.model.Grape; import cz.muni.fi.pa165.harvestservice.data.model.GrowthConditions; import cz.muni.fi.pa165.harvestservice.data.repository.GrapeRepository; import cz.muni.fi.pa165.common.exceptions.ResourceAlreadyExistsException; import cz.muni.fi.pa165.common.exceptions.ResourceNotFoundException; import cz.muni.fi.pa165.harvestservice.data.repository.GrowthConditionsRepository; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; Loading @@ -26,6 +28,9 @@ class GrapeServiceTests { @Mock private GrapeRepository grapeRepository; @Mock private GrowthConditionsRepository growthConditionsRepository; @InjectMocks private GrapeService grapeService; Loading Loading @@ -70,11 +75,19 @@ class GrapeServiceTests { void createGrape_valid_savesAndReturns() { Grape g = new Grape(); g.setName("Merlot"); GrowthConditions gc = new GrowthConditions(); g.setGrowthConditions(gc); Mockito.when(grapeRepository.findByName("Merlot")).thenReturn(null); Mockito.when(growthConditionsRepository.save(gc)).thenReturn(gc); Mockito.when(grapeRepository.save(g)).thenReturn(g); Grape result = grapeService.createGrape(g); assertThat(result).isSameAs(g); Mockito.verify(growthConditionsRepository).save(gc); Mockito.verify(grapeRepository).save(g); } @Test Loading production-service/src/main/java/cz/muni/fi/pa165/productionservice/service/WineProductionService.java +1 −4 Original line number Diff line number Diff line package cz.muni.fi.pa165.productionservice.service; import cz.muni.fi.pa165.productionservice.api.GrapeDto; import cz.muni.fi.pa165.productionservice.api.GrapeUsageDto; import cz.muni.fi.pa165.productionservice.data.model.Recipe; import cz.muni.fi.pa165.productionservice.data.model.RecipeGrape; import cz.muni.fi.pa165.productionservice.data.model.Wine; import cz.muni.fi.pa165.common.exceptions.ResourceNotFoundException; import jakarta.transaction.Transactional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.time.Year; import java.util.List; import java.util.stream.Collectors; @Service @Transactional public class WineProductionService { private final HarvestClient harvestClient; Loading Loading @@ -95,4 +91,5 @@ public class WineProductionService { } wine.setYear(newestHarvestYear); } } Loading
harvest-service/src/main/java/cz/muni/fi/pa165/harvestservice/data/repository/GrapeRepository.java +1 −1 Original line number Diff line number Diff line Loading @@ -2,7 +2,7 @@ package cz.muni.fi.pa165.harvestservice.data.repository; import cz.muni.fi.pa165.harvestservice.data.model.Grape; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; // Import this import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; import java.util.List; Loading
harvest-service/src/main/java/cz/muni/fi/pa165/harvestservice/service/GrapeService.java +23 −1 Original line number Diff line number Diff line package cz.muni.fi.pa165.harvestservice.service; import cz.muni.fi.pa165.harvestservice.data.model.Grape; import cz.muni.fi.pa165.harvestservice.data.model.GrowthConditions; import cz.muni.fi.pa165.harvestservice.data.repository.GrapeRepository; import cz.muni.fi.pa165.harvestservice.data.repository.GrowthConditionsRepository; import cz.muni.fi.pa165.common.exceptions.ResourceAlreadyExistsException; import cz.muni.fi.pa165.common.exceptions.ResourceNotFoundException; import org.springframework.beans.factory.annotation.Autowired; Loading @@ -14,10 +16,13 @@ import java.util.List; public class GrapeService { private final GrapeRepository grapeRepository; private final GrowthConditionsRepository growthConditionsRepository; @Autowired public GrapeService(GrapeRepository grapeRepository) { public GrapeService(GrapeRepository grapeRepository, GrowthConditionsRepository growthConditionsRepository) { this.grapeRepository = grapeRepository; this.growthConditionsRepository = growthConditionsRepository; } @Transactional Loading @@ -29,6 +34,23 @@ public class GrapeService { throw new ResourceAlreadyExistsException( "A grape with name '" + grape.getName() + "' already exists"); } GrowthConditions incomingGc = grape.getGrowthConditions(); if (incomingGc == null) { throw new IllegalArgumentException("Missing growthConditions block"); } GrowthConditions managedGc; if (incomingGc.getId() == null) { managedGc = growthConditionsRepository.save(incomingGc); } else { managedGc = growthConditionsRepository.findById(incomingGc.getId()) .orElseThrow(() -> new ResourceNotFoundException( "GrowthConditions with ID " + incomingGc.getId() + " was not found")); } grape.setGrowthConditions(managedGc); return grapeRepository.save(grape); } Loading
harvest-service/src/test/java/cz/muni/fi/pa165/harvestservice/service/GrapeServiceTests.java +13 −0 Original line number Diff line number Diff line package cz.muni.fi.pa165.harvestservice.service; import cz.muni.fi.pa165.harvestservice.data.model.Grape; import cz.muni.fi.pa165.harvestservice.data.model.GrowthConditions; import cz.muni.fi.pa165.harvestservice.data.repository.GrapeRepository; import cz.muni.fi.pa165.common.exceptions.ResourceAlreadyExistsException; import cz.muni.fi.pa165.common.exceptions.ResourceNotFoundException; import cz.muni.fi.pa165.harvestservice.data.repository.GrowthConditionsRepository; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; import org.mockito.InjectMocks; Loading @@ -26,6 +28,9 @@ class GrapeServiceTests { @Mock private GrapeRepository grapeRepository; @Mock private GrowthConditionsRepository growthConditionsRepository; @InjectMocks private GrapeService grapeService; Loading Loading @@ -70,11 +75,19 @@ class GrapeServiceTests { void createGrape_valid_savesAndReturns() { Grape g = new Grape(); g.setName("Merlot"); GrowthConditions gc = new GrowthConditions(); g.setGrowthConditions(gc); Mockito.when(grapeRepository.findByName("Merlot")).thenReturn(null); Mockito.when(growthConditionsRepository.save(gc)).thenReturn(gc); Mockito.when(grapeRepository.save(g)).thenReturn(g); Grape result = grapeService.createGrape(g); assertThat(result).isSameAs(g); Mockito.verify(growthConditionsRepository).save(gc); Mockito.verify(grapeRepository).save(g); } @Test Loading
production-service/src/main/java/cz/muni/fi/pa165/productionservice/service/WineProductionService.java +1 −4 Original line number Diff line number Diff line package cz.muni.fi.pa165.productionservice.service; import cz.muni.fi.pa165.productionservice.api.GrapeDto; import cz.muni.fi.pa165.productionservice.api.GrapeUsageDto; import cz.muni.fi.pa165.productionservice.data.model.Recipe; import cz.muni.fi.pa165.productionservice.data.model.RecipeGrape; import cz.muni.fi.pa165.productionservice.data.model.Wine; import cz.muni.fi.pa165.common.exceptions.ResourceNotFoundException; import jakarta.transaction.Transactional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import java.time.Year; import java.util.List; import java.util.stream.Collectors; @Service @Transactional public class WineProductionService { private final HarvestClient harvestClient; Loading Loading @@ -95,4 +91,5 @@ public class WineProductionService { } wine.setYear(newestHarvestYear); } }