aboutsummaryrefslogtreecommitdiffstats
path: root/model/context-model/src/test/java/org/onap/policy/apex/model/contextmodel/concepts/ContextAlbumsTest.java
blob: 2e2005fcdbffb039526a7f2d8f389fc9417091f7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.apex.model.contextmodel.concepts;

import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

import org.junit.Test;
import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;

/**
 * Context album tests.
 *
 * @author Liam Fallon (liam.fallon@ericsson.com)
 */
public class ContextAlbumsTest {

    @Test
    public void testContextAlbums() {
        assertNotNull(new AxContextAlbum());
        assertNotNull(new AxContextAlbum(new AxArtifactKey()));
        assertNotNull(new AxContextAlbum(new AxArtifactKey(), "AlbumScope", false, new AxArtifactKey()));

        final AxArtifactKey albumKey = new AxArtifactKey("AlbumName", "0.0.1");
        final AxArtifactKey albumSchemaKey = new AxArtifactKey("AlbumSchemaName", "0.0.1");

        final AxContextAlbum album = new AxContextAlbum(albumKey, "AlbumScope", false, albumSchemaKey);
        assertNotNull(album);

        final AxArtifactKey newKey = new AxArtifactKey("NewAlbumName", "0.0.1");
        album.setKey(newKey);
        assertEquals("NewAlbumName:0.0.1", album.getKey().getId());
        assertEquals("NewAlbumName:0.0.1", album.getKeys().get(0).getId());
        album.setKey(albumKey);

        assertThatThrownBy(() -> album.setScope(""))
            .hasMessage("parameter \"scope\": value \"\", does not match regular expression "
                    + "\"[A-Za-z0-9\\-_]+\"");

        album.setScope("NewAlbumScope");
        assertEquals("NewAlbumScope", album.getScope());

        assertEquals(false, album.isWritable());
        album.setWritable(true);
        assertEquals(true, album.isWritable());

        final AxArtifactKey newSchemaKey = new AxArtifactKey("NewAlbumSchemaName", "0.0.1");
        album.setItemSchema(newSchemaKey);
        assertEquals("NewAlbumSchemaName:0.0.1", album.getItemSchema().getId());
        album.setItemSchema(albumSchemaKey);

        AxValidationResult result = new AxValidationResult();
        result = album.validate(result);
        assertEquals(ValidationResult.VALID, result.getValidationResult());

        album.setKey(AxArtifactKey.getNullKey());
        result = new AxValidationResult();
        result = album.validate(result);
        assertEquals(ValidationResult.INVALID, result.getValidationResult());

        album.setKey(newKey);
        result = new AxValidationResult();
        result = album.validate(result);
        assertEquals(ValidationResult.VALID, result.getValidationResult());

        album.setScope("UNDEFINED");
        result = new AxValidationResult();
        result = album.validate(result);
        assertEquals(ValidationResult.INVALID, result.getValidationResult());

        album.setScope("NewAlbumScope");
        result = new AxValidationResult();
        result = album.validate(result);
        assertEquals(ValidationResult.VALID, result.getValidationResult());

        album.setItemSchema(AxArtifactKey.getNullKey());
        result = new AxValidationResult();
        result = album.validate(result);
        assertEquals(ValidationResult.INVALID, result.getValidationResult());

        album.setItemSchema(albumSchemaKey);
        result = new AxValidationResult();
        result = album.validate(result);
        assertEquals(ValidationResult.VALID, result.getValidationResult());

        album.clean();

        final AxContextAlbum clonedAlbum = new AxContextAlbum(album);
        assertEquals("AxContextAlbum:(key=AxArtifactKey:(name=NewAlbumName,version=0.0.1),"
                        + "scope=NewAlbumScope,isWritable=true,itemSchema="
                        + "AxArtifactKey:(name=AlbumSchemaName,version=0.0.1))", clonedAlbum.toString());

        assertNotEquals(0, album.hashCode());
        // disabling sonar because this code tests the equals() method
        assertEquals(album, album); // NOSONAR
        assertEquals(album, clonedAlbum);
        assertNotNull(album);
        assertNotEquals(album, "Hello");
        assertNotEquals(album, new AxContextAlbum(new AxArtifactKey(), "Scope", false, AxArtifactKey.getNullKey()));
        assertNotEquals(album, new AxContextAlbum(newKey, "Scope", false, AxArtifactKey.getNullKey()));
        assertNotEquals(album, new AxContextAlbum(newKey, "NewAlbumScope", false, AxArtifactKey.getNullKey()));
        assertNotEquals(album, new AxContextAlbum(newKey, "NewAlbumScope", true, AxArtifactKey.getNullKey()));
        assertEquals(album, new AxContextAlbum(newKey, "NewAlbumScope", true, albumSchemaKey));

        assertEquals(0, album.compareTo(album));
        assertEquals(0, album.compareTo(clonedAlbum));
        assertNotEquals(0, album.compareTo(null));
        assertNotEquals(0, album.compareTo(new AxArtifactKey()));
        assertNotEquals(0, album.compareTo(
                        new AxContextAlbum(new AxArtifactKey(), "Scope", false, AxArtifactKey.getNullKey())));
        assertNotEquals(0, album.compareTo(new AxContextAlbum(newKey, "Scope", false, AxArtifactKey.getNullKey())));
        assertNotEquals(0, album
                        .compareTo(new AxContextAlbum(newKey, "NewAlbumScope", false, AxArtifactKey.getNullKey())));
        assertNotEquals(0,
                        album.compareTo(new AxContextAlbum(newKey, "NewAlbumScope", true, AxArtifactKey.getNullKey())));
        assertEquals(0, album.compareTo(new AxContextAlbum(newKey, "NewAlbumScope", true, albumSchemaKey)));

        final AxContextAlbums albums = new AxContextAlbums();
        result = new AxValidationResult();
        result = albums.validate(result);
        assertEquals(ValidationResult.INVALID, result.getValidationResult());

        // Observation, no albums in album map
        albums.setKey(new AxArtifactKey("AlbumsKey", "0.0.1"));
        result = new AxValidationResult();
        result = albums.validate(result);
        assertEquals(ValidationResult.OBSERVATION, result.getValidationResult());

        albums.getAlbumsMap().put(newKey, album);
        result = new AxValidationResult();
        result = albums.validate(result);
        assertEquals(ValidationResult.VALID, result.getValidationResult());

        albums.getAlbumsMap().put(AxArtifactKey.getNullKey(), null);
        result = new AxValidationResult();
        result = albums.validate(result);
        assertEquals(ValidationResult.INVALID, result.getValidationResult());

        albums.getAlbumsMap().remove(AxArtifactKey.getNullKey());
        result = new AxValidationResult();
        result = albums.validate(result);
        assertEquals(ValidationResult.VALID, result.getValidationResult());

        albums.getAlbumsMap().put(new AxArtifactKey("NullValueKey", "0.0.1"), null);
        result = new AxValidationResult();
        result = albums.validate(result);
        assertEquals(ValidationResult.INVALID, result.getValidationResult());

        albums.getAlbumsMap().remove(new AxArtifactKey("NullValueKey", "0.0.1"));
        result = new AxValidationResult();
        result = albums.validate(result);
        assertEquals(ValidationResult.VALID, result.getValidationResult());

        albums.clean();

        final AxContextAlbums clonedAlbums = new AxContextAlbums(albums);
        assertTrue(clonedAlbums.toString().startsWith(
                        "AxContextAlbums:(AxContextAlbums:(key=AxArtifactKey:(name=AlbumsKey,version=0.0.1)"));

        assertNotEquals(0, albums.hashCode());

        assertEquals(albums, clonedAlbums);
        assertNotNull(albums);
        assertNotEquals(albums, "Hello");
        assertNotEquals(albums, new AxContextAlbums(new AxArtifactKey()));

        assertEquals(0, albums.compareTo(albums));
        assertEquals(0, albums.compareTo(clonedAlbums));
        assertNotEquals(0, albums.compareTo(null));
        assertNotEquals(0, albums.compareTo(new AxArtifactKey()));
        assertNotEquals(0, albums.compareTo(new AxContextAlbums(new AxArtifactKey())));

        clonedAlbums.get(newKey).setScope("YetAnotherScope");
        assertNotEquals(0, albums.compareTo(clonedAlbums));

        assertEquals("NewAlbumName", albums.get("NewAlbumName").getKey().getName());
        assertEquals("NewAlbumName", albums.get("NewAlbumName", "0.0.1").getKey().getName());
        assertEquals(1, albums.getAll("NewAlbumName", "0.0.1").size());
        assertEquals(0, albums.getAll("NonExistantAlbumName").size());
    }
}