summaryrefslogtreecommitdiffstats
path: root/infra/translate-utils/src/test/java/io/fd/honeycomb/translate/util/read/cache/TypeAwareIdentifierCacheKeyFactoryTest.java
blob: 305fba143ac31ecd99cb3a677494a6d94f9d67e7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
/*
 * Copyright (c) 2016 Cisco and/or its affiliates.
 *
 * 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.fd.honeycomb.translate.util.read.cache;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import com.google.common.collect.ImmutableSet;
import org.junit.Before;
import org.junit.Test;
import org.opendaylight.yangtools.yang.binding.ChildOf;
import org.opendaylight.yangtools.yang.binding.DataObject;
import org.opendaylight.yangtools.yang.binding.Identifiable;
import org.opendaylight.yangtools.yang.binding.Identifier;
import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;

public class TypeAwareIdentifierCacheKeyFactoryTest {

    private DataObjectParentKey parentKey;
    private DataObjectChildKey childKey;
    private InstanceIdentifier<DataObjectChild> identifierBothKeyed;
    private InstanceIdentifier<DataObjectChild> identifierOneMissing;
    private InstanceIdentifier<DataObjectChild> identifierNoneKeyed;
    private TypeAwareIdentifierCacheKeyFactory simpleKeyFactory;
    private TypeAwareIdentifierCacheKeyFactory complexKeyFactory;

    @Before
    public void init() {
        parentKey = new DataObjectParentKey();
        childKey = new DataObjectChildKey();
        identifierBothKeyed = InstanceIdentifier.create(SuperDataObject.class).child(DataObjectParent.class, parentKey)
                .child(DataObjectChild.class, childKey);
        identifierOneMissing = InstanceIdentifier.create(DataObjectChild.class);
        identifierNoneKeyed = InstanceIdentifier.create(SuperDataObject.class).child(DataObjectParent.class)
                .child(DataObjectChild.class);

        complexKeyFactory =
                new TypeAwareIdentifierCacheKeyFactory(String.class, ImmutableSet.of(DataObjectParent.class));
        simpleKeyFactory = new TypeAwareIdentifierCacheKeyFactory(String.class);
    }

    @Test
    public void createKeyBothKeyedComplex() {
        final String key = complexKeyFactory.createKey(identifierBothKeyed);

        /**
         * Should pass because key constructed in this case should look like :
         * additional_scope_type[additional_scope_type_key]|cached_type
         * */
        verifyComplexKey(key);
    }

    /**
     * Should fail because provided identifier does'nt contain all requested key parts
     */
    @Test(expected = IllegalArgumentException.class)
    public void createKeyOneMissingComplex() {
        complexKeyFactory.createKey(identifierOneMissing);
    }

    /**
     * Should fail because request paths are not keyed
     */
    @Test(expected = IllegalArgumentException.class)
    public void createKeyNoneKeyedComplex() {
        complexKeyFactory.createKey(identifierNoneKeyed);
    }

    @Test
    public void createKeyBothKeyedSimple() {
        final String key = simpleKeyFactory.createKey(identifierBothKeyed);

        /**
         * Should pass because key constructed in this case should look like : cached_type
         * */
        verifySimpleKey(key);
    }

    @Test
    public void createKeyOneMissingSimple() {
        final String key = simpleKeyFactory.createKey(identifierOneMissing);
        /**
         * Should pass because key constructed in this case should look like : cached_type
         * */
        verifySimpleKey(key);
    }

    /**
     * Should fail because request paths are not keyed
     */
    @Test
    public void createKeyNoneKeyedSimple() {
        final String key = simpleKeyFactory.createKey(identifierNoneKeyed);
        /**
         * Should pass because key constructed in this case should look like : cached_type
         * */
        verifySimpleKey(key);
    }

    private void verifyComplexKey(final String key) {
        assertTrue(key.contains(String.class.getTypeName()));
        assertTrue(key.contains(DataObjectParent.class.getTypeName()));
        assertTrue(key.contains(parentKey.toString()));
        assertTrue(key.contains(DataObjectChild.class.getTypeName()));
        assertFalse(key.contains(childKey.toString()));
        assertFalse(key.contains(SuperDataObject.class.getTypeName()));
    }

    private void verifySimpleKey(final String key) {
        assertTrue(key.contains(String.class.getTypeName()));
        assertFalse(key.contains(DataObjectParent.class.getTypeName()));
        assertFalse(key.contains(parentKey.toString()));
        assertTrue(key.contains(DataObjectChild.class.getTypeName()));
        assertFalse(key.contains(childKey.toString()));
        assertFalse(key.contains(SuperDataObject.class.getTypeName()));
    }

    private interface SuperDataObject extends DataObject {
    }

    private interface DataObjectParent extends DataObject, ChildOf<SuperDataObject>, Identifiable<DataObjectParentKey> {
    }

    private interface DataObjectChild extends DataObject, ChildOf<DataObjectParent>, Identifiable<DataObjectChildKey> {
    }

    private class DataObjectParentKey implements Identifier<DataObjectParent> {
    }

    private class DataObjectChildKey implements Identifier<DataObjectChild> {
    }
}