2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2021 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.apex.model.contextmodel.concepts;
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
30 import org.junit.Test;
31 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
36 * Context album tests.
38 * @author Liam Fallon (liam.fallon@ericsson.com)
40 public class ContextAlbumsTest {
43 public void testNewAxContectAlbum() {
44 assertNotNull(new AxContextAlbum());
45 assertNotNull(new AxContextAlbum(new AxArtifactKey()));
46 assertNotNull(new AxContextAlbum(new AxArtifactKey(), "AlbumScope", false, new AxArtifactKey()));
50 public void testContextAlbums() {
51 final AxArtifactKey albumKey = new AxArtifactKey("AlbumName", "0.0.1");
52 final AxArtifactKey albumSchemaKey = new AxArtifactKey("AlbumSchemaName", "0.0.1");
54 final AxContextAlbum album = new AxContextAlbum(albumKey, "AlbumScope", false, albumSchemaKey);
57 final AxArtifactKey newKey = new AxArtifactKey("NewAlbumName", "0.0.1");
59 assertEquals("NewAlbumName:0.0.1", album.getKey().getId());
60 assertEquals("NewAlbumName:0.0.1", album.getKeys().get(0).getId());
61 album.setKey(albumKey);
63 assertThatThrownBy(() -> album.setScope(""))
64 .hasMessage("parameter \"scope\": value \"\", does not match regular expression "
65 + "\"[A-Za-z0-9\\-_]+\"");
67 album.setScope("NewAlbumScope");
68 assertEquals("NewAlbumScope", album.getScope());
70 assertEquals(false, album.isWritable());
71 album.setWritable(true);
72 assertEquals(true, album.isWritable());
74 final AxArtifactKey newSchemaKey = new AxArtifactKey("NewAlbumSchemaName", "0.0.1");
75 album.setItemSchema(newSchemaKey);
76 assertEquals("NewAlbumSchemaName:0.0.1", album.getItemSchema().getId());
77 album.setItemSchema(albumSchemaKey);
80 private AxContextAlbum setTestAlbum() {
81 final AxArtifactKey newKey = new AxArtifactKey("NewAlbumName", "0.0.1");
82 final AxArtifactKey newSchemaKey = new AxArtifactKey("NewAlbumSchemaName", "0.0.1");
84 final AxContextAlbum album = new AxContextAlbum(newKey, "AlbumScope", false, newSchemaKey);
86 album.setScope("NewAlbumScope");
87 album.setWritable(true);
93 public void testAxvalidationAlbum() {
94 final AxContextAlbum album = setTestAlbum();
95 AxValidationResult result = new AxValidationResult();
96 result = album.validate(result);
97 assertEquals(ValidationResult.VALID, result.getValidationResult());
99 album.setKey(AxArtifactKey.getNullKey());
100 result = new AxValidationResult();
101 result = album.validate(result);
102 assertEquals(ValidationResult.INVALID, result.getValidationResult());
104 final AxArtifactKey newKey = new AxArtifactKey("NewAlbumName", "0.0.1");
105 album.setKey(newKey);
106 result = new AxValidationResult();
107 result = album.validate(result);
108 assertEquals(ValidationResult.VALID, result.getValidationResult());
110 album.setScope("UNDEFINED");
111 result = new AxValidationResult();
112 result = album.validate(result);
113 assertEquals(ValidationResult.INVALID, result.getValidationResult());
115 album.setScope("NewAlbumScope");
116 result = new AxValidationResult();
117 result = album.validate(result);
118 assertEquals(ValidationResult.VALID, result.getValidationResult());
120 album.setItemSchema(AxArtifactKey.getNullKey());
121 result = new AxValidationResult();
122 result = album.validate(result);
123 assertEquals(ValidationResult.INVALID, result.getValidationResult());
125 final AxArtifactKey albumSchemaKey = new AxArtifactKey("AlbumSchemaName", "0.0.1");
126 album.setItemSchema(albumSchemaKey);
127 result = new AxValidationResult();
128 result = album.validate(result);
129 assertEquals(ValidationResult.VALID, result.getValidationResult());
134 public void testEqualsAlbum() {
135 final AxContextAlbum album = setTestAlbum();
136 final AxArtifactKey newKey = new AxArtifactKey("NewAlbumName", "0.0.1");
137 final AxArtifactKey albumSchemaKey = new AxArtifactKey("AlbumSchemaName", "0.0.1");
138 album.setItemSchema(albumSchemaKey);
140 final AxContextAlbum clonedAlbum = new AxContextAlbum(album);
141 assertEquals("AxContextAlbum:(key=AxArtifactKey:(name=NewAlbumName,version=0.0.1),"
142 + "scope=NewAlbumScope,isWritable=true,itemSchema="
143 + "AxArtifactKey:(name=AlbumSchemaName,version=0.0.1))", clonedAlbum.toString());
145 assertNotEquals(0, album.hashCode());
146 // disabling sonar because this code tests the equals() method
147 assertEquals(album, album); // NOSONAR
148 assertEquals(album, clonedAlbum);
149 assertNotNull(album);
150 assertNotEquals(album, (Object) "Hello");
151 assertNotEquals(album, new AxContextAlbum(new AxArtifactKey(), "Scope", false, AxArtifactKey.getNullKey()));
152 assertNotEquals(album, new AxContextAlbum(newKey, "Scope", false, AxArtifactKey.getNullKey()));
153 assertNotEquals(album, new AxContextAlbum(newKey, "NewAlbumScope", false, AxArtifactKey.getNullKey()));
154 assertNotEquals(album, new AxContextAlbum(newKey, "NewAlbumScope", true, AxArtifactKey.getNullKey()));
155 assertEquals(album, new AxContextAlbum(newKey, "NewAlbumScope", true, albumSchemaKey));
157 assertEquals(0, album.compareTo(album));
158 assertEquals(0, album.compareTo(clonedAlbum));
159 assertNotEquals(0, album.compareTo(null));
160 assertNotEquals(0, album.compareTo(new AxArtifactKey()));
161 assertNotEquals(0, album.compareTo(
162 new AxContextAlbum(new AxArtifactKey(), "Scope", false, AxArtifactKey.getNullKey())));
163 assertNotEquals(0, album.compareTo(new AxContextAlbum(newKey, "Scope", false, AxArtifactKey.getNullKey())));
164 assertNotEquals(0, album
165 .compareTo(new AxContextAlbum(newKey, "NewAlbumScope", false, AxArtifactKey.getNullKey())));
167 album.compareTo(new AxContextAlbum(newKey, "NewAlbumScope", true, AxArtifactKey.getNullKey())));
168 assertEquals(0, album.compareTo(new AxContextAlbum(newKey, "NewAlbumScope", true, albumSchemaKey)));
172 public void testMultipleAlbums() {
173 final AxContextAlbums albums = new AxContextAlbums();
174 final AxContextAlbum album = setTestAlbum();
175 final AxArtifactKey newKey = new AxArtifactKey("NewAlbumName", "0.0.1");
176 AxValidationResult result = new AxValidationResult();
177 result = albums.validate(result);
178 assertEquals(ValidationResult.INVALID, result.getValidationResult());
180 // Observation, no albums in album map
181 albums.setKey(new AxArtifactKey("AlbumsKey", "0.0.1"));
182 result = new AxValidationResult();
183 result = albums.validate(result);
184 assertEquals(ValidationResult.OBSERVATION, result.getValidationResult());
186 albums.getAlbumsMap().put(newKey, album);
187 result = new AxValidationResult();
188 result = albums.validate(result);
189 assertEquals(ValidationResult.VALID, result.getValidationResult());
191 albums.getAlbumsMap().put(AxArtifactKey.getNullKey(), null);
192 result = new AxValidationResult();
193 result = albums.validate(result);
194 assertEquals(ValidationResult.INVALID, result.getValidationResult());
196 albums.getAlbumsMap().remove(AxArtifactKey.getNullKey());
197 result = new AxValidationResult();
198 result = albums.validate(result);
199 assertEquals(ValidationResult.VALID, result.getValidationResult());
201 albums.getAlbumsMap().put(new AxArtifactKey("NullValueKey", "0.0.1"), null);
202 result = new AxValidationResult();
203 result = albums.validate(result);
204 assertEquals(ValidationResult.INVALID, result.getValidationResult());
206 albums.getAlbumsMap().remove(new AxArtifactKey("NullValueKey", "0.0.1"));
207 result = new AxValidationResult();
208 result = albums.validate(result);
209 assertEquals(ValidationResult.VALID, result.getValidationResult());
214 public void testClonedAlbums() {
215 final AxContextAlbums albums = new AxContextAlbums();
216 final AxContextAlbum album = setTestAlbum();
217 final AxArtifactKey newKey = new AxArtifactKey("NewAlbumName", "0.0.1");
218 albums.setKey(new AxArtifactKey("AlbumsKey", "0.0.1"));
219 albums.getAlbumsMap().put(newKey, album);
222 final AxContextAlbums clonedAlbums = new AxContextAlbums(albums);
223 assertTrue(clonedAlbums.toString().startsWith(
224 "AxContextAlbums:(AxContextAlbums:(key=AxArtifactKey:(name=AlbumsKey,version=0.0.1)"));
226 assertNotEquals(0, albums.hashCode());
228 assertEquals(albums, clonedAlbums);
229 assertNotNull(albums);
230 assertNotEquals(albums, (Object) "Hello");
231 assertNotEquals(albums, new AxContextAlbums(new AxArtifactKey()));
233 assertEquals(0, albums.compareTo(albums));
234 assertEquals(0, albums.compareTo(clonedAlbums));
235 assertNotEquals(0, albums.compareTo(null));
236 assertNotEquals(0, albums.compareTo(new AxArtifactKey()));
237 assertNotEquals(0, albums.compareTo(new AxContextAlbums(new AxArtifactKey())));
239 clonedAlbums.get(newKey).setScope("YetAnotherScope");
240 assertNotEquals(0, albums.compareTo(clonedAlbums));
242 assertEquals("NewAlbumName", albums.get("NewAlbumName").getKey().getName());
243 assertEquals("NewAlbumName", albums.get("NewAlbumName", "0.0.1").getKey().getName());
244 assertEquals(1, albums.getAll("NewAlbumName", "0.0.1").size());
245 assertEquals(0, albums.getAll("NonExistantAlbumName").size());