Skip to content
Snippets Groups Projects
Commit 6260c38e authored by OZGCloud's avatar OZGCloud
Browse files

Merge branch 'master' into OZG-6710-update-common-lib

# Conflicts:
#	zufi-manager-interface/pom.xml
parents 00f9e98c 21eda9ce
No related branches found
No related tags found
No related merge requests found
Showing
with 277 additions and 37 deletions
......@@ -3,6 +3,8 @@ package de.ozgcloud.zufi.fachstelle;
import java.util.List;
import de.ozgcloud.zufi.grpc.fachstelle.FachstelleServiceGrpc.FachstelleServiceImplBase;
import de.ozgcloud.zufi.grpc.fachstelle.GrpcFachstelleGetRequest;
import de.ozgcloud.zufi.grpc.fachstelle.GrpcFachstelleGetResponse;
import de.ozgcloud.zufi.grpc.fachstelle.GrpcFachstelleSearchRequest;
import de.ozgcloud.zufi.grpc.fachstelle.GrpcFachstelleSearchResponse;
import io.grpc.stub.StreamObserver;
......@@ -19,13 +21,25 @@ class FachstelleGrpcService extends FachstelleServiceImplBase {
@Override
public void search(GrpcFachstelleSearchRequest request, StreamObserver<GrpcFachstelleSearchResponse> responseObserver) {
var fachstellen = searchService.search(request.getSearchBy());
responseObserver.onNext(buildResponse(fachstellen));
responseObserver.onNext(buildFachstelleSearchResponse(fachstellen));
responseObserver.onCompleted();
}
GrpcFachstelleSearchResponse buildResponse(List<Fachstelle> fachstellen) {
GrpcFachstelleSearchResponse buildFachstelleSearchResponse(List<Fachstelle> fachstellen) {
var responseBuilder = GrpcFachstelleSearchResponse.newBuilder();
fachstellen.stream().map(mapper::toGrpc).forEach(responseBuilder::addFachstellen);
return responseBuilder.build();
}
public void getById(GrpcFachstelleGetRequest request, StreamObserver<GrpcFachstelleGetResponse> responseObserver) {
var fachstelle = searchService.getById(request.getId());
responseObserver.onNext(buildFachstelleGetResponse(fachstelle));
responseObserver.onCompleted();
}
GrpcFachstelleGetResponse buildFachstelleGetResponse(Fachstelle fachstelle) {
var responseBuilder = GrpcFachstelleGetResponse.newBuilder();
responseBuilder.setFachstelle(mapper.toGrpc(fachstelle));
return responseBuilder.build();
}
}
......@@ -4,11 +4,14 @@ import org.mapstruct.Mapper;
import org.mapstruct.NullValueCheckStrategy;
import de.ozgcloud.zufi.grpc.fachstelle.GrpcFachstelle;
import de.ozgcloud.zufi.grpc.fachstelle.GrpcFachstelleRegistrationRequest;
@Mapper(nullValueCheckStrategy = NullValueCheckStrategy.ALWAYS)
interface FachstelleMapper {
Fachstelle fromGrpc(GrpcFachstelle grpcFachstelle);
Fachstelle fromGrpcRegistrationRequest(GrpcFachstelleRegistrationRequest grpcRegistrationRequest);
GrpcFachstelle toGrpc(Fachstelle fachstelle);
}
package de.ozgcloud.zufi.fachstelle;
import de.ozgcloud.zufi.common.errorhandling.ResourceNotFoundException;
public class FachstelleNotFoundException extends ResourceNotFoundException {
public static final String RESOURCE_NAME = "Fachstelle";
public FachstelleNotFoundException(String id) {
super(id, RESOURCE_NAME);
}
}
......@@ -16,7 +16,7 @@ class FachstelleRegistrationGrpcService extends FachstelleRegistrationServiceImp
@Override
public void register(GrpcFachstelleRegistrationRequest request, StreamObserver<GrpcFachstelleRegistrationResponse> responseObserver) {
fachstelleRegistrationService.register(fachstelleMapper.fromGrpc(request.getFachstelle()));
fachstelleRegistrationService.register(fachstelleMapper.fromGrpcRegistrationRequest(request));
responseObserver.onNext(GrpcFachstelleRegistrationResponse.getDefaultInstance());
responseObserver.onCompleted();
}
......
......@@ -14,6 +14,10 @@ class FachstelleSearchService {
private final FachstelleRepository repository;
public Fachstelle getById(String id) {
return repository.findById(id).orElseThrow(() -> new FachstelleNotFoundException(id));
}
public List<Fachstelle> search(String text) {
if (StringUtils.isBlank(text)) {
return Collections.emptyList();
......
......@@ -2,6 +2,8 @@ package de.ozgcloud.zufi.organisationseinheit;
import java.util.List;
import de.ozgcloud.zufi.grpc.organisationseinheit.GrpcGetByOrganisationsEinheitIdRequest;
import de.ozgcloud.zufi.grpc.organisationseinheit.GrpcGetByOrganisationsEinheitIdResponse;
import de.ozgcloud.zufi.grpc.organisationseinheit.GrpcOrganisationsEinheitGetRequest;
import de.ozgcloud.zufi.grpc.organisationseinheit.GrpcOrganisationsEinheitGetResponse;
import de.ozgcloud.zufi.grpc.organisationseinheit.GrpcOrganisationsEinheitSearchRequest;
......@@ -48,4 +50,19 @@ class OrganisationsEinheitGrpcService extends OrganisationsEinheitServiceImplBas
return responseBuilder.build();
}
@Override
public void getByOrganisationsEinheitId(GrpcGetByOrganisationsEinheitIdRequest request,
StreamObserver<GrpcGetByOrganisationsEinheitIdResponse> responseObserver) {
var organisationsEinheiten = searchService.getByOrganisationsEinheitId(request.getOrganisationsEinheitId());
responseObserver.onNext(buildGetByOrganisationsEinheitIdResponse(organisationsEinheiten));
responseObserver.onCompleted();
}
GrpcGetByOrganisationsEinheitIdResponse buildGetByOrganisationsEinheitIdResponse(List<OrganisationsEinheit> organisationsEinheiten) {
var responseBuilder = GrpcGetByOrganisationsEinheitIdResponse.newBuilder();
organisationsEinheiten.stream()
.map(mapper::fromOrganisationsEinheit)
.forEach(responseBuilder::addOrganisationsEinheiten);;
return responseBuilder.build();
}
}
package de.ozgcloud.zufi.organisationseinheit;
import de.ozgcloud.zufi.common.errorhandling.ResourceNotFoundException;
public class OrganisationsEinheitNotFoundException extends ResourceNotFoundException {
public static final String RESOURCE_NAME = "Organisationseinheit";
public OrganisationsEinheitNotFoundException(String id) {
super(id, RESOURCE_NAME);
}
}
......@@ -5,7 +5,6 @@ import java.util.List;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import de.ozgcloud.zufi.common.errorhandling.OrganisationsEinheitNotFoundException;
import lombok.RequiredArgsConstructor;
@Service
......@@ -18,6 +17,10 @@ class OrganisationsEinheitSearchService {
return repository.findById(id).orElseThrow(() -> new OrganisationsEinheitNotFoundException(id));
}
public List<OrganisationsEinheit> getByOrganisationsEinheitId(String organisationsEinheitId) {
return repository.findByOrganisationsEinheitId(organisationsEinheitId);
}
public List<OrganisationsEinheit> search(String text, String ars) {
if (StringUtils.isEmpty(ars)) {
return repository.findAllContaining(text);
......
......@@ -26,7 +26,6 @@ package de.ozgcloud.zufi.organisationseinheit;
import java.util.List;
import de.ozgcloud.zufi.common.errorhandling.MissingRequiredGrpcArgumentException;
import de.ozgcloud.zufi.common.errorhandling.OrganisationsEinheitNotFoundException;
import de.ozgcloud.zufi.grpc.organisationseinheit.GrpcVorgangManagerGetAddressRequest;
import de.ozgcloud.zufi.grpc.organisationseinheit.GrpcVorgangManagerGetAddressResponse;
import de.ozgcloud.zufi.grpc.organisationseinheit.GrpcVorgangManagerRegistrationRequest;
......
......@@ -2,7 +2,6 @@ logging:
level:
root: WARN,
'[de.ozgcloud]': INFO
'[io.grpc]': DEBUG
config: classpath:log4j2-local.xml
server:
......
......@@ -2,7 +2,6 @@ logging:
level:
ROOT: WARN
'[de.ozgcloud]': INFO
'[io.grpc]': DEBUG
spring:
application:
......
......@@ -47,6 +47,7 @@ import jakarta.validation.ConstraintViolationException;
class GrpcExceptionHandlersTest {
private static final String LOG_MESSAGE = LoremIpsum.getInstance().getWords(10);
@Spy
@InjectMocks
private GrpcExceptionHandlers handler;
......@@ -125,9 +126,11 @@ class GrpcExceptionHandlersTest {
}
@Nested
class TestHandleOrganisationsEinheitNotFoundException {
class TestHandleResourceNotFoundException {
private static final String RESOURCE_ID = UUID.randomUUID().toString();
private static final String RESOURCE_NAME = LoremIpsum.getInstance().getWords(1);
private final OrganisationsEinheitNotFoundException exception = new OrganisationsEinheitNotFoundException(LOG_MESSAGE,
private final ResourceNotFoundException exception = new ResourceNotFoundException(RESOURCE_ID, RESOURCE_NAME,
new RuntimeException());
@Test
......@@ -141,7 +144,7 @@ class GrpcExceptionHandlersTest {
void shouldHaveDescription() {
var status = handleException().getStatus();
assertThat(status.getDescription()).contains(LOG_MESSAGE);
assertThat(status.getDescription()).isEqualTo(exception.getMessage());
}
@Test
......@@ -152,7 +155,7 @@ class GrpcExceptionHandlersTest {
}
private StatusException handleException() {
return handler.handleOrganisationsEinheitNotFoundException(exception);
return handler.handleResourceNotFoundException(exception);
}
}
......
package de.ozgcloud.zufi.common.errorhandling;
import static org.assertj.core.api.Assertions.*;
import java.util.UUID;
import org.junit.jupiter.api.Test;
import com.thedeanda.lorem.LoremIpsum;
class ResourceNotFoundExceptionTest {
private static final String RESOURCE_ID = UUID.randomUUID().toString();
private static final String RESOURCE_NAME = LoremIpsum.getInstance().getWords(1);
@Test
void shouldHaveMessage() {
var exception = new ResourceNotFoundException(RESOURCE_ID, RESOURCE_NAME);
assertThat(exception.getMessage()).startsWith(RESOURCE_NAME + " with ID " + RESOURCE_ID + " could not be found");
}
}
......@@ -13,6 +13,7 @@ import org.mockito.Mock;
import org.mockito.Spy;
import de.ozgcloud.zufi.grpc.fachstelle.GrpcFachstelle;
import de.ozgcloud.zufi.grpc.fachstelle.GrpcFachstelleGetResponse;
import de.ozgcloud.zufi.grpc.fachstelle.GrpcFachstelleSearchResponse;
import io.grpc.stub.StreamObserver;
......@@ -26,25 +27,26 @@ class FachstelleGrpcServiceTest {
@Mock
private FachstelleMapper mapper;
private final Fachstelle fachstelle1 = FachstelleTestFactory.create();
private final Fachstelle fachstelle = FachstelleTestFactory.create();
private final Fachstelle fachstelle2 = FachstelleTestFactory.create();
private final List<Fachstelle> fachstellen = List.of(fachstelle1, fachstelle2);
private final List<Fachstelle> fachstellen = List.of(fachstelle, fachstelle2);
private final GrpcFachstelle grpcFachstelle1 = GrpcFachstelleTestFactory.createBuilder().setMukId("id-1").build();
private final GrpcFachstelle grpcFachstelle = GrpcFachstelleTestFactory.createBuilder().setMukId("id-1").build();
private final GrpcFachstelle grpcFachstelle2 = GrpcFachstelleTestFactory.createBuilder().setMukId("id-2").build();
private final List<GrpcFachstelle> grpcFachstellen = List.of(grpcFachstelle1, grpcFachstelle2);
private final List<GrpcFachstelle> grpcFachstellen = List.of(grpcFachstelle, grpcFachstelle2);
@Nested
class TestSearch {
@Mock
private StreamObserver<GrpcFachstelleSearchResponse> responseObserver;
private final GrpcFachstelleSearchResponse searchResponse = GrpcFachstelleSearchResponseTestFactory.createBuilder().addAllFachstellen(grpcFachstellen).build();
private final GrpcFachstelleSearchResponse searchResponse = GrpcFachstelleSearchResponseTestFactory.createBuilder()
.addAllFachstellen(grpcFachstellen).build();
@BeforeEach
void init() {
when(searchService.search(GrpcFachstelleSearchRequestTestFactory.SEARCH_BY)).thenReturn(fachstellen);
doReturn(searchResponse).when(grpcService).buildResponse(fachstellen);
doReturn(searchResponse).when(grpcService).buildFachstelleSearchResponse(fachstellen);
}
@Test
......@@ -58,7 +60,7 @@ class FachstelleGrpcServiceTest {
void shouldBuildResponse() {
callGrpcService();
verify(grpcService).buildResponse(fachstellen);
verify(grpcService).buildFachstelleSearchResponse(fachstellen);
}
@Test
......@@ -81,26 +83,99 @@ class FachstelleGrpcServiceTest {
}
@Nested
class TestBuildResponse {
class TestBuildFachstelleSearchResponse {
@BeforeEach
void init() {
when(mapper.toGrpc(fachstelle1)).thenReturn(grpcFachstelle1);
when(mapper.toGrpc(fachstelle)).thenReturn(grpcFachstelle);
when(mapper.toGrpc(fachstelle2)).thenReturn(grpcFachstelle2);
}
@Test
void shouldCallMapper() {
grpcService.buildResponse(fachstellen);
grpcService.buildFachstelleSearchResponse(fachstellen);
fachstellen.forEach(fachstelle -> verify(mapper).toGrpc(fachstelle));
}
@Test
void shouldAddFachstellenToResponse() {
var response = grpcService.buildResponse(fachstellen);
var response = grpcService.buildFachstelleSearchResponse(fachstellen);
assertThat(response.getFachstellenList()).containsExactlyElementsOf(grpcFachstellen);
}
}
@Nested
class TestGetById {
private final GrpcFachstelleGetResponse response = GrpcFachstelleGetResponseTestFactory.create();
@Mock
private StreamObserver<GrpcFachstelleGetResponse> responseObserver;
@BeforeEach
void init() {
when(searchService.getById(FachstelleTestFactory.ID)).thenReturn(fachstelle);
doReturn(response).when(grpcService).buildFachstelleGetResponse(fachstelle);
}
@Test
void shouldCalSearchService() {
callService();
verify(searchService).getById(FachstelleTestFactory.ID);
}
@Test
void shouldBuildResponse() {
callService();
verify(grpcService).buildFachstelleGetResponse(fachstelle);
}
@Test
void shouldNotifyObserver() {
callService();
verify(responseObserver).onNext(response);
}
@Test
void shouldCompleteResponse() {
callService();
verify(responseObserver).onCompleted();
}
private void callService() {
grpcService.getById(GrpcFachstelleGetRequestTestFactory.create(), responseObserver);
}
}
@Nested
class TestBuildFachstelleGetResponse {
@BeforeEach
void init() {
when(mapper.toGrpc(fachstelle)).thenReturn(grpcFachstelle);
}
@Test
void shouldMapFachstelle() {
callService();
verify(mapper).toGrpc(fachstelle);
}
@Test
void shouldReturnResponse() {
var result = callService();
assertThat(result.getFachstelle()).isEqualTo(grpcFachstelle);
}
private GrpcFachstelleGetResponse callService() {
return grpcService.buildFachstelleGetResponse(fachstelle);
}
}
}
......@@ -17,6 +17,17 @@ class FachstelleMapperTest {
void shouldMapAllSourceFields() {
var mapped = mapper.fromGrpc(GrpcFachstelleTestFactory.create());
assertThat(mapped).usingRecursiveComparison().isEqualTo(FachstelleTestFactory.create());
}
}
@Nested
class TestFromRegistrationGrpc {
@Test
void shouldMapAllSourceFields() {
var mapped = mapper.fromGrpcRegistrationRequest(GrpcFachstelleRegistrationRequestTestFactory.create());
assertThat(mapped).usingRecursiveComparison().isEqualTo(FachstelleTestFactory.createWithoutId());
}
}
......
......@@ -42,11 +42,8 @@ class FachstelleRegistrationGrpcServiceITCase {
@Nested
class TestRegister {
private final GrpcFachstelleRegistrationRequest validRequest = GrpcFachstelleRegistrationRequest.newBuilder()
.setFachstelle(GrpcFachstelleTestFactory.create()).build();
private final GrpcFachstelleRegistrationRequest invalidRequestMissingMukId = GrpcFachstelleRegistrationRequest.newBuilder()
.setFachstelle(GrpcFachstelleTestFactory.createWithoutMukId()).build();
private final GrpcFachstelleRegistrationRequest invalidRequestMissingFachstelle = GrpcFachstelleRegistrationRequest.newBuilder().build();
private final GrpcFachstelleRegistrationRequest validRequest = GrpcFachstelleRegistrationRequestTestFactory.create();
private final GrpcFachstelleRegistrationRequest invalidRequestMissingMukId = GrpcFachstelleRegistrationRequestTestFactory.createWithoutMukId();
@Test
void shouldPersistFachstelle() {
......@@ -64,11 +61,5 @@ class FachstelleRegistrationGrpcServiceITCase {
assertThatThrownBy(() -> serviceStub.register(invalidRequestMissingMukId)).isInstanceOf(StatusRuntimeException.class).hasMessageContainingAll(
Code.forNumber(Code.INVALID_ARGUMENT_VALUE).toString(), "mukId");
}
@Test
void shouldThrowExceptionOnMissingFachstelle() {
assertThatThrownBy(() -> serviceStub.register(invalidRequestMissingFachstelle)).isInstanceOf(StatusRuntimeException.class).hasMessageContainingAll(
Code.forNumber(Code.INVALID_ARGUMENT_VALUE).toString(), "fachstelle");
}
}
}
package de.ozgcloud.zufi.fachstelle;
import static de.ozgcloud.zufi.fachstelle.GrpcFachstelleRegistrationRequestTestFactory.*;
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.BeforeEach;
......@@ -10,6 +9,7 @@ import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import de.ozgcloud.zufi.grpc.fachstelle.GrpcFachstelleRegistrationRequest;
import de.ozgcloud.zufi.grpc.fachstelle.GrpcFachstelleRegistrationResponse;
import io.grpc.stub.StreamObserver;
......@@ -26,13 +26,14 @@ class FachstelleRegistrationGrpcServiceTest {
@Nested
class TestRegister {
private final GrpcFachstelleRegistrationRequest registrationRequest = GrpcFachstelleRegistrationRequestTestFactory.create();
private final Fachstelle fachstelle = FachstelleTestFactory.createWithoutId();
@Mock
private StreamObserver<GrpcFachstelleRegistrationResponse> responseObserver;
@BeforeEach
void init() {
when(mapper.fromGrpc(GRPC_FACHSTELLE)).thenReturn(fachstelle);
when(mapper.fromGrpcRegistrationRequest(registrationRequest)).thenReturn(fachstelle);
doNothing().when(service).register(fachstelle);
}
......@@ -40,7 +41,7 @@ class FachstelleRegistrationGrpcServiceTest {
void shouldMapFromGrpc() {
callService();
verify(mapper).fromGrpc(GRPC_FACHSTELLE);
verify(mapper).fromGrpcRegistrationRequest(registrationRequest);
}
@Test
......@@ -65,7 +66,7 @@ class FachstelleRegistrationGrpcServiceTest {
}
private void callService() {
grpcService.register(GrpcFachstelleRegistrationRequestTestFactory.create(), responseObserver);
grpcService.register(registrationRequest, responseObserver);
}
}
}
......@@ -4,7 +4,9 @@ import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import java.util.List;
import java.util.Optional;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.params.ParameterizedTest;
......@@ -25,6 +27,53 @@ class FachstelleSearchServiceTest {
@Mock
private FachstelleRepository repository;
@Nested
class TestGetById {
@Nested
class OnFachstelleFound {
private final Fachstelle fachstelle = FachstelleTestFactory.create();
@BeforeEach
void init() {
when(repository.findById(FachstelleTestFactory.ID)).thenReturn(Optional.of(fachstelle));
}
@Test
void shouldFindById() {
callService();
verify(repository).findById(FachstelleTestFactory.ID);
}
@Test
void shouldReturnFachstelle() {
var foundFachstelle = callService();
assertThat(foundFachstelle).isEqualTo(fachstelle);
}
}
@Nested
class OnFachstelleNotFound {
@BeforeEach
void init() {
when(repository.findById(FachstelleTestFactory.ID)).thenReturn(Optional.empty());
}
@Test
void shouldThrowException() {
assertThatThrownBy(() -> callService()).isInstanceOf(FachstelleNotFoundException.class).hasMessageContaining(FachstelleTestFactory.ID);
}
}
private Fachstelle callService() {
return service.getById(FachstelleTestFactory.ID);
}
}
@Nested
class TestSearch {
......
package de.ozgcloud.zufi.fachstelle;
import de.ozgcloud.zufi.grpc.fachstelle.GrpcFachstelleGetRequest;
class GrpcFachstelleGetRequestTestFactory {
public static GrpcFachstelleGetRequest create() {
return createBuilder().build();
}
public static GrpcFachstelleGetRequest.Builder createBuilder() {
return GrpcFachstelleGetRequest.newBuilder().setId(FachstelleTestFactory.ID);
}
}
package de.ozgcloud.zufi.fachstelle;
import de.ozgcloud.zufi.grpc.fachstelle.GrpcFachstelleGetResponse;
class GrpcFachstelleGetResponseTestFactory {
public static GrpcFachstelleGetResponse create() {
return createBuilder().build();
}
public static GrpcFachstelleGetResponse.Builder createBuilder() {
return GrpcFachstelleGetResponse.newBuilder().setFachstelle(GrpcFachstelleTestFactory.create());
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment