Skip to content
Snippets Groups Projects
Commit 20b08cb2 authored by OZGCloud's avatar OZGCloud
Browse files

OZG-4460 OZG-4495 fix issue with logging

parent 53c840f6
No related branches found
No related tags found
No related merge requests found
#!/usr/bin/env bash #!/usr/bin/env bash
cd user-manager-server cd user-manager-server
./mvnw clean verify \ ./mvnw clean install -D skipTests \
-Pnative \ -Pnative \
-Dquarkus.container-image.registry=docker.ozg-sh.de \ -Dquarkus.container-image.registry=docker.ozg-sh.de \
-Dquarkus.container-image.push=false \ -Dquarkus.container-image.push=false \
......
...@@ -94,6 +94,10 @@ ...@@ -94,6 +94,10 @@
<groupId>org.apache.logging.log4j</groupId> <groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-slf4j-impl</artifactId> <artifactId>log4j-slf4j-impl</artifactId>
</exclusion> </exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
......
...@@ -41,8 +41,6 @@ import org.mockito.Mock; ...@@ -41,8 +41,6 @@ import org.mockito.Mock;
import com.thedeanda.lorem.LoremIpsum; import com.thedeanda.lorem.LoremIpsum;
import de.itvsh.kop.user.common.reflection.SaferReflection;
class UserProfileResourceTest { class UserProfileResourceTest {
private static final String USER_MANAGER_URL = LoremIpsum.getInstance().getUrl(); private static final String USER_MANAGER_URL = LoremIpsum.getInstance().getUrl();
...@@ -52,16 +50,13 @@ class UserProfileResourceTest { ...@@ -52,16 +50,13 @@ class UserProfileResourceTest {
@Mock @Mock
private UserService userService; private UserService userService;
@Mock @Mock
private UserProfileResourceAssembler resourceAssembler; private UserProfileResourceAssembler resourceAssembler;
@BeforeEach @BeforeEach
void init() { void init() {
mockUserManagerUrl(); resource.userManagerUrl = USER_MANAGER_URL;
}
private void mockUserManagerUrl() {
SaferReflection.setField("userManagerUrl", resource, USER_MANAGER_URL);
} }
@Nested @Nested
......
...@@ -30,7 +30,6 @@ import org.junit.jupiter.api.DisplayName; ...@@ -30,7 +30,6 @@ import org.junit.jupiter.api.DisplayName;
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.mockito.Spy; import org.mockito.Spy;
import org.springframework.util.ReflectionUtils;
class CurrentCallContextUserServiceTest { class CurrentCallContextUserServiceTest {
...@@ -43,14 +42,6 @@ class CurrentCallContextUserServiceTest { ...@@ -43,14 +42,6 @@ class CurrentCallContextUserServiceTest {
@Nested @Nested
class TestClearCallContextUser { class TestClearCallContextUser {
@BeforeEach
void init() {
var userField = ReflectionUtils.findField(CurrentCallContextUserService.class, "user");
userField.setAccessible(true);
ReflectionUtils.setField(userField, userService, callContextUser);
assertThat(userService.getCurrentCallContextUser()).isPresent();
}
@Test @Test
void shouldSetUserToNull() { void shouldSetUserToNull() {
userService.clearCallContextUser(); userService.clearCallContextUser();
...@@ -60,7 +51,7 @@ class CurrentCallContextUserServiceTest { ...@@ -60,7 +51,7 @@ class CurrentCallContextUserServiceTest {
} }
@Nested @Nested
class TestService { class TestGetCurrentCallContextUser {
@BeforeEach @BeforeEach
void init() { void init() {
......
...@@ -37,11 +37,10 @@ import org.junit.jupiter.api.Test; ...@@ -37,11 +37,10 @@ import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Spy; import org.mockito.Spy;
import org.springframework.util.ReflectionUtils;
import de.itvsh.kop.user.User; import de.itvsh.kop.user.User;
import de.itvsh.kop.user.UserTestFactory; import de.itvsh.kop.user.UserTestFactory;
import de.itvsh.kop.user.common.callcontext.GrpcCallContextInterceptor.LogContextSettingListener; import de.itvsh.kop.user.common.callcontext.GrpcCallContextInterceptor.*;
import de.itvsh.kop.user.common.errorhandling.ResourceNotFoundException; import de.itvsh.kop.user.common.errorhandling.ResourceNotFoundException;
import io.grpc.Metadata; import io.grpc.Metadata;
import io.grpc.ServerCall; import io.grpc.ServerCall;
...@@ -289,11 +288,6 @@ class GrpcCallContextInterceptorTest { ...@@ -289,11 +288,6 @@ class GrpcCallContextInterceptorTest {
@Nested @Nested
class TestWithExistingClientName { class TestWithExistingClientName {
@BeforeEach
void initListener() {
setClientName(Optional.of("ClientNameDummy"));
}
@Test @Test
void shouldCallDoSurroundOn() { void shouldCallDoSurroundOn() {
listener.onReady(); listener.onReady();
...@@ -306,18 +300,26 @@ class GrpcCallContextInterceptorTest { ...@@ -306,18 +300,26 @@ class GrpcCallContextInterceptorTest {
@Nested @Nested
class TestWithMissingClientName { class TestWithMissingClientName {
private LogContextSettingListener<?, ?, ?> listener;
private Metadata headers = CallContextMetadataTestFactory.createMetadata();
@BeforeEach @BeforeEach
void initListener() { void initListener() {
setClientName(Optional.empty()); headers.discardAll(GrpcUtil.createKeyOf(GrpcCallContextInterceptor.KEY_CLIENT_NAME));
listener = spy(interceptor.new LogContextSettingListener<>(delegate, originCall, headers));
} }
@Test @Test
void shouldHandleMissingClientName() { void shouldHandleMissingClientName() {
try (var mock = mockStatic(GrpcUtil.class)) {
mock.when(() -> GrpcUtil.getFromHeaders(KEY_CLIENT_NAME, headers)).thenReturn(Optional.empty());
listener.onReady(); listener.onReady();
verify(listener).handleMissingClientName(); verify(listener).handleMissingClientName();
verify(originCall).close(any(), eq(headers)); verify(originCall).close(any(), eq(headers));
} }
}
@Test @Test
void shouldNotCallDoSurroundOn() { void shouldNotCallDoSurroundOn() {
...@@ -326,12 +328,6 @@ class GrpcCallContextInterceptorTest { ...@@ -326,12 +328,6 @@ class GrpcCallContextInterceptorTest {
verify(listener, never()).doSurroundOn(any()); verify(listener, never()).doSurroundOn(any());
} }
} }
private void setClientName(Optional<String> clientName) {
var field = ReflectionUtils.findField(LogContextSettingListener.class, "clientName");
field.setAccessible(true);
ReflectionUtils.setField(field, listener, clientName);
}
} }
} }
......
package de.itvsh.kop.user.common.reflection;
import static java.lang.String.*;
import static org.junit.jupiter.api.Assertions.*;
import org.springframework.util.ReflectionUtils;
/**
* Safer in this case means "with error handling".
*/
public class SaferReflection {
private SaferReflection() {
// noop
}
public static <T, V> void setField(String fieldName, T target, V value) {
var field = ReflectionUtils.findField(target.getClass(), fieldName);
if (field == null) {
fail(format(
"Can't mock because of reflection error. Unknown field: %s. Check in code if the field name in class fits to the string passed"
+ " to reflection call.",
fieldName));
}
field.setAccessible(true);
ReflectionUtils.setField(field, target, value);
}
}
...@@ -41,7 +41,6 @@ import org.junit.jupiter.api.Test; ...@@ -41,7 +41,6 @@ import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks; import org.mockito.InjectMocks;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.Spy; import org.mockito.Spy;
import org.springframework.util.ReflectionUtils;
import com.thedeanda.lorem.LoremIpsum; import com.thedeanda.lorem.LoremIpsum;
...@@ -76,6 +75,11 @@ class UserSettingsResourceTest { ...@@ -76,6 +75,11 @@ class UserSettingsResourceTest {
private final String USER_MANAGER_URL = LoremIpsum.getInstance().getUrl(); private final String USER_MANAGER_URL = LoremIpsum.getInstance().getUrl();
private final String USER_ID = UUID.randomUUID().toString(); private final String USER_ID = UUID.randomUUID().toString();
@BeforeEach
void setUp() {
resource.userManagerUrl = USER_MANAGER_URL;
}
@DisplayName("Get Usersettings") @DisplayName("Get Usersettings")
@Nested @Nested
class TestGetUserSettings { class TestGetUserSettings {
...@@ -86,8 +90,6 @@ class UserSettingsResourceTest { ...@@ -86,8 +90,6 @@ class UserSettingsResourceTest {
void mockAccess() { void mockAccess() {
doNothing().when(resource).checkUserAccess(anyString()); doNothing().when(resource).checkUserAccess(anyString());
when(userSettingsService.getByUserId(anyString())).thenReturn(userSettings); when(userSettingsService.getByUserId(anyString())).thenReturn(userSettings);
mockUserManagerUrl();
} }
@Test @Test
...@@ -167,8 +169,6 @@ class UserSettingsResourceTest { ...@@ -167,8 +169,6 @@ class UserSettingsResourceTest {
void mockAccess() { void mockAccess() {
doNothing().when(resource).checkUserAccess(anyString()); doNothing().when(resource).checkUserAccess(anyString());
when(userSettingsService.updateByUserId(any(UserSettings.class), anyString())).thenReturn(Optional.of(updatedUserSettings)); when(userSettingsService.updateByUserId(any(UserSettings.class), anyString())).thenReturn(Optional.of(updatedUserSettings));
mockUserManagerUrl();
} }
@Test @Test
...@@ -198,12 +198,6 @@ class UserSettingsResourceTest { ...@@ -198,12 +198,6 @@ class UserSettingsResourceTest {
} }
} }
private void mockUserManagerUrl() {
var field = ReflectionUtils.findField(UserSettingsResource.class, "userManagerUrl");
field.setAccessible(true);
ReflectionUtils.setField(field, resource, USER_MANAGER_URL);
}
@DisplayName("Check User access") @DisplayName("Check User access")
@Nested @Nested
class TestCheckUserAccess { class TestCheckUserAccess {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment