2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.controlloop.actorserviceprovider.topic;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
29 import lombok.Builder;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.onap.policy.common.utils.coder.StandardCoderObject;
35 import org.onap.policy.controlloop.actorserviceprovider.Util;
37 public class SelectorKeyTest {
38 private static final String FIELD1 = "map";
39 private static final String FIELD2 = "abc";
40 private static final String FIELDX = "abd";
42 private SelectorKey key;
46 key = new SelectorKey(FIELD1, FIELD2);
50 public void testHashCode_testEquals() {
51 SelectorKey key2 = new SelectorKey(FIELD1, FIELD2);
52 assertEquals(key, key2);
53 assertEquals(key.hashCode(), key2.hashCode());
55 key2 = new SelectorKey(FIELD1, FIELDX);
56 assertNotEquals(key, key2);
57 assertNotEquals(key.hashCode(), key2.hashCode());
60 key = new SelectorKey();
61 key2 = new SelectorKey();
62 assertEquals(key, key2);
63 assertEquals(key.hashCode(), key2.hashCode());
67 public void testExtractField() {
68 Map<String, Object> map = Map.of("hello", "world", FIELD1, Map.of("another", "", FIELD2, "value B"));
69 StandardCoderObject sco = Util.translate("", map, StandardCoderObject.class);
71 String result = key.extractField(sco);
72 assertNotNull(result);
73 assertEquals("value B", result);
76 assertEquals("world", new SelectorKey("hello").extractField(sco));
77 assertNull(new SelectorKey("bye").extractField(sco));
80 assertNull(new SelectorKey(FIELD1, "not field 2").extractField(sco));
82 // test with empty key
83 assertNull(new SelectorKey().extractField(sco));
89 protected static class Data {
91 private Map<String, String> map;