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 * @author Liam Fallon (liam.fallon@ericsson.com)
40 public class TestContextAlbums {
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);
62 fail("test should throw an exception here");
63 } catch (final Exception e) {
64 assertEquals("parameter \"scope\": value \"\", does not match regular expression \"[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);
80 AxValidationResult result = new AxValidationResult();
81 result = album.validate(result);
82 assertEquals(ValidationResult.VALID, result.getValidationResult());
84 album.setKey(AxArtifactKey.getNullKey());
85 result = new AxValidationResult();
86 result = album.validate(result);
87 assertEquals(ValidationResult.INVALID, result.getValidationResult());
90 result = new AxValidationResult();
91 result = album.validate(result);
92 assertEquals(ValidationResult.VALID, result.getValidationResult());
94 album.setScope("UNDEFINED");
95 result = new AxValidationResult();
96 result = album.validate(result);
97 assertEquals(ValidationResult.INVALID, result.getValidationResult());
99 album.setScope("NewAlbumScope");
100 result = new AxValidationResult();
101 result = album.validate(result);
102 assertEquals(ValidationResult.VALID, result.getValidationResult());
104 album.setItemSchema(AxArtifactKey.getNullKey());
105 result = new AxValidationResult();
106 result = album.validate(result);
107 assertEquals(ValidationResult.INVALID, result.getValidationResult());
109 album.setItemSchema(albumSchemaKey);
110 result = new AxValidationResult();
111 result = album.validate(result);
112 assertEquals(ValidationResult.VALID, result.getValidationResult());
116 final AxContextAlbum clonedAlbum = new AxContextAlbum(album);
118 "AxContextAlbum:(key=AxArtifactKey:(name=NewAlbumName,version=0.0.1),scope=NewAlbumScope,isWritable=true,itemSchema=AxArtifactKey:(name=AlbumSchemaName,version=0.0.1))",
119 clonedAlbum.toString());
121 assertFalse(album.hashCode() == 0);
123 assertTrue(album.equals(album));
124 assertTrue(album.equals(clonedAlbum));
125 assertFalse(album.equals(null));
126 assertFalse(album.equals("Hello"));
127 assertFalse(album.equals(new AxContextAlbum(new AxArtifactKey(), "Scope", false, AxArtifactKey.getNullKey())));
128 assertFalse(album.equals(new AxContextAlbum(newKey, "Scope", false, AxArtifactKey.getNullKey())));
129 assertFalse(album.equals(new AxContextAlbum(newKey, "NewAlbumScope", false, AxArtifactKey.getNullKey())));
130 assertFalse(album.equals(new AxContextAlbum(newKey, "NewAlbumScope", true, AxArtifactKey.getNullKey())));
131 assertTrue(album.equals(new AxContextAlbum(newKey, "NewAlbumScope", true, albumSchemaKey)));
133 assertEquals(0, album.compareTo(album));
134 assertEquals(0, album.compareTo(clonedAlbum));
135 assertNotEquals(0, album.compareTo(null));
136 assertNotEquals(0, album.compareTo(new AxArtifactKey()));
138 album.compareTo(new AxContextAlbum(new AxArtifactKey(), "Scope", false, AxArtifactKey.getNullKey())));
139 assertNotEquals(0, album.compareTo(new AxContextAlbum(newKey, "Scope", false, AxArtifactKey.getNullKey())));
141 album.compareTo(new AxContextAlbum(newKey, "NewAlbumScope", false, AxArtifactKey.getNullKey())));
143 album.compareTo(new AxContextAlbum(newKey, "NewAlbumScope", true, AxArtifactKey.getNullKey())));
144 assertEquals(0, album.compareTo(new AxContextAlbum(newKey, "NewAlbumScope", true, albumSchemaKey)));
146 final AxContextAlbums albums = new AxContextAlbums();
147 result = new AxValidationResult();
148 result = albums.validate(result);
149 assertEquals(ValidationResult.INVALID, result.getValidationResult());
151 // Observation, no albums in album map
152 albums.setKey(new AxArtifactKey("AlbumsKey", "0.0.1"));
153 result = new AxValidationResult();
154 result = albums.validate(result);
155 assertEquals(ValidationResult.OBSERVATION, result.getValidationResult());
157 albums.getAlbumsMap().put(newKey, album);
158 result = new AxValidationResult();
159 result = albums.validate(result);
160 assertEquals(ValidationResult.VALID, result.getValidationResult());
162 albums.getAlbumsMap().put(AxArtifactKey.getNullKey(), null);
163 result = new AxValidationResult();
164 result = albums.validate(result);
165 assertEquals(ValidationResult.INVALID, result.getValidationResult());
167 albums.getAlbumsMap().remove(AxArtifactKey.getNullKey());
168 result = new AxValidationResult();
169 result = albums.validate(result);
170 assertEquals(ValidationResult.VALID, result.getValidationResult());
172 albums.getAlbumsMap().put(new AxArtifactKey("NullValueKey", "0.0.1"), null);
173 result = new AxValidationResult();
174 result = albums.validate(result);
175 assertEquals(ValidationResult.INVALID, result.getValidationResult());
177 albums.getAlbumsMap().remove(new AxArtifactKey("NullValueKey", "0.0.1"));
178 result = new AxValidationResult();
179 result = albums.validate(result);
180 assertEquals(ValidationResult.VALID, result.getValidationResult());
184 final AxContextAlbums clonedAlbums = new AxContextAlbums(albums);
185 assertTrue(clonedAlbums.toString()
186 .startsWith("AxContextAlbums:(AxContextAlbums:(key=AxArtifactKey:(name=AlbumsKey,version=0.0.1)"));
188 assertFalse(albums.hashCode() == 0);
190 assertTrue(albums.equals(albums));
191 assertTrue(albums.equals(clonedAlbums));
192 assertFalse(albums.equals(null));
193 assertFalse(albums.equals("Hello"));
194 assertFalse(albums.equals(new AxContextAlbums(new AxArtifactKey())));
196 assertEquals(0, albums.compareTo(albums));
197 assertEquals(0, albums.compareTo(clonedAlbums));
198 assertNotEquals(0, albums.compareTo(null));
199 assertNotEquals(0, albums.compareTo(new AxArtifactKey()));
200 assertNotEquals(0, albums.compareTo(new AxContextAlbums(new AxArtifactKey())));
202 clonedAlbums.get(newKey).setScope("YetAnotherScope");
203 assertNotEquals(0, albums.compareTo(clonedAlbums));
205 assertEquals("NewAlbumName", albums.get("NewAlbumName").getKey().getName());
206 assertEquals("NewAlbumName", albums.get("NewAlbumName", "0.0.1").getKey().getName());
207 assertEquals(1, albums.getAll("NewAlbumName", "0.0.1").size());
208 assertEquals(0, albums.getAll("NonExistantAlbumName").size());