Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Key<T> specialization to SessionContext.containsKey #1844

Merged
merged 1 commit into from
Nov 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,23 @@ public <T> T getOrDefault(Key<T> key, T defaultValue) {
return defaultValue;
}

/**
* {@inheritDoc}
*
* <p>This method exists for static analysis.
*/
@Override
public boolean containsKey(Object key) {
return super.containsKey(key);
}

/**
* Checks for the existence of the key in the context.
*/
public <T> boolean containsKey(Key<T> key) {
return typedMap.containsKey(Objects.requireNonNull(key, "key"));
}

/**
* {@inheritDoc}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,14 @@
*/
package com.netflix.zuul.context;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
import static org.junit.jupiter.api.Assertions.assertThrows;

import com.google.common.truth.Truth;
import com.netflix.zuul.context.SessionContext.Key;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import static org.junit.jupiter.api.Assertions.*;

@ExtendWith(MockitoExtension.class)
class SessionContextTest {

Expand Down Expand Up @@ -107,4 +105,18 @@ void remove() {
Truth.assertThat(context.get(key)).isNull();
Truth.assertThat(val).isEqualTo("bar");
}

@Test
void containsKey() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe add a test case for the non-typed version as well (if it doesn't already exist)?

SessionContext context = new SessionContext();
Key<String> key = SessionContext.newKey("foo");
context.put(key, "bar");

Truth.assertThat(context.containsKey(key)).isTrue();

String val = context.remove(key);
Truth.assertThat(val).isEqualTo("bar");

Truth.assertThat(context.containsKey(key)).isFalse();
}
}
Loading