2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2021 Nordix Foundation.
5 * Modifications Copyright (C) 2021 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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.apex.model.contextmodel.concepts;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotEquals;
29 import static org.junit.Assert.assertNotNull;
31 import org.junit.Test;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
37 * Context album tests.
39 * @author Liam Fallon (liam.fallon@ericsson.com)
41 public class ContextAlbumsTest {
44 public void testNewAxContectAlbum() {
45 assertNotNull(new AxContextAlbum());
46 assertNotNull(new AxContextAlbum(new AxArtifactKey()));
47 assertNotNull(new AxContextAlbum(new AxArtifactKey(), "AlbumScope", false, new AxArtifactKey()));
51 public void testContextAlbums() {
52 final AxArtifactKey albumKey = new AxArtifactKey("AlbumName", "0.0.1");
53 final AxArtifactKey albumSchemaKey = new AxArtifactKey("AlbumSchemaName", "0.0.1");
55 final AxContextAlbum album = new AxContextAlbum(albumKey, "AlbumScope", false, albumSchemaKey);
58 final AxArtifactKey newKey = new AxArtifactKey("NewAlbumName", "0.0.1");
60 assertEquals("NewAlbumName:0.0.1", album.getKey().getId());
61 assertEquals("NewAlbumName:0.0.1", album.getKeys().get(0).getId());
62 album.setKey(albumKey);
64 assertThatThrownBy(() -> album.setScope(""))
65 .hasMessage("parameter \"scope\": value \"\", does not match regular expression "
66 + "\"[A-Za-z0-9\\-_]+\"");
68 album.setScope("NewAlbumScope");
69 assertEquals("NewAlbumScope", album.getScope());
71 assertEquals(false, album.isWritable());
72 album.setWritable(true);
73 assertEquals(true, album.isWritable());
75 final AxArtifactKey newSchemaKey = new AxArtifactKey("NewAlbumSchemaName", "0.0.1");
76 album.setItemSchema(newSchemaKey);
77 assertEquals("NewAlbumSchemaName:0.0.1", album.getItemSchema().getId());
78 album.setItemSchema(albumSchemaKey);
81 private AxContextAlbum setTestAlbum() {
82 final AxArtifactKey newKey = new AxArtifactKey("NewAlbumName", "0.0.1");
83 final AxArtifactKey newSchemaKey = new AxArtifactKey("NewAlbumSchemaName", "0.0.1");
85 final AxContextAlbum album = new AxContextAlbum(newKey, "AlbumScope", false, newSchemaKey);
87 album.setScope("NewAlbumScope");
88 album.setWritable(true);
94 public void testAxvalidationAlbum() {
95 final AxContextAlbum album = setTestAlbum();
96 AxValidationResult result = new AxValidationResult();
97 result = album.validate(result);
98 assertEquals(ValidationResult.VALID, result.getValidationResult());
100 album.setKey(AxArtifactKey.getNullKey());
101 result = new AxValidationResult();
102 result = album.validate(result);
103 assertEquals(ValidationResult.INVALID, result.getValidationResult());
105 final AxArtifactKey newKey = new AxArtifactKey("NewAlbumName", "0.0.1");
106 album.setKey(newKey);
107 result = new AxValidationResult();
108 result = album.validate(result);
109 assertEquals(ValidationResult.VALID, result.getValidationResult());
111 album.setScope("UNDEFINED");
112 result = new AxValidationResult();
113 result = album.validate(result);
114 assertEquals(ValidationResult.INVALID, result.getValidationResult());
116 album.setScope("NewAlbumScope");
117 result = new AxValidationResult();
118 result = album.validate(result);
119 assertEquals(ValidationResult.VALID, result.getValidationResult());
121 album.setItemSchema(AxArtifactKey.getNullKey());
122 result = new AxValidationResult();
123 result = album.validate(result);
124 assertEquals(ValidationResult.INVALID, result.getValidationResult());
126 final AxArtifactKey albumSchemaKey = new AxArtifactKey("AlbumSchemaName", "0.0.1");
127 album.setItemSchema(albumSchemaKey);
128 result = new AxValidationResult();
129 result = album.validate(result);
130 assertEquals(ValidationResult.VALID, result.getValidationResult());
135 public void testEqualsAlbum() {
136 final AxContextAlbum album = setTestAlbum();
137 final AxArtifactKey newKey = new AxArtifactKey("NewAlbumName", "0.0.1");
138 final AxArtifactKey albumSchemaKey = new AxArtifactKey("AlbumSchemaName", "0.0.1");
139 album.setItemSchema(albumSchemaKey);
141 final AxContextAlbum clonedAlbum = new AxContextAlbum(album);
142 assertEquals("AxContextAlbum(key=AxArtifactKey:(name=NewAlbumName,version=0.0.1), "
143 + "scope=NewAlbumScope, isWritable=true, itemSchema="
144 + "AxArtifactKey:(name=AlbumSchemaName,version=0.0.1))", clonedAlbum.toString());
146 assertNotEquals(0, album.hashCode());
147 // disabling sonar because this code tests the equals() method
148 assertEquals(album, album); // NOSONAR
149 assertEquals(album, clonedAlbum);
150 assertNotNull(album);
151 assertNotEquals(album, (Object) "Hello");
152 assertNotEquals(album, new AxContextAlbum(new AxArtifactKey(), "Scope", false, AxArtifactKey.getNullKey()));
153 assertNotEquals(album, new AxContextAlbum(newKey, "Scope", false, AxArtifactKey.getNullKey()));
154 assertNotEquals(album, new AxContextAlbum(newKey, "NewAlbumScope", false, AxArtifactKey.getNullKey()));
155 assertNotEquals(album, new AxContextAlbum(newKey, "NewAlbumScope", true, AxArtifactKey.getNullKey()));
156 assertEquals(album, new AxContextAlbum(newKey, "NewAlbumScope", true, albumSchemaKey));
158 assertEquals(0, album.compareTo(album));
159 assertEquals(0, album.compareTo(clonedAlbum));
160 assertNotEquals(0, album.compareTo(null));
161 assertNotEquals(0, album.compareTo(new AxArtifactKey()));
162 assertNotEquals(0, album.compareTo(
163 new AxContextAlbum(new AxArtifactKey(), "Scope", false, AxArtifactKey.getNullKey())));
164 assertNotEquals(0, album.compareTo(new AxContextAlbum(newKey, "Scope", false, AxArtifactKey.getNullKey())));
165 assertNotEquals(0, album
166 .compareTo(new AxContextAlbum(newKey, "NewAlbumScope", false, AxArtifactKey.getNullKey())));
168 album.compareTo(new AxContextAlbum(newKey, "NewAlbumScope", true, AxArtifactKey.getNullKey())));
169 assertEquals(0, album.compareTo(new AxContextAlbum(newKey, "NewAlbumScope", true, albumSchemaKey)));
173 public void testMultipleAlbums() {
174 final AxContextAlbums albums = new AxContextAlbums();
175 final AxContextAlbum album = setTestAlbum();
176 final AxArtifactKey newKey = new AxArtifactKey("NewAlbumName", "0.0.1");
177 AxValidationResult result = new AxValidationResult();
178 result = albums.validate(result);
179 assertEquals(ValidationResult.INVALID, result.getValidationResult());
181 // Observation, no albums in album map
182 albums.setKey(new AxArtifactKey("AlbumsKey", "0.0.1"));
183 result = new AxValidationResult();
184 result = albums.validate(result);
185 assertEquals(ValidationResult.OBSERVATION, result.getValidationResult());
187 albums.getAlbumsMap().put(newKey, album);
188 result = new AxValidationResult();
189 result = albums.validate(result);
190 assertEquals(ValidationResult.VALID, result.getValidationResult());
192 albums.getAlbumsMap().put(AxArtifactKey.getNullKey(), null);
193 result = new AxValidationResult();
194 result = albums.validate(result);
195 assertEquals(ValidationResult.INVALID, result.getValidationResult());
197 albums.getAlbumsMap().remove(AxArtifactKey.getNullKey());
198 result = new AxValidationResult();
199 result = albums.validate(result);
200 assertEquals(ValidationResult.VALID, result.getValidationResult());
202 albums.getAlbumsMap().put(new AxArtifactKey("NullValueKey", "0.0.1"), null);
203 result = new AxValidationResult();
204 result = albums.validate(result);
205 assertEquals(ValidationResult.INVALID, result.getValidationResult());
207 albums.getAlbumsMap().remove(new AxArtifactKey("NullValueKey", "0.0.1"));
208 result = new AxValidationResult();
209 result = albums.validate(result);
210 assertEquals(ValidationResult.VALID, result.getValidationResult());
215 public void testClonedAlbums() {
216 final AxContextAlbums albums = new AxContextAlbums();
217 final AxContextAlbum album = setTestAlbum();
218 final AxArtifactKey newKey = new AxArtifactKey("NewAlbumName", "0.0.1");
219 albums.setKey(new AxArtifactKey("AlbumsKey", "0.0.1"));
220 albums.getAlbumsMap().put(newKey, album);
223 final AxContextAlbums clonedAlbums = new AxContextAlbums(albums);
224 assertThat(clonedAlbums.toString()).startsWith(
225 "AxContextAlbums(key=AxArtifactKey:(name=AlbumsKey,version=0.0.1)");
227 assertNotEquals(0, albums.hashCode());
229 assertEquals(albums, clonedAlbums);
230 assertNotNull(albums);
231 assertNotEquals(albums, (Object) "Hello");
232 assertNotEquals(albums, new AxContextAlbums(new AxArtifactKey()));
234 assertEquals(0, albums.compareTo(albums));
235 assertEquals(0, albums.compareTo(clonedAlbums));
236 assertNotEquals(0, albums.compareTo(null));
237 assertNotEquals(0, albums.compareTo(new AxArtifactKey()));
238 assertNotEquals(0, albums.compareTo(new AxContextAlbums(new AxArtifactKey())));
240 clonedAlbums.get(newKey).setScope("YetAnotherScope");
241 assertNotEquals(0, albums.compareTo(clonedAlbums));
243 assertEquals("NewAlbumName", albums.get("NewAlbumName").getKey().getName());
244 assertEquals("NewAlbumName", albums.get("NewAlbumName", "0.0.1").getKey().getName());
245 assertEquals(1, albums.getAll("NewAlbumName", "0.0.1").size());
246 assertEquals(0, albums.getAll("NonExistantAlbumName").size());