|
| 1 | +/* |
| 2 | + * Copyright 2023 Greptime Team |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.greptime.rpc; |
| 18 | + |
| 19 | +import org.junit.Assert; |
| 20 | +import org.junit.Test; |
| 21 | + |
| 22 | +/** |
| 23 | + * @author jiachun.fjc |
| 24 | + */ |
| 25 | +public class ContextTest { |
| 26 | + @Test |
| 27 | + public void newDefaultShouldReturnEmptyContextTest() { |
| 28 | + Context context = Context.newDefault(); |
| 29 | + Assert.assertTrue(context.entrySet().isEmpty()); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + public void ofShouldReturnContextWithOneEntryTest() { |
| 34 | + Context context = Context.of("key", "value"); |
| 35 | + Assert.assertEquals("value", context.get("key")); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void hintShouldReturnContextWithHintEntryTest() { |
| 40 | + Context context = Context.hint("key", "value"); |
| 41 | + Assert.assertEquals("value", context.get("x-greptime-hint:key")); |
| 42 | + } |
| 43 | + |
| 44 | + @Test |
| 45 | + public void withShouldAddEntryToContextTest() { |
| 46 | + Context context = Context.newDefault(); |
| 47 | + context.with("key", "value"); |
| 48 | + Assert.assertEquals("value", context.get("key")); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void withHintShouldAddHintEntryToContextTest() { |
| 53 | + Context context = Context.newDefault(); |
| 54 | + context.withHint("key", "value").with("key2", "value"); |
| 55 | + Assert.assertEquals("value", context.get("x-greptime-hint:key")); |
| 56 | + Assert.assertEquals("value", context.get("key2")); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void getShouldReturnNullForNonExistingKeyTest() { |
| 61 | + Context context = Context.newDefault(); |
| 62 | + Assert.assertNull(context.get("key")); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void removeShouldRemoveEntryFromContextTest() { |
| 67 | + Context context = Context.of("key", "value"); |
| 68 | + String value = context.remove("key"); |
| 69 | + Assert.assertNull(context.get("key")); |
| 70 | + Assert.assertEquals("value", value); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + public void getOrDefaultShouldReturnDefaultValueForNonExistingKeyTest() { |
| 75 | + Context context = Context.newDefault(); |
| 76 | + Assert.assertEquals("default", context.getOrDefault("key", "default")); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void clearShouldRemoveAllEntriesFromContextTest() { |
| 81 | + Context context = Context.of("key", "value"); |
| 82 | + context.clear(); |
| 83 | + Assert.assertTrue(context.entrySet().isEmpty()); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + public void copyShouldReturnIdenticalContextTest() { |
| 88 | + Context context = Context.of("key", "value"); |
| 89 | + Context copy = context.copy(); |
| 90 | + Assert.assertEquals(context.get("key").toString(), copy.get("key").toString()); |
| 91 | + } |
| 92 | +} |
0 commit comments