0406543fba63cc7a38ecc70da569732f4f52f54f
[policy/apex-pdp.git] / model / context-model / src / test / java / org / onap / policy / apex / model / contextmodel / concepts / ContextAlbumsTest.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.model.contextmodel.concepts;
23
24 import static org.junit.Assert.assertEquals;
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;
29
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
35 /**
36  * Context album tests.
37  *
38  * @author Liam Fallon (liam.fallon@ericsson.com)
39  */
40 public class ContextAlbumsTest {
41
42     @Test
43     public void testContextAlbums() {
44         assertNotNull(new AxContextAlbum());
45         assertNotNull(new AxContextAlbum(new AxArtifactKey()));
46         assertNotNull(new AxContextAlbum(new AxArtifactKey(), "AlbumScope", false, new AxArtifactKey()));
47
48         final AxArtifactKey albumKey = new AxArtifactKey("AlbumName", "0.0.1");
49         final AxArtifactKey albumSchemaKey = new AxArtifactKey("AlbumSchemaName", "0.0.1");
50
51         final AxContextAlbum album = new AxContextAlbum(albumKey, "AlbumScope", false, albumSchemaKey);
52         assertNotNull(album);
53
54         final AxArtifactKey newKey = new AxArtifactKey("NewAlbumName", "0.0.1");
55         album.setKey(newKey);
56         assertEquals("NewAlbumName:0.0.1", album.getKey().getId());
57         assertEquals("NewAlbumName:0.0.1", album.getKeys().get(0).getId());
58         album.setKey(albumKey);
59
60         try {
61             album.setScope("");
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\\-_]+\"",
65                             e.getMessage());
66         }
67
68         album.setScope("NewAlbumScope");
69         assertEquals("NewAlbumScope", album.getScope());
70
71         assertEquals(false, album.isWritable());
72         album.setWritable(true);
73         assertEquals(true, album.isWritable());
74
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);
79
80         AxValidationResult result = new AxValidationResult();
81         result = album.validate(result);
82         assertEquals(ValidationResult.VALID, result.getValidationResult());
83
84         album.setKey(AxArtifactKey.getNullKey());
85         result = new AxValidationResult();
86         result = album.validate(result);
87         assertEquals(ValidationResult.INVALID, result.getValidationResult());
88
89         album.setKey(newKey);
90         result = new AxValidationResult();
91         result = album.validate(result);
92         assertEquals(ValidationResult.VALID, result.getValidationResult());
93
94         album.setScope("UNDEFINED");
95         result = new AxValidationResult();
96         result = album.validate(result);
97         assertEquals(ValidationResult.INVALID, result.getValidationResult());
98
99         album.setScope("NewAlbumScope");
100         result = new AxValidationResult();
101         result = album.validate(result);
102         assertEquals(ValidationResult.VALID, result.getValidationResult());
103
104         album.setItemSchema(AxArtifactKey.getNullKey());
105         result = new AxValidationResult();
106         result = album.validate(result);
107         assertEquals(ValidationResult.INVALID, result.getValidationResult());
108
109         album.setItemSchema(albumSchemaKey);
110         result = new AxValidationResult();
111         result = album.validate(result);
112         assertEquals(ValidationResult.VALID, result.getValidationResult());
113
114         album.clean();
115
116         final AxContextAlbum clonedAlbum = new AxContextAlbum(album);
117         assertEquals("AxContextAlbum:(key=AxArtifactKey:(name=NewAlbumName,version=0.0.1),"
118                         + "scope=NewAlbumScope,isWritable=true,itemSchema="
119                         + "AxArtifactKey:(name=AlbumSchemaName,version=0.0.1))", clonedAlbum.toString());
120
121         assertNotEquals(0, album.hashCode());
122
123         assertEquals(album, album);
124         assertEquals(album, clonedAlbum);
125         assertNotNull(album);
126         assertNotEquals(album, (Object) "Hello");
127         assertNotEquals(album, new AxContextAlbum(new AxArtifactKey(), "Scope", false, AxArtifactKey.getNullKey()));
128         assertNotEquals(album, new AxContextAlbum(newKey, "Scope", false, AxArtifactKey.getNullKey()));
129         assertNotEquals(album, new AxContextAlbum(newKey, "NewAlbumScope", false, AxArtifactKey.getNullKey()));
130         assertNotEquals(album, new AxContextAlbum(newKey, "NewAlbumScope", true, AxArtifactKey.getNullKey()));
131         assertEquals(album, new AxContextAlbum(newKey, "NewAlbumScope", true, albumSchemaKey));
132
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()));
137         assertNotEquals(0, album.compareTo(
138                         new AxContextAlbum(new AxArtifactKey(), "Scope", false, AxArtifactKey.getNullKey())));
139         assertNotEquals(0, album.compareTo(new AxContextAlbum(newKey, "Scope", false, AxArtifactKey.getNullKey())));
140         assertNotEquals(0, album
141                         .compareTo(new AxContextAlbum(newKey, "NewAlbumScope", false, AxArtifactKey.getNullKey())));
142         assertNotEquals(0,
143                         album.compareTo(new AxContextAlbum(newKey, "NewAlbumScope", true, AxArtifactKey.getNullKey())));
144         assertEquals(0, album.compareTo(new AxContextAlbum(newKey, "NewAlbumScope", true, albumSchemaKey)));
145
146         final AxContextAlbums albums = new AxContextAlbums();
147         result = new AxValidationResult();
148         result = albums.validate(result);
149         assertEquals(ValidationResult.INVALID, result.getValidationResult());
150
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());
156
157         albums.getAlbumsMap().put(newKey, album);
158         result = new AxValidationResult();
159         result = albums.validate(result);
160         assertEquals(ValidationResult.VALID, result.getValidationResult());
161
162         albums.getAlbumsMap().put(AxArtifactKey.getNullKey(), null);
163         result = new AxValidationResult();
164         result = albums.validate(result);
165         assertEquals(ValidationResult.INVALID, result.getValidationResult());
166
167         albums.getAlbumsMap().remove(AxArtifactKey.getNullKey());
168         result = new AxValidationResult();
169         result = albums.validate(result);
170         assertEquals(ValidationResult.VALID, result.getValidationResult());
171
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());
176
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());
181
182         albums.clean();
183
184         final AxContextAlbums clonedAlbums = new AxContextAlbums(albums);
185         assertTrue(clonedAlbums.toString().startsWith(
186                         "AxContextAlbums:(AxContextAlbums:(key=AxArtifactKey:(name=AlbumsKey,version=0.0.1)"));
187
188         assertNotEquals(0, albums.hashCode());
189
190         assertEquals(albums, albums);
191         assertEquals(albums, clonedAlbums);
192         assertNotNull(albums);
193         assertNotEquals(albums, (Object) "Hello");
194         assertNotEquals(albums, new AxContextAlbums(new AxArtifactKey()));
195
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())));
201
202         clonedAlbums.get(newKey).setScope("YetAnotherScope");
203         assertNotEquals(0, albums.compareTo(clonedAlbums));
204
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());
209     }
210 }