-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add hint api for Context (#43)
- Loading branch information
1 parent
e9d0860
commit 83096a0
Showing
3 changed files
with
128 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
92 changes: 92 additions & 0 deletions
92
ingester-rpc/src/test/java/io/greptime/rpc/ContextTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
/* | ||
* Copyright 2023 Greptime Team | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package io.greptime.rpc; | ||
|
||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* @author jiachun.fjc | ||
*/ | ||
public class ContextTest { | ||
@Test | ||
public void newDefaultShouldReturnEmptyContextTest() { | ||
Context context = Context.newDefault(); | ||
Assert.assertTrue(context.entrySet().isEmpty()); | ||
} | ||
|
||
@Test | ||
public void ofShouldReturnContextWithOneEntryTest() { | ||
Context context = Context.of("key", "value"); | ||
Assert.assertEquals("value", context.get("key")); | ||
} | ||
|
||
@Test | ||
public void hintShouldReturnContextWithHintEntryTest() { | ||
Context context = Context.hint("key", "value"); | ||
Assert.assertEquals("value", context.get("x-greptime-hint:key")); | ||
} | ||
|
||
@Test | ||
public void withShouldAddEntryToContextTest() { | ||
Context context = Context.newDefault(); | ||
context.with("key", "value"); | ||
Assert.assertEquals("value", context.get("key")); | ||
} | ||
|
||
@Test | ||
public void withHintShouldAddHintEntryToContextTest() { | ||
Context context = Context.newDefault(); | ||
context.withHint("key", "value").with("key2", "value"); | ||
Assert.assertEquals("value", context.get("x-greptime-hint:key")); | ||
Assert.assertEquals("value", context.get("key2")); | ||
} | ||
|
||
@Test | ||
public void getShouldReturnNullForNonExistingKeyTest() { | ||
Context context = Context.newDefault(); | ||
Assert.assertNull(context.get("key")); | ||
} | ||
|
||
@Test | ||
public void removeShouldRemoveEntryFromContextTest() { | ||
Context context = Context.of("key", "value"); | ||
String value = context.remove("key"); | ||
Assert.assertNull(context.get("key")); | ||
Assert.assertEquals("value", value); | ||
} | ||
|
||
@Test | ||
public void getOrDefaultShouldReturnDefaultValueForNonExistingKeyTest() { | ||
Context context = Context.newDefault(); | ||
Assert.assertEquals("default", context.getOrDefault("key", "default")); | ||
} | ||
|
||
@Test | ||
public void clearShouldRemoveAllEntriesFromContextTest() { | ||
Context context = Context.of("key", "value"); | ||
context.clear(); | ||
Assert.assertTrue(context.entrySet().isEmpty()); | ||
} | ||
|
||
@Test | ||
public void copyShouldReturnIdenticalContextTest() { | ||
Context context = Context.of("key", "value"); | ||
Context copy = context.copy(); | ||
Assert.assertEquals(context.get("key").toString(), copy.get("key").toString()); | ||
} | ||
} |