c23687bb19c8e6689f29e8f2dc80beea78d4cafc
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2019-2020 Nordix Foundation.
5  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.apex.model.enginemodel.concepts;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.assertj.core.api.Assertions.assertThatThrownBy;
27 import static org.junit.Assert.assertEquals;
28 import static org.junit.Assert.assertNotEquals;
29 import static org.junit.Assert.assertNotNull;
30 import static org.junit.Assert.assertTrue;
31
32 import org.junit.Test;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxKeyInformation;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
36 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
37 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
38 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums;
39 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
40
41 /**
42  * Test engine models.
43  * @author Liam Fallon (liam.fallon@ericsson.com)
44  */
45 public class EngineModelTest {
46
47     @Test
48     public void testEnginetModel() {
49         assertNotNull(new AxEngineModel());
50         assertNotNull(new AxEngineModel(new AxArtifactKey()));
51         assertNotNull(new AxEngineModel(new AxArtifactKey(), new AxContextSchemas(), new AxKeyInformation(),
52                 new AxContextAlbums()));
53         assertNotNull(new AxEngineModel(new AxArtifactKey(), new AxContextSchemas(), new AxKeyInformation(),
54                 new AxContextAlbums(), AxEngineState.UNDEFINED, new AxEngineStats()));
55
56         final AxArtifactKey modelKey = new AxArtifactKey("ModelName", "0.0.1");
57         final AxArtifactKey schemasKey = new AxArtifactKey("SchemasKey", "0.0.1");
58         final AxArtifactKey albumKey = new AxArtifactKey("AlbumKey", "0.0.1");
59         final AxArtifactKey keyInfoKey = new AxArtifactKey("SchemasKey", "0.0.1");
60         final AxEngineStats stats = new AxEngineStats(new AxReferenceKey(modelKey, "EngineStats"));
61         final AxEngineStats otherStats = new AxEngineStats();
62         otherStats.setAverageExecutionTime(100);
63
64         final AxEngineModel model = new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
65                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats);
66         model.register();
67
68         assertThatThrownBy(() -> model.setKey(null))
69             .hasMessage("key may not be null");
70         model.setKey(modelKey);
71         assertEquals("ModelName:0.0.1", model.getKey().getId());
72         assertEquals("ModelName:0.0.1", model.getKeys().get(0).getId());
73
74         final long timestamp = System.currentTimeMillis();
75         model.setTimestamp(timestamp);
76         assertEquals(timestamp, model.getTimestamp());
77         model.setTimestamp(-1);
78         assertTrue(model.getTimeStampString().matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}.\\d{3}"));
79
80         assertThatThrownBy(() -> model.setState(null))
81             .hasMessage("state may not be null");
82         for (final AxEngineState state : AxEngineState.values()) {
83             model.setState(state);
84             assertEquals(state, model.getState());
85         }
86
87         model.setState(AxEngineState.READY);
88         assertEquals(AxEngineState.READY, model.getState());
89
90         assertThatThrownBy(() -> model.setStats(null))
91             .hasMessage("stats may not be null");
92         model.setStats(stats);
93         assertEquals(stats, model.getStats());
94
95         model.clean();
96         assertNotNull(model);
97         assertThat(model.toString()).startsWith("AxEngineModel:(AxContextModel(super=AxEngineModel:(key=A");
98
99         final AxEngineModel clonedModel = new AxEngineModel(model);
100
101         assertNotEquals(0, model.hashCode());
102
103         // disabling sonar because this code tests the equals() method
104         assertEquals(model, model); // NOSONAR
105         assertEquals(model, clonedModel);
106         assertNotEquals(model, (Object) "Hello");
107         assertNotEquals(model, new AxEngineModel(new AxArtifactKey()));
108         assertNotEquals(model, new AxEngineModel(new AxArtifactKey(), new AxContextSchemas(schemasKey),
109                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats));
110         assertNotEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(), new AxKeyInformation(keyInfoKey),
111                 new AxContextAlbums(albumKey), AxEngineState.READY, stats));
112         assertNotEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(schemasKey), new AxKeyInformation(),
113                 new AxContextAlbums(albumKey), AxEngineState.READY, stats));
114         assertNotEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
115                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(), AxEngineState.READY, stats));
116         assertNotEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
117                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.STOPPED, stats));
118         assertNotEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
119                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, otherStats));
120         model.setTimestamp(timestamp);
121         assertNotEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
122                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats));
123         model.setTimestamp(0);
124         assertEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
125                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats));
126
127         model.setTimestamp(-1);
128         assertEquals(0, model.compareTo(model));
129         assertEquals(0, model.compareTo(clonedModel));
130         assertNotEquals(0, model.compareTo(new AxArtifactKey()));
131         assertNotEquals(model, new AxEngineModel(new AxArtifactKey()));
132         assertNotEquals(0, model.compareTo(new AxEngineModel(new AxArtifactKey(), new AxContextSchemas(schemasKey),
133                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats)));
134         assertNotEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(),
135                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats)));
136         assertNotEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
137                 new AxKeyInformation(), new AxContextAlbums(albumKey), AxEngineState.READY, stats)));
138         assertNotEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
139                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(), AxEngineState.READY, stats)));
140         assertNotEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
141                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.STOPPED, stats)));
142         assertNotEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
143                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, otherStats)));
144         model.setTimestamp(timestamp);
145         assertNotEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
146                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats)));
147         model.setTimestamp(0);
148         assertEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
149                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats)));
150
151         model.setTimestamp(timestamp);
152         AxValidationResult result = new AxValidationResult();
153         result = model.validate(result);
154         assertEquals(ValidationResult.VALID, result.getValidationResult());
155
156         model.setTimestamp(-1);
157         result = new AxValidationResult();
158         result = model.validate(result);
159         assertEquals(ValidationResult.INVALID, result.getValidationResult());
160
161         model.setTimestamp(timestamp);
162         result = new AxValidationResult();
163         result = model.validate(result);
164         assertEquals(ValidationResult.VALID, result.getValidationResult());
165
166         model.setState(AxEngineState.UNDEFINED);
167         result = new AxValidationResult();
168         result = model.validate(result);
169         assertEquals(ValidationResult.INVALID, result.getValidationResult());
170
171         model.setState(AxEngineState.READY);
172         result = new AxValidationResult();
173         result = model.validate(result);
174         assertEquals(ValidationResult.VALID, result.getValidationResult());
175     }
176 }