Skip to content
Snippets Groups Projects
Commit 8e5be8c7 authored by OZGCloud's avatar OZGCloud
Browse files

Merge pull request 'OZG-5814: Nacharbeiten' (#24) from bugfix/OZG-5814-nacharbeiten into dev

parents af317fec 913f8851
No related branches found
No related tags found
No related merge requests found
...@@ -20,19 +20,23 @@ ...@@ -20,19 +20,23 @@
package de.ozgcloud.info.common; package de.ozgcloud.info.common;
import io.jsonwebtoken.*; import java.util.Optional;
import io.jsonwebtoken.security.Keys;
import io.jsonwebtoken.security.SignatureException;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import java.util.Optional; import io.jsonwebtoken.ExpiredJwtException;
import io.jsonwebtoken.JwtParser;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.MalformedJwtException;
import io.jsonwebtoken.UnsupportedJwtException;
import io.jsonwebtoken.security.Keys;
import io.jsonwebtoken.security.SignatureException;
import lombok.extern.log4j.Log4j2;
@Log4j2 @Log4j2
@Component @Component
public class JwtTokenVerifier { public class JwtTokenVerifier {
@Value("${ozgcloud.jwt.secret}") @Value("${ozgcloud.jwt.secret}")
private String jwtSecret; private String jwtSecret;
......
...@@ -22,6 +22,9 @@ package de.ozgcloud.info.nachricht; ...@@ -22,6 +22,9 @@ package de.ozgcloud.info.nachricht;
import jakarta.validation.ConstraintViolationException; import jakarta.validation.ConstraintViolationException;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import de.ozgcloud.info.common.InvalidNachrichtenListUrlException; import de.ozgcloud.info.common.InvalidNachrichtenListUrlException;
import io.grpc.Status; import io.grpc.Status;
import io.grpc.stub.StreamObserver; import io.grpc.stub.StreamObserver;
...@@ -31,11 +34,13 @@ import net.devh.boot.grpc.server.service.GrpcService; ...@@ -31,11 +34,13 @@ import net.devh.boot.grpc.server.service.GrpcService;
@GrpcService @GrpcService
@RequiredArgsConstructor @RequiredArgsConstructor
public class NachrichtenGrpcService extends NachrichtServiceGrpc.NachrichtServiceImplBase { public class NachrichtenGrpcService extends NachrichtServiceGrpc.NachrichtServiceImplBase {
static final String LOCAL_PROFILE = "local";
private static final String NACHRICHTEN_LIST_URL_REGEX = "^[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*:[0-9]{1,5}$"; private static final String NACHRICHTEN_LIST_URL_REGEX = "^[A-Za-z0-9-]+(\\.[A-Za-z0-9-]+)*:[0-9]{1,5}$";
static final String NACHRICHTEN_LIST_URL_FORMAT = "dns:///%s"; static final String NACHRICHTEN_LIST_URL_FORMAT = "dns:///%s";
static final String STATUS_OK = "ok"; static final String STATUS_OK = "ok";
private final NachrichtenService nachrichtService; private final NachrichtenService nachrichtService;
private final Environment environment;
@Override @Override
public void saveNewNachricht(GrpcNewNachrichtRequest request, StreamObserver<GrpcNewNachrichtReply> responseObserver) { public void saveNewNachricht(GrpcNewNachrichtRequest request, StreamObserver<GrpcNewNachrichtReply> responseObserver) {
...@@ -59,6 +64,10 @@ public class NachrichtenGrpcService extends NachrichtServiceGrpc.NachrichtServic ...@@ -59,6 +64,10 @@ public class NachrichtenGrpcService extends NachrichtServiceGrpc.NachrichtServic
String getNachrichtenListUrl(GrpcNachricht nachricht) { String getNachrichtenListUrl(GrpcNachricht nachricht) {
var nachrichtenListUrl = nachricht.getNachrichtenListUrl(); var nachrichtenListUrl = nachricht.getNachrichtenListUrl();
if (environment.acceptsProfiles(Profiles.of(LOCAL_PROFILE))) {
return nachrichtenListUrl;
}
if (!nachrichtenListUrl.matches(NACHRICHTEN_LIST_URL_REGEX)) { if (!nachrichtenListUrl.matches(NACHRICHTEN_LIST_URL_REGEX)) {
throw new InvalidNachrichtenListUrlException(nachrichtenListUrl); throw new InvalidNachrichtenListUrlException(nachrichtenListUrl);
} }
......
...@@ -58,7 +58,7 @@ public class NachrichtEventTestFactory { ...@@ -58,7 +58,7 @@ public class NachrichtEventTestFactory {
.build(); .build();
} }
private static GrpcNachricht createGrpcNachricht() { public static GrpcNachricht createGrpcNachricht() {
return createGrpcNachricht(URL); return createGrpcNachricht(URL);
} }
......
...@@ -26,6 +26,7 @@ import static org.mockito.Mockito.*; ...@@ -26,6 +26,7 @@ import static org.mockito.Mockito.*;
import jakarta.validation.ConstraintViolationException; import jakarta.validation.ConstraintViolationException;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Nested; import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
...@@ -35,6 +36,8 @@ import org.mockito.InjectMocks; ...@@ -35,6 +36,8 @@ import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Spy; import org.mockito.Spy;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import de.ozgcloud.info.NachrichtEventTestFactory; import de.ozgcloud.info.NachrichtEventTestFactory;
import de.ozgcloud.info.common.InvalidNachrichtenListUrlException; import de.ozgcloud.info.common.InvalidNachrichtenListUrlException;
...@@ -49,6 +52,9 @@ class NachrichtEventGrpcServiceTest { ...@@ -49,6 +52,9 @@ class NachrichtEventGrpcServiceTest {
@Mock @Mock
private NachrichtenService nachrichtenService; private NachrichtenService nachrichtenService;
@Mock
private Environment environment;
@Captor @Captor
private ArgumentCaptor<NachrichtEvent> nachrichtArgumentCaptor; private ArgumentCaptor<NachrichtEvent> nachrichtArgumentCaptor;
...@@ -59,6 +65,11 @@ class NachrichtEventGrpcServiceTest { ...@@ -59,6 +65,11 @@ class NachrichtEventGrpcServiceTest {
private final GrpcNewNachrichtRequest newNachrichtRequest = NachrichtEventTestFactory.createNewNachrichtRequest(); private final GrpcNewNachrichtRequest newNachrichtRequest = NachrichtEventTestFactory.createNewNachrichtRequest();
@BeforeEach
void init() {
when(environment.acceptsProfiles(Profiles.of(LOCAL_PROFILE))).thenReturn(false);
}
@Test @Test
void shouldCallService() { void shouldCallService() {
nachrichtenGrpcService.saveNewNachricht(newNachrichtRequest, newNachrichtStreamObserver); nachrichtenGrpcService.saveNewNachricht(newNachrichtRequest, newNachrichtStreamObserver);
...@@ -79,6 +90,20 @@ class NachrichtEventGrpcServiceTest { ...@@ -79,6 +90,20 @@ class NachrichtEventGrpcServiceTest {
assertThat(savedNachricht.getNachrichtenListUrl()).isEqualTo(expectedNachrichtenListUrl); assertThat(savedNachricht.getNachrichtenListUrl()).isEqualTo(expectedNachrichtenListUrl);
} }
@Test
void shouldCreateLocalNachricht() {
when(environment.acceptsProfiles(Profiles.of(LOCAL_PROFILE))).thenReturn(true);
nachrichtenGrpcService.saveNewNachricht(newNachrichtRequest, newNachrichtStreamObserver);
verify(nachrichtenService, atMostOnce()).save(nachrichtArgumentCaptor.capture());
var savedNachricht = nachrichtArgumentCaptor.getValue();
assertThat(savedNachricht.getPostfachId()).isEqualTo(NachrichtEventTestFactory.POSTKORB_HANDLE);
assertThat(savedNachricht.getNachrichtenListUrl()).isEqualTo(NachrichtEventTestFactory.URL);
}
@Test @Test
void shouldCallOnNext() { void shouldCallOnNext() {
nachrichtenGrpcService.saveNewNachricht(newNachrichtRequest, newNachrichtStreamObserver); nachrichtenGrpcService.saveNewNachricht(newNachrichtRequest, newNachrichtStreamObserver);
...@@ -113,12 +138,25 @@ class NachrichtEventGrpcServiceTest { ...@@ -113,12 +138,25 @@ class NachrichtEventGrpcServiceTest {
@Nested @Nested
class TestGetNachrichtenListUrl { class TestGetNachrichtenListUrl {
@BeforeEach
void init() {
when(environment.acceptsProfiles(Profiles.of(LOCAL_PROFILE))).thenReturn(false);
}
@Test @Test
void shouldHaveValidFormat() { void shouldPassOnUrl() {
var url = "localhost:2345"; when(environment.acceptsProfiles(Profiles.of(LOCAL_PROFILE))).thenReturn(true);
var nachricht = NachrichtEventTestFactory.createGrpcNachricht(url);
var expectedNachrichtenListUrl = String.format(NACHRICHTEN_LIST_URL_FORMAT, url); var nachricht = NachrichtEventTestFactory.createGrpcNachricht();
var result = nachrichtenGrpcService.getNachrichtenListUrl(nachricht);
assertThat(result).isEqualTo(NachrichtEventTestFactory.URL);
}
@Test
void shouldHaveValidFormat() {
var nachricht = NachrichtEventTestFactory.createGrpcNachricht();
var expectedNachrichtenListUrl = String.format(NACHRICHTEN_LIST_URL_FORMAT, NachrichtEventTestFactory.URL);
var result = nachrichtenGrpcService.getNachrichtenListUrl(nachricht); var result = nachrichtenGrpcService.getNachrichtenListUrl(nachricht);
assertThat(result).isEqualTo(expectedNachrichtenListUrl); assertThat(result).isEqualTo(expectedNachrichtenListUrl);
...@@ -128,7 +166,6 @@ class NachrichtEventGrpcServiceTest { ...@@ -128,7 +166,6 @@ class NachrichtEventGrpcServiceTest {
void shouldHaveValidSubdomainFormat() { void shouldHaveValidSubdomainFormat() {
var url = "test.example.com:20"; var url = "test.example.com:20";
var nachricht = NachrichtEventTestFactory.createGrpcNachricht(url); var nachricht = NachrichtEventTestFactory.createGrpcNachricht(url);
var expectedNachrichtenListUrl = String.format(NACHRICHTEN_LIST_URL_FORMAT, url); var expectedNachrichtenListUrl = String.format(NACHRICHTEN_LIST_URL_FORMAT, url);
var result = nachrichtenGrpcService.getNachrichtenListUrl(nachricht); var result = nachrichtenGrpcService.getNachrichtenListUrl(nachricht);
...@@ -155,7 +192,7 @@ class NachrichtEventGrpcServiceTest { ...@@ -155,7 +192,7 @@ class NachrichtEventGrpcServiceTest {
@Test @Test
void shouldHaveInvalidPortFormat() { void shouldHaveInvalidPortFormat() {
var url = "localhost:2345678"; var url = "localhost:234567";
var nachricht = NachrichtEventTestFactory.createGrpcNachricht(url); var nachricht = NachrichtEventTestFactory.createGrpcNachricht(url);
assertThatExceptionOfType(InvalidNachrichtenListUrlException.class).isThrownBy( assertThatExceptionOfType(InvalidNachrichtenListUrlException.class).isThrownBy(
......
...@@ -22,11 +22,14 @@ ...@@ -22,11 +22,14 @@
*/ */
package de.ozgcloud.info.nachricht; package de.ozgcloud.info.nachricht;
import static de.ozgcloud.info.nachricht.NachrichtenGrpcService.*;
import static org.mockito.Mockito.*; import static org.mockito.Mockito.*;
import org.springframework.boot.autoconfigure.ImportAutoConfiguration; import org.springframework.boot.autoconfigure.ImportAutoConfiguration;
import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import net.devh.boot.grpc.client.autoconfigure.GrpcClientAutoConfiguration; import net.devh.boot.grpc.client.autoconfigure.GrpcClientAutoConfiguration;
import net.devh.boot.grpc.server.autoconfigure.GrpcServerAutoConfiguration; import net.devh.boot.grpc.server.autoconfigure.GrpcServerAutoConfiguration;
...@@ -48,8 +51,16 @@ class NachrichtEventGrpcTestConfiguration { ...@@ -48,8 +51,16 @@ class NachrichtEventGrpcTestConfiguration {
return mock(NachrichtenRepository.class); return mock(NachrichtenRepository.class);
} }
@Bean
Environment environment() {
var environment = mock(Environment.class);
when(environment.acceptsProfiles(Profiles.of(LOCAL_PROFILE))).thenReturn(false);
return environment;
}
@Bean @Bean
NachrichtenGrpcService nachrichtenGrpcService() { NachrichtenGrpcService nachrichtenGrpcService() {
return new NachrichtenGrpcService(nachrichtenService()); return new NachrichtenGrpcService(nachrichtenService(), environment());
} }
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment