2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2019-2020 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 testContextAlbums() {
44 assertNotNull(new AxContextAlbum());
45 assertNotNull(new AxContextAlbum(new AxArtifactKey()));
46 assertNotNull(new AxContextAlbum(new AxArtifactKey(), "AlbumScope", false, new AxArtifactKey()));
48 final AxArtifactKey albumKey = new AxArtifactKey("AlbumName", "0.0.1");
49 final AxArtifactKey albumSchemaKey = new AxArtifactKey("AlbumSchemaName", "0.0.1");
51 final AxContextAlbum album = new AxContextAlbum(albumKey, "AlbumScope", false, albumSchemaKey);
54 final AxArtifactKey newKey = new AxArtifactKey("NewAlbumName", "0.0.1");
56 assertEquals("NewAlbumName:0.0.1", album.getKey().getId());
57 assertEquals("NewAlbumName:0.0.1", album.getKeys().get(0).getId());
58 album.setKey(albumKey);
60 assertThatThrownBy(() -> album.setScope(""))
61 .hasMessage("parameter \"scope\": value \"\", does not match regular expression "
62 + "\"[A-Za-z0-9\\-_]+\"");
64 album.setScope("NewAlbumScope");
65 assertEquals("NewAlbumScope", album.getScope());
67 assertEquals(false, album.isWritable());
68 album.setWritable(true);
69 assertEquals(true, album.isWritable());
71 final AxArtifactKey newSchemaKey = new AxArtifactKey("NewAlbumSchemaName", "0.0.1");
72 album.setItemSchema(newSchemaKey);
73 assertEquals("NewAlbumSchemaName:0.0.1", album.getItemSchema().getId());
74 album.setItemSchema(albumSchemaKey);
76 AxValidationResult result = new AxValidationResult();
77 result = album.validate(result);
78 assertEquals(ValidationResult.VALID, result.getValidationResult());
80 album.setKey(AxArtifactKey.getNullKey());
81 result = new AxValidationResult();
82 result = album.validate(result);
83 assertEquals(ValidationResult.INVALID, result.getValidationResult());
86 result = new AxValidationResult();
87 result = album.validate(result);
88 assertEquals(ValidationResult.VALID, result.getValidationResult());
90 album.setScope("UNDEFINED");
91 result = new AxValidationResult();
92 result = album.validate(result);
93 assertEquals(ValidationResult.INVALID, result.getValidationResult());
95 album.setScope("NewAlbumScope");
96 result = new AxValidationResult();
97 result = album.validate(result);
98 assertEquals(ValidationResult.VALID, result.getValidationResult());
100 album.setItemSchema(AxArtifactKey.getNullKey());
101 result = new AxValidationResult();
102 result = album.validate(result);
103 assertEquals(ValidationResult.INVALID, result.getValidationResult());
105 album.setItemSchema(albumSchemaKey);
106 result = new AxValidationResult();
107 result = album.validate(result);
108 assertEquals(ValidationResult.VALID, result.getValidationResult());
112 final AxContextAlbum clonedAlbum = new AxContextAlbum(album);
113 assertEquals("AxContextAlbum:(key=AxArtifactKey:(name=NewAlbumName,version=0.0.1),"
114 + "scope=NewAlbumScope,isWritable=true,itemSchema="
115 + "AxArtifactKey:(name=AlbumSchemaName,version=0.0.1))", clonedAlbum.toString());
117 assertNotEquals(0, album.hashCode());
119 assertEquals(album, album);
120 assertEquals(album, clonedAlbum);
121 assertNotNull(album);
122 assertNotEquals(album, (Object) "Hello");
123 assertNotEquals(album, new AxContextAlbum(new AxArtifactKey(), "Scope", false, AxArtifactKey.getNullKey()));
124 assertNotEquals(album, new AxContextAlbum(newKey, "Scope", false, AxArtifactKey.getNullKey()));
125 assertNotEquals(album, new AxContextAlbum(newKey, "NewAlbumScope", false, AxArtifactKey.getNullKey()));
126 assertNotEquals(album, new AxContextAlbum(newKey, "NewAlbumScope", true, AxArtifactKey.getNullKey()));
127 assertEquals(album, new AxContextAlbum(newKey, "NewAlbumScope", true, albumSchemaKey));
129 assertEquals(0, album.compareTo(album));
130 assertEquals(0, album.compareTo(clonedAlbum));
131 assertNotEquals(0, album.compareTo(null));
132 assertNotEquals(0, album.compareTo(new AxArtifactKey()));
133 assertNotEquals(0, album.compareTo(
134 new AxContextAlbum(new AxArtifactKey(), "Scope", false, AxArtifactKey.getNullKey())));
135 assertNotEquals(0, album.compareTo(new AxContextAlbum(newKey, "Scope", false, AxArtifactKey.getNullKey())));
136 assertNotEquals(0, album
137 .compareTo(new AxContextAlbum(newKey, "NewAlbumScope", false, AxArtifactKey.getNullKey())));
139 album.compareTo(new AxContextAlbum(newKey, "NewAlbumScope", true, AxArtifactKey.getNullKey())));
140 assertEquals(0, album.compareTo(new AxContextAlbum(newKey, "NewAlbumScope", true, albumSchemaKey)));
142 final AxContextAlbums albums = new AxContextAlbums();
143 result = new AxValidationResult();
144 result = albums.validate(result);
145 assertEquals(ValidationResult.INVALID, result.getValidationResult());
147 // Observation, no albums in album map
148 albums.setKey(new AxArtifactKey("AlbumsKey", "0.0.1"));
149 result = new AxValidationResult();
150 result = albums.validate(result);
151 assertEquals(ValidationResult.OBSERVATION, result.getValidationResult());
153 albums.getAlbumsMap().put(newKey, album);
154 result = new AxValidationResult();
155 result = albums.validate(result);
156 assertEquals(ValidationResult.VALID, result.getValidationResult());
158 albums.getAlbumsMap().put(AxArtifactKey.getNullKey(), null);
159 result = new AxValidationResult();
160 result = albums.validate(result);
161 assertEquals(ValidationResult.INVALID, result.getValidationResult());
163 albums.getAlbumsMap().remove(AxArtifactKey.getNullKey());
164 result = new AxValidationResult();
165 result = albums.validate(result);
166 assertEquals(ValidationResult.VALID, result.getValidationResult());
168 albums.getAlbumsMap().put(new AxArtifactKey("NullValueKey", "0.0.1"), null);
169 result = new AxValidationResult();
170 result = albums.validate(result);
171 assertEquals(ValidationResult.INVALID, result.getValidationResult());
173 albums.getAlbumsMap().remove(new AxArtifactKey("NullValueKey", "0.0.1"));
174 result = new AxValidationResult();
175 result = albums.validate(result);
176 assertEquals(ValidationResult.VALID, result.getValidationResult());
180 final AxContextAlbums clonedAlbums = new AxContextAlbums(albums);
181 assertTrue(clonedAlbums.toString().startsWith(
182 "AxContextAlbums:(AxContextAlbums:(key=AxArtifactKey:(name=AlbumsKey,version=0.0.1)"));
184 assertNotEquals(0, albums.hashCode());
186 assertEquals(albums, albums);
187 assertEquals(albums, clonedAlbums);
188 assertNotNull(albums);
189 assertNotEquals(albums, (Object) "Hello");
190 assertNotEquals(albums, new AxContextAlbums(new AxArtifactKey()));
192 assertEquals(0, albums.compareTo(albums));
193 assertEquals(0, albums.compareTo(clonedAlbums));
194 assertNotEquals(0, albums.compareTo(null));
195 assertNotEquals(0, albums.compareTo(new AxArtifactKey()));
196 assertNotEquals(0, albums.compareTo(new AxContextAlbums(new AxArtifactKey())));
198 clonedAlbums.get(newKey).setScope("YetAnotherScope");
199 assertNotEquals(0, albums.compareTo(clonedAlbums));
201 assertEquals("NewAlbumName", albums.get("NewAlbumName").getKey().getName());
202 assertEquals("NewAlbumName", albums.get("NewAlbumName", "0.0.1").getKey().getName());
203 assertEquals(1, albums.getAll("NewAlbumName", "0.0.1").size());
204 assertEquals(0, albums.getAll("NonExistantAlbumName").size());