|
| 1 | +/* |
| 2 | +Copyright IBM 2017 All Rights Reserved. |
| 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 | +package org.hyperledger.fabric.shim.ledger; |
| 17 | + |
| 18 | +import static org.hamcrest.Matchers.contains; |
| 19 | +import static org.hamcrest.Matchers.equalTo; |
| 20 | +import static org.hamcrest.Matchers.hasSize; |
| 21 | +import static org.hamcrest.Matchers.is; |
| 22 | +import static org.junit.Assert.assertThat; |
| 23 | + |
| 24 | +import java.util.Arrays; |
| 25 | + |
| 26 | +import org.junit.Test; |
| 27 | + |
| 28 | +public class CompositeKeyTest { |
| 29 | + |
| 30 | + @Test |
| 31 | + public void testCompositeKeyStringStringArray() { |
| 32 | + final CompositeKey key = new CompositeKey("abc", new String[] {"def", "ghi", "jkl", "mno"}); |
| 33 | + assertThat(key.getObjectType(), is(equalTo("abc"))); |
| 34 | + assertThat(key.getAttributes(), hasSize(4)); |
| 35 | + assertThat(key.toString(), is(equalTo("abc\u0000def\u0000ghi\u0000jkl\u0000mno\u0000"))); |
| 36 | + } |
| 37 | + |
| 38 | + @Test |
| 39 | + public void testCompositeKeyStringListOfString() { |
| 40 | + final CompositeKey key = new CompositeKey("abc", Arrays.asList(new String[] {"def", "ghi", "jkl", "mno"})); |
| 41 | + assertThat(key.getObjectType(), is(equalTo("abc"))); |
| 42 | + assertThat(key.getAttributes(), hasSize(4)); |
| 43 | + assertThat(key.toString(), is(equalTo("abc\u0000def\u0000ghi\u0000jkl\u0000mno\u0000"))); |
| 44 | + } |
| 45 | + |
| 46 | + @Test(expected=CompositeKeyFormatException.class) |
| 47 | + public void testCompositeKeyWithInvalidObjectTypeDelimiter() { |
| 48 | + new CompositeKey("ab\u0000c", Arrays.asList(new String[] {"def", "ghi", "jkl", "mno"})); |
| 49 | + } |
| 50 | + |
| 51 | + @Test(expected=CompositeKeyFormatException.class) |
| 52 | + public void testCompositeKeyWithInvalidAttributeDelimiter() { |
| 53 | + new CompositeKey("abc", Arrays.asList(new String[] {"def", "ghi", "j\u0000kl", "mno"})); |
| 54 | + } |
| 55 | + |
| 56 | + @Test(expected=CompositeKeyFormatException.class) |
| 57 | + public void testCompositeKeyWithInvalidObjectTypeMaxCodePoint() { |
| 58 | + new CompositeKey("ab\udbff\udfffc", Arrays.asList(new String[] {"def", "ghi", "jkl", "mno"})); |
| 59 | + } |
| 60 | + @Test(expected=CompositeKeyFormatException.class) |
| 61 | + public void testCompositeKeyWithInvalidAttributeMaxCodePoint() { |
| 62 | + new CompositeKey("abc", Arrays.asList(new String[] {"def", "ghi", "jk\udbff\udfffl", "mno"})); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + public void testGetObjectType() { |
| 67 | + final CompositeKey key = new CompositeKey("abc", Arrays.asList(new String[] {"def", "ghi", "jkl", "mno"})); |
| 68 | + assertThat(key.getObjectType(), is(equalTo("abc"))); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testGetAttributes() { |
| 73 | + final CompositeKey key = new CompositeKey("abc", Arrays.asList(new String[] {"def", "ghi", "jkl", "mno"})); |
| 74 | + assertThat(key.getObjectType(), is(equalTo("abc"))); |
| 75 | + assertThat(key.getAttributes(), hasSize(4)); |
| 76 | + assertThat(key.getAttributes(), contains("def", "ghi", "jkl", "mno")); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + public void testToString() { |
| 81 | + final CompositeKey key = new CompositeKey("abc", Arrays.asList(new String[] {"def", "ghi", "jkl", "mno"})); |
| 82 | + assertThat(key.toString(), is(equalTo("abc\u0000def\u0000ghi\u0000jkl\u0000mno\u0000"))); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testParseCompositeKey() { |
| 87 | + final CompositeKey key = CompositeKey.parseCompositeKey("abc\u0000def\u0000ghi\u0000jkl\u0000mno\u0000"); |
| 88 | + assertThat(key.getObjectType(), is(equalTo("abc"))); |
| 89 | + assertThat(key.getAttributes(), hasSize(4)); |
| 90 | + assertThat(key.getAttributes(), contains("def", "ghi", "jkl", "mno")); |
| 91 | + assertThat(key.toString(), is(equalTo("abc\u0000def\u0000ghi\u0000jkl\u0000mno\u0000"))); |
| 92 | + } |
| 93 | + |
| 94 | + @Test(expected=CompositeKeyFormatException.class) |
| 95 | + public void testParseCompositeKeyInvalidObjectType() { |
| 96 | + CompositeKey.parseCompositeKey("ab\udbff\udfffc\u0000def\u0000ghi\u0000jkl\u0000mno\u0000"); |
| 97 | + } |
| 98 | + |
| 99 | + @Test(expected=CompositeKeyFormatException.class) |
| 100 | + public void testParseCompositeKeyInvalidAttribute() { |
| 101 | + CompositeKey.parseCompositeKey("abc\u0000def\u0000ghi\u0000jk\udbff\udfffl\u0000mno\u0000"); |
| 102 | + } |
| 103 | + |
| 104 | +} |
0 commit comments