5ffcbf7dcd145ac6d982fa4c5c5613a6475bb689
[policy/models.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.onap.policy.controlloop.actorserviceprovider.topic;
22
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;
27
28 import java.util.Map;
29 import lombok.Builder;
30 import lombok.Getter;
31 import lombok.Setter;
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;
36
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";
41
42     private SelectorKey key;
43
44     @Before
45     public void setUp() {
46         key = new SelectorKey(FIELD1, FIELD2);
47     }
48
49     @Test
50     public void testHashCode_testEquals() {
51         SelectorKey key2 = new SelectorKey(FIELD1, FIELD2);
52         assertEquals(key, key2);
53         assertEquals(key.hashCode(), key2.hashCode());
54
55         key2 = new SelectorKey(FIELD1, FIELDX);
56         assertNotEquals(key, key2);
57         assertNotEquals(key.hashCode(), key2.hashCode());
58
59         // test empty key
60         key = new SelectorKey();
61         key2 = new SelectorKey();
62         assertEquals(key, key2);
63         assertEquals(key.hashCode(), key2.hashCode());
64     }
65
66     @Test
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);
70
71         String result = key.extractField(sco);
72         assertNotNull(result);
73         assertEquals("value B", result);
74
75         // shorter key
76         assertEquals("world", new SelectorKey("hello").extractField(sco));
77         assertNull(new SelectorKey("bye").extractField(sco));
78
79         // not found
80         assertNull(new SelectorKey(FIELD1, "not field 2").extractField(sco));
81
82         // test with empty key
83         assertNull(new SelectorKey().extractField(sco));
84     }
85
86     @Test
87     public void testToString() {
88         assertEquals("[map, abc]", key.toString());
89     }
90
91     @Getter
92     @Setter
93     @Builder
94     protected static class Data {
95         private String text;
96         private Map<String, String> map;
97     }
98 }