2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.apex.model.contextmodel.concepts;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotEquals;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
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;
34 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbum;
35 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums;
38 * Context album tests.
40 * @author Liam Fallon (liam.fallon@ericsson.com)
42 public class ContextAlbumsTest {
45 public void testContextAlbums() {
46 assertNotNull(new AxContextAlbum());
47 assertNotNull(new AxContextAlbum(new AxArtifactKey()));
48 assertNotNull(new AxContextAlbum(new AxArtifactKey(), "AlbumScope", false, new AxArtifactKey()));
50 final AxArtifactKey albumKey = new AxArtifactKey("AlbumName", "0.0.1");
51 final AxArtifactKey albumSchemaKey = new AxArtifactKey("AlbumSchemaName", "0.0.1");
53 final AxContextAlbum album = new AxContextAlbum(albumKey, "AlbumScope", false, albumSchemaKey);
56 final AxArtifactKey newKey = new AxArtifactKey("NewAlbumName", "0.0.1");
58 assertEquals("NewAlbumName:0.0.1", album.getKey().getId());
59 assertEquals("NewAlbumName:0.0.1", album.getKeys().get(0).getId());
60 album.setKey(albumKey);
64 fail("test should throw an exception here");
65 } catch (final Exception e) {
66 assertEquals("parameter \"scope\": value \"\", does not match regular expression \"[A-Za-z0-9\\-_]+\"",
70 album.setScope("NewAlbumScope");
71 assertEquals("NewAlbumScope", album.getScope());
73 assertEquals(false, album.isWritable());
74 album.setWritable(true);
75 assertEquals(true, album.isWritable());
77 final AxArtifactKey newSchemaKey = new AxArtifactKey("NewAlbumSchemaName", "0.0.1");
78 album.setItemSchema(newSchemaKey);
79 assertEquals("NewAlbumSchemaName:0.0.1", album.getItemSchema().getId());
80 album.setItemSchema(albumSchemaKey);
82 AxValidationResult result = new AxValidationResult();
83 result = album.validate(result);
84 assertEquals(ValidationResult.VALID, result.getValidationResult());
86 album.setKey(AxArtifactKey.getNullKey());
87 result = new AxValidationResult();
88 result = album.validate(result);
89 assertEquals(ValidationResult.INVALID, result.getValidationResult());
92 result = new AxValidationResult();
93 result = album.validate(result);
94 assertEquals(ValidationResult.VALID, result.getValidationResult());
96 album.setScope("UNDEFINED");
97 result = new AxValidationResult();
98 result = album.validate(result);
99 assertEquals(ValidationResult.INVALID, result.getValidationResult());
101 album.setScope("NewAlbumScope");
102 result = new AxValidationResult();
103 result = album.validate(result);
104 assertEquals(ValidationResult.VALID, result.getValidationResult());
106 album.setItemSchema(AxArtifactKey.getNullKey());
107 result = new AxValidationResult();
108 result = album.validate(result);
109 assertEquals(ValidationResult.INVALID, result.getValidationResult());
111 album.setItemSchema(albumSchemaKey);
112 result = new AxValidationResult();
113 result = album.validate(result);
114 assertEquals(ValidationResult.VALID, result.getValidationResult());
118 final AxContextAlbum clonedAlbum = new AxContextAlbum(album);
119 assertEquals("AxContextAlbum:(key=AxArtifactKey:(name=NewAlbumName,version=0.0.1),"
120 + "scope=NewAlbumScope,isWritable=true,itemSchema="
121 + "AxArtifactKey:(name=AlbumSchemaName,version=0.0.1))", clonedAlbum.toString());
123 assertFalse(album.hashCode() == 0);
125 assertTrue(album.equals(album));
126 assertTrue(album.equals(clonedAlbum));
127 assertFalse(album.equals(null));
128 assertFalse(album.equals("Hello"));
129 assertFalse(album.equals(new AxContextAlbum(new AxArtifactKey(), "Scope", false, AxArtifactKey.getNullKey())));
130 assertFalse(album.equals(new AxContextAlbum(newKey, "Scope", false, AxArtifactKey.getNullKey())));
131 assertFalse(album.equals(new AxContextAlbum(newKey, "NewAlbumScope", false, AxArtifactKey.getNullKey())));
132 assertFalse(album.equals(new AxContextAlbum(newKey, "NewAlbumScope", true, AxArtifactKey.getNullKey())));
133 assertTrue(album.equals(new AxContextAlbum(newKey, "NewAlbumScope", true, albumSchemaKey)));
135 assertEquals(0, album.compareTo(album));
136 assertEquals(0, album.compareTo(clonedAlbum));
137 assertNotEquals(0, album.compareTo(null));
138 assertNotEquals(0, album.compareTo(new AxArtifactKey()));
139 assertNotEquals(0, album.compareTo(
140 new AxContextAlbum(new AxArtifactKey(), "Scope", false, AxArtifactKey.getNullKey())));
141 assertNotEquals(0, album.compareTo(new AxContextAlbum(newKey, "Scope", false, AxArtifactKey.getNullKey())));
142 assertNotEquals(0, album
143 .compareTo(new AxContextAlbum(newKey, "NewAlbumScope", false, AxArtifactKey.getNullKey())));
145 album.compareTo(new AxContextAlbum(newKey, "NewAlbumScope", true, AxArtifactKey.getNullKey())));
146 assertEquals(0, album.compareTo(new AxContextAlbum(newKey, "NewAlbumScope", true, albumSchemaKey)));
148 final AxContextAlbums albums = new AxContextAlbums();
149 result = new AxValidationResult();
150 result = albums.validate(result);
151 assertEquals(ValidationResult.INVALID, result.getValidationResult());
153 // Observation, no albums in album map
154 albums.setKey(new AxArtifactKey("AlbumsKey", "0.0.1"));
155 result = new AxValidationResult();
156 result = albums.validate(result);
157 assertEquals(ValidationResult.OBSERVATION, result.getValidationResult());
159 albums.getAlbumsMap().put(newKey, album);
160 result = new AxValidationResult();
161 result = albums.validate(result);
162 assertEquals(ValidationResult.VALID, result.getValidationResult());
164 albums.getAlbumsMap().put(AxArtifactKey.getNullKey(), null);
165 result = new AxValidationResult();
166 result = albums.validate(result);
167 assertEquals(ValidationResult.INVALID, result.getValidationResult());
169 albums.getAlbumsMap().remove(AxArtifactKey.getNullKey());
170 result = new AxValidationResult();
171 result = albums.validate(result);
172 assertEquals(ValidationResult.VALID, result.getValidationResult());
174 albums.getAlbumsMap().put(new AxArtifactKey("NullValueKey", "0.0.1"), null);
175 result = new AxValidationResult();
176 result = albums.validate(result);
177 assertEquals(ValidationResult.INVALID, result.getValidationResult());
179 albums.getAlbumsMap().remove(new AxArtifactKey("NullValueKey", "0.0.1"));
180 result = new AxValidationResult();
181 result = albums.validate(result);
182 assertEquals(ValidationResult.VALID, result.getValidationResult());
186 final AxContextAlbums clonedAlbums = new AxContextAlbums(albums);
187 assertTrue(clonedAlbums.toString().startsWith(
188 "AxContextAlbums:(AxContextAlbums:(key=AxArtifactKey:(name=AlbumsKey,version=0.0.1)"));
190 assertFalse(albums.hashCode() == 0);
192 assertTrue(albums.equals(albums));
193 assertTrue(albums.equals(clonedAlbums));
194 assertFalse(albums.equals(null));
195 assertFalse(albums.equals("Hello"));
196 assertFalse(albums.equals(new AxContextAlbums(new AxArtifactKey())));
198 assertEquals(0, albums.compareTo(albums));
199 assertEquals(0, albums.compareTo(clonedAlbums));
200 assertNotEquals(0, albums.compareTo(null));
201 assertNotEquals(0, albums.compareTo(new AxArtifactKey()));
202 assertNotEquals(0, albums.compareTo(new AxContextAlbums(new AxArtifactKey())));
204 clonedAlbums.get(newKey).setScope("YetAnotherScope");
205 assertNotEquals(0, albums.compareTo(clonedAlbums));
207 assertEquals("NewAlbumName", albums.get("NewAlbumName").getKey().getName());
208 assertEquals("NewAlbumName", albums.get("NewAlbumName", "0.0.1").getKey().getName());
209 assertEquals(1, albums.getAll("NewAlbumName", "0.0.1").size());
210 assertEquals(0, albums.getAll("NonExistantAlbumName").size());