Skip to content

Commit

Permalink
Merge pull request #1844 from Netflix/add_more_sessioncontext_methods
Browse files Browse the repository at this point in the history
Add Key<T> specialization to SessionContext.containsKey
  • Loading branch information
connorworley authored Nov 4, 2024
2 parents 37f4ea9 + 027099b commit 4ad1d2f
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 4 deletions.
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() {
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();
}
}

0 comments on commit 4ad1d2f

Please sign in to comment.