9d1c7bf44a797ee94b3a0daad5c3d22f12bcc2a7
[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  * ================================================================================
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.enginemodel.concepts;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNotEquals;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertTrue;
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.AxKeyInformation;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey;
34 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult;
35 import org.onap.policy.apex.model.basicmodel.concepts.AxValidationResult.ValidationResult;
36 import org.onap.policy.apex.model.contextmodel.concepts.AxContextAlbums;
37 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
38
39 /**
40  * Test engine models.
41  * @author Liam Fallon (liam.fallon@ericsson.com)
42  */
43 public class EngineModelTest {
44
45     @Test
46     public void testEnginetModel() {
47         assertNotNull(new AxEngineModel());
48         assertNotNull(new AxEngineModel(new AxArtifactKey()));
49         assertNotNull(new AxEngineModel(new AxArtifactKey(), new AxContextSchemas(), new AxKeyInformation(),
50                 new AxContextAlbums()));
51         assertNotNull(new AxEngineModel(new AxArtifactKey(), new AxContextSchemas(), new AxKeyInformation(),
52                 new AxContextAlbums(), AxEngineState.UNDEFINED, new AxEngineStats()));
53
54         final AxArtifactKey modelKey = new AxArtifactKey("ModelName", "0.0.1");
55         final AxArtifactKey schemasKey = new AxArtifactKey("SchemasKey", "0.0.1");
56         final AxArtifactKey albumKey = new AxArtifactKey("AlbumKey", "0.0.1");
57         final AxArtifactKey keyInfoKey = new AxArtifactKey("SchemasKey", "0.0.1");
58         final AxEngineStats stats = new AxEngineStats(new AxReferenceKey(modelKey, "EngineStats"));
59         final AxEngineStats otherStats = new AxEngineStats();
60         otherStats.setAverageExecutionTime(100);
61
62         final AxEngineModel model = new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
63                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats);
64         model.register();
65
66         assertThatThrownBy(() -> model.setKey(null))
67             .hasMessage("key may not be null");
68         model.setKey(modelKey);
69         assertEquals("ModelName:0.0.1", model.getKey().getId());
70         assertEquals("ModelName:0.0.1", model.getKeys().get(0).getId());
71
72         final long timestamp = System.currentTimeMillis();
73         model.setTimestamp(timestamp);
74         assertEquals(timestamp, model.getTimestamp());
75         model.setTimestamp(-1);
76         assertTrue(model.getTimeStampString().matches("\\d{4}-\\d{2}-\\d{2} \\d{2}:\\d{2}:\\d{2}.\\d{3}"));
77
78         assertThatThrownBy(() -> model.setState(null))
79             .hasMessage("state may not be null");
80         for (final AxEngineState state : AxEngineState.values()) {
81             model.setState(state);
82             assertEquals(state, model.getState());
83         }
84
85         model.setState(AxEngineState.READY);
86         assertEquals(AxEngineState.READY, model.getState());
87
88         assertThatThrownBy(() -> model.setStats(null))
89             .hasMessage("stats may not be null");
90         model.setStats(stats);
91         assertEquals(stats, model.getStats());
92
93         model.clean();
94         assertNotNull(model);
95         assertEquals("AxEngineModel:(AxEngineModel:(AxEngineModel:(key=A", model.toString().substring(0, 50));
96
97         final AxEngineModel clonedModel = new AxEngineModel(model);
98
99         assertNotEquals(0, model.hashCode());
100
101         // disabling sonar because this code tests the equals() method
102         assertEquals(model, model); // NOSONAR
103         assertEquals(model, clonedModel);
104         assertNotEquals(model, "Hello");
105         assertNotEquals(model, new AxEngineModel(new AxArtifactKey()));
106         assertNotEquals(model, new AxEngineModel(new AxArtifactKey(), new AxContextSchemas(schemasKey),
107                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats));
108         assertNotEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(), new AxKeyInformation(keyInfoKey),
109                 new AxContextAlbums(albumKey), AxEngineState.READY, stats));
110         assertNotEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(schemasKey), new AxKeyInformation(),
111                 new AxContextAlbums(albumKey), AxEngineState.READY, stats));
112         assertNotEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
113                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(), AxEngineState.READY, stats));
114         assertNotEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
115                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.STOPPED, stats));
116         assertNotEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
117                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, otherStats));
118         model.setTimestamp(timestamp);
119         assertNotEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
120                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats));
121         model.setTimestamp(0);
122         assertEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
123                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats));
124
125         model.setTimestamp(-1);
126         assertEquals(0, model.compareTo(model));
127         assertEquals(0, model.compareTo(clonedModel));
128         assertNotEquals(0, model.compareTo(new AxArtifactKey()));
129         assertNotEquals(model, new AxEngineModel(new AxArtifactKey()));
130         assertNotEquals(0, model.compareTo(new AxEngineModel(new AxArtifactKey(), new AxContextSchemas(schemasKey),
131                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats)));
132         assertNotEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(),
133                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats)));
134         assertNotEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
135                 new AxKeyInformation(), new AxContextAlbums(albumKey), AxEngineState.READY, stats)));
136         assertNotEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
137                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(), AxEngineState.READY, stats)));
138         assertNotEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
139                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.STOPPED, stats)));
140         assertNotEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
141                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, otherStats)));
142         model.setTimestamp(timestamp);
143         assertNotEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
144                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats)));
145         model.setTimestamp(0);
146         assertEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
147                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats)));
148
149         model.setTimestamp(timestamp);
150         AxValidationResult result = new AxValidationResult();
151         result = model.validate(result);
152         assertEquals(ValidationResult.VALID, result.getValidationResult());
153
154         model.setTimestamp(-1);
155         result = new AxValidationResult();
156         result = model.validate(result);
157         assertEquals(ValidationResult.INVALID, result.getValidationResult());
158
159         model.setTimestamp(timestamp);
160         result = new AxValidationResult();
161         result = model.validate(result);
162         assertEquals(ValidationResult.VALID, result.getValidationResult());
163
164         model.setState(AxEngineState.UNDEFINED);
165         result = new AxValidationResult();
166         result = model.validate(result);
167         assertEquals(ValidationResult.INVALID, result.getValidationResult());
168
169         model.setState(AxEngineState.READY);
170         result = new AxValidationResult();
171         result = model.validate(result);
172         assertEquals(ValidationResult.VALID, result.getValidationResult());
173     }
174 }