summaryrefslogtreecommitdiffstats
path: root/context/context-management/src/test/java/org/onap/policy/apex/context/impl/ContextAlbumImplTest.java
blob: 6ebb79f4377beecd9fce390522d12ae5ba464caa (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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2020 Nordix Foundation.
 * ================================================================================
 * 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.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.context.impl;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;

import java.util.LinkedHashMap;
import java.util.Map;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;
import org.onap.policy.apex.context.ContextAlbum;
import org.onap.policy.apex.context.ContextException;
import org.onap.policy.apex.context.Distributor;
import org.onap.policy.apex.context.impl.distribution.jvmlocal.JvmLocalDistributor;
import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters;
import org.onap.policy.apex.context.parameters.ContextParameterConstants;
import org.onap.policy.apex.context.parameters.ContextParameters;
import org.onap.policy.apex.context.parameters.SchemaParameters;
import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
import org.onap.policy.apex.model.basicmodel.concepts.AxConcept;
import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
import org.onap.policy.apex.model.basicmodel.service.ModelService;
import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums;
import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
import org.onap.policy.common.parameters.ParameterService;

public class ContextAlbumImplTest {
    /**
     * Set ups everything for the test.
     */
    @BeforeClass
    public static void prepareForTest() {
        final ContextParameters contextParameters = new ContextParameters();
        contextParameters.getLockManagerParameters()
            .setPluginClass("org.onap.policy.apex.context.impl.locking.jvmlocal.JvmLocalLockManager");

        contextParameters.setName(ContextParameterConstants.MAIN_GROUP_NAME);
        contextParameters.getDistributorParameters().setName(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
        contextParameters.getLockManagerParameters().setName(ContextParameterConstants.LOCKING_GROUP_NAME);
        contextParameters.getPersistorParameters().setName(ContextParameterConstants.PERSISTENCE_GROUP_NAME);

        ParameterService.register(contextParameters);
        ParameterService.register(contextParameters.getDistributorParameters());
        ParameterService.register(contextParameters.getLockManagerParameters());
        ParameterService.register(contextParameters.getPersistorParameters());

        final SchemaParameters schemaParameters = new SchemaParameters();
        schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
        schemaParameters.getSchemaHelperParameterMap().put("JAVA", new JavaSchemaHelperParameters());

        ParameterService.register(schemaParameters);
    }

    /**
     * Clear down the test data.
     */
    @AfterClass
    public static void cleanUpAfterTest() {
        ParameterService.deregister(ContextParameterConstants.DISTRIBUTOR_GROUP_NAME);
        ParameterService.deregister(ContextParameterConstants.LOCKING_GROUP_NAME);
        ParameterService.deregister(ContextParameterConstants.PERSISTENCE_GROUP_NAME);
        ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
        ParameterService.deregister(ContextParameterConstants.MAIN_GROUP_NAME);
        ParameterService.clear();
    }

    @Test
    public void testNullsOnConstructor() {
        assertThatThrownBy(() -> new ContextAlbumImpl(null, null, null))
            .hasMessage("Context album definition may not be null");

        assertThatThrownBy(() -> new ContextAlbumImpl(new AxContextAlbum(), null, null))
            .hasMessage("Distributor may not be null");

        assertThatThrownBy(() -> new ContextAlbumImpl(new AxContextAlbum(), new JvmLocalDistributor(), null))
            .hasMessage("Album map may not be null");

        assertThatThrownBy(() -> new ContextAlbumImpl(new AxContextAlbum(), new JvmLocalDistributor(),
            new LinkedHashMap<String, Object>()))
                .hasMessage("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas "
                    + "not found in model service");
    }

    @Test
    public void testAlbumInterface() throws ContextException {
        AxContextSchemas schemas = new AxContextSchemas();
        AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"),
            "JAVA", "java.lang.String");
        schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
        ModelService.registerModel(AxContextSchemas.class, schemas);

        AxContextAlbum axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
            true, AxArtifactKey.getNullKey());

        assertThatThrownBy(
            () -> new ContextAlbumImpl(axContextAlbum, new JvmLocalDistributor(), new LinkedHashMap<String, Object>()))
                .hasMessageContaining("could not initiate schema management for context album AxContextAlbum");

        axContextAlbum.setItemSchema(simpleStringSchema.getKey());
        Distributor distributor = new JvmLocalDistributor();
        distributor.init(axContextAlbum.getKey());
        ContextAlbum album = new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<String, Object>());

        assertEquals("TestContextAlbum", album.getName());
        assertEquals("TestContextAlbum:0.0.1", album.getKey().getId());
        assertEquals("TestContextAlbum:0.0.1", album.getAlbumDefinition().getId());
        assertEquals("SimpleStringSchema:0.0.1", album.getSchemaHelper().getSchema().getId());

        assertThatThrownBy(() -> album.containsKey(null))
            .hasMessage("null values are illegal on method parameter \"key\"");
        assertEquals(false, album.containsKey("Key0"));

        assertThatThrownBy(() -> album.containsValue(null))
            .hasMessage("null values are illegal on method parameter \"value\"");
        assertEquals(false, album.containsValue("some value"));

        assertThatThrownBy(() -> album.get(null))
            .hasMessage("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for get()");

        assertThatThrownBy(() -> album.put(null, null))
            .hasMessage("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for put()");

        assertThatThrownBy(() -> album.put("KeyNull", null))
            .hasMessage("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"KeyNull\"" + " for put()");

        AxContextAlbum axContextAlbumRo = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
            false, simpleStringSchema.getKey());
        ContextAlbum albumRo = new ContextAlbumImpl(axContextAlbumRo, distributor, new LinkedHashMap<String, Object>());

        assertThatThrownBy(() -> albumRo.put("KeyReadOnly", "A value for a Read Only Album"))
            .hasMessage("album \"TestContextAlbum:0.0.1\" put() not allowed on read only albums "
                + "for key=\"KeyReadOnly\", value=\"A value for a Read Only Album");

        Map<String, Object> putAllData = new LinkedHashMap<>();
        putAllData.put("AllKey0", "vaue of AllKey0");
        putAllData.put("AllKey1", "vaue of AllKey1");
        putAllData.put("AllKey2", "vaue of AllKey2");

        assertThatThrownBy(() -> albumRo.putAll(putAllData))
            .hasMessage("album \"TestContextAlbum:0.0.1\" putAll() not allowed on read only albums");

        assertThatThrownBy(() -> albumRo.remove("AllKey0")).hasMessage(
            "album \"TestContextAlbum:0.0.1\" remove() not allowed " + "on read only albums for key=\"AllKey0\"");

        assertThatThrownBy(() -> album.remove(null))
            .hasMessage("null values are illegal on method parameter \"keyID\"");

        assertThatThrownBy(albumRo::clear)
            .hasMessage("album \"TestContextAlbum:0.0.1\" clear() not allowed on read only albums");

        // The following locking tests pass because the locking protects access to Key0
        // across all
        // copies of the distributed album whether the key exists or not
        album.lockForReading("Key0");
        assertEquals(null, album.get("Key0"));
        album.unlockForReading("Key0");
        assertEquals(null, album.get("Key0"));

        album.lockForWriting("Key0");
        assertEquals(null, album.get("Key0"));
        album.unlockForWriting("Key0");
        assertEquals(null, album.get("Key0"));

        // Test write access, trivial test because Integration Test does
        // a full test of access locking over albums in different JVMs
        album.lockForWriting("Key0");
        assertEquals(null, album.get("Key0"));
        album.put("Key0", "value of Key0");
        assertEquals("value of Key0", album.get("Key0"));
        album.unlockForWriting("Key0");
        assertEquals("value of Key0", album.get("Key0"));

        // Test read access, trivial test because Integration Test does
        // a full test of access locking over albums in different JVMs
        album.lockForReading("Key0");
        assertEquals("value of Key0", album.get("Key0"));
        album.unlockForReading("Key0");

        AxArtifactKey somePolicyKey = new AxArtifactKey("MyPolicy", "0.0.1");
        AxReferenceKey somePolicyState = new AxReferenceKey(somePolicyKey, "SomeState");

        AxConcept[] userArtifactStack = { somePolicyKey, somePolicyState };
        album.setUserArtifactStack(userArtifactStack);
        assertEquals("MyPolicy:0.0.1", album.getUserArtifactStack()[0].getId());
        assertEquals("MyPolicy:0.0.1:NULL:SomeState", album.getUserArtifactStack()[1].getId());

        assertEquals(true, album.keySet().contains("Key0"));
        assertEquals(true, album.values().contains("value of Key0"));
        assertEquals(1, album.entrySet().size());

        // The flush() operation fails because the distributor is not initialized with
        // the album which
        // is fine for unit test
        assertThatThrownBy(album::flush).hasMessage("map flush failed, supplied map is null");
        assertEquals(1, album.size());
        assertEquals(false, album.isEmpty());

        album.put("Key0", "New value of Key0");
        assertEquals("New value of Key0", album.get("Key0"));

        album.putAll(putAllData);

        putAllData.put("AllKey3", null);
        assertThatThrownBy(() -> album.putAll(putAllData))
            .hasMessage("album \"TestContextAlbum:0.0.1\" null values are illegal on key " + "\"AllKey3\" for put()");
        assertEquals("New value of Key0", album.remove("Key0"));

        album.clear();
        assertTrue(album.isEmpty());

        ModelService.clear();
    }

    @Test
    public void testCompareToEqualsHash() throws ContextException {
        AxContextSchemas schemas = new AxContextSchemas();
        AxContextSchema simpleIntSchema = new AxContextSchema(new AxArtifactKey("SimpleIntSchema", "0.0.1"), "JAVA",
            "java.lang.Integer");
        schemas.getSchemasMap().put(simpleIntSchema.getKey(), simpleIntSchema);
        AxContextSchema simpleStringSchema = new AxContextSchema(new AxArtifactKey("SimpleStringSchema", "0.0.1"),
            "JAVA", "java.lang.String");
        schemas.getSchemasMap().put(simpleStringSchema.getKey(), simpleStringSchema);
        ModelService.registerModel(AxContextSchemas.class, schemas);

        AxContextAlbum axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy",
            true, AxArtifactKey.getNullKey());

        axContextAlbum.setItemSchema(simpleIntSchema.getKey());
        Distributor distributor = new JvmLocalDistributor();
        distributor.init(axContextAlbum.getKey());
        ContextAlbumImpl album = new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<String, Object>());

        assertNotEquals(0, album.hashCode());

        assertEquals(0, album.compareTo(album));
        assertEquals(1, album.compareTo(null));

        assertEquals(album, album);
        assertNotEquals(album, new DummyContextAlbumImpl());

        ContextAlbumImpl otherAlbum = new ContextAlbumImpl(axContextAlbum, distributor,
            new LinkedHashMap<String, Object>());
        assertEquals(album, otherAlbum);

        otherAlbum.put("Key", 123);
        assertNotEquals(album, otherAlbum);

        assertThatThrownBy(() -> {
            ContextAlbumImpl otherAlbumBad = new ContextAlbumImpl(axContextAlbum, distributor,
                new LinkedHashMap<String, Object>());
            otherAlbumBad.put("Key", "BadValue");
        }).hasMessage("Failed to set context value for key \"Key\" in album \"TestContextAlbum:0.0.1\": "
            + "TestContextAlbum:0.0.1: object \"BadValue\" of class \"java.lang.String\" "
            + "not compatible with class \"java.lang.Integer\"");
        AxContextAlbum otherAxContextAlbum = new AxContextAlbum(new AxArtifactKey("OtherTestContextAlbum", "0.0.1"),
            "Policy", true, AxArtifactKey.getNullKey());

        otherAxContextAlbum.setItemSchema(simpleStringSchema.getKey());
        otherAlbum = new ContextAlbumImpl(otherAxContextAlbum, distributor, new LinkedHashMap<String, Object>());
        assertNotEquals(album, otherAlbum);

        assertThatThrownBy(album::flush).hasMessage("map flush failed, supplied map is null");
        AxContextAlbums albums = new AxContextAlbums();
        ModelService.registerModel(AxContextAlbums.class, albums);
        albums.getAlbumsMap().put(axContextAlbum.getKey(), axContextAlbum);
        distributor.createContextAlbum(album.getKey());

        album.flush();

        ModelService.clear();
    }
}