Remove try/catch blocks with assertj - apex-pdp
[policy/apex-pdp.git] / model / engine-model / src / test / java / org / onap / policy / apex / model / enginemodel / concepts / EngineModelTest.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.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         assertEquals(model, model);
102         assertEquals(model, clonedModel);
103         assertNotEquals(model, (Object) "Hello");
104         assertNotEquals(model, new AxEngineModel(new AxArtifactKey()));
105         assertNotEquals(model, new AxEngineModel(new AxArtifactKey(), new AxContextSchemas(schemasKey),
106                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats));
107         assertNotEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(), new AxKeyInformation(keyInfoKey),
108                 new AxContextAlbums(albumKey), AxEngineState.READY, stats));
109         assertNotEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(schemasKey), new AxKeyInformation(),
110                 new AxContextAlbums(albumKey), AxEngineState.READY, stats));
111         assertNotEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
112                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(), AxEngineState.READY, stats));
113         assertNotEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
114                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.STOPPED, stats));
115         assertNotEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
116                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, otherStats));
117         model.setTimestamp(timestamp);
118         assertNotEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
119                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats));
120         model.setTimestamp(0);
121         assertEquals(model, new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
122                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats));
123
124         model.setTimestamp(-1);
125         assertEquals(0, model.compareTo(model));
126         assertEquals(0, model.compareTo(clonedModel));
127         assertNotEquals(0, model.compareTo(new AxArtifactKey()));
128         assertNotEquals(model, new AxEngineModel(new AxArtifactKey()));
129         assertNotEquals(0, model.compareTo(new AxEngineModel(new AxArtifactKey(), new AxContextSchemas(schemasKey),
130                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats)));
131         assertNotEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(),
132                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats)));
133         assertNotEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
134                 new AxKeyInformation(), new AxContextAlbums(albumKey), AxEngineState.READY, stats)));
135         assertNotEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
136                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(), AxEngineState.READY, stats)));
137         assertNotEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
138                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.STOPPED, stats)));
139         assertNotEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
140                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, otherStats)));
141         model.setTimestamp(timestamp);
142         assertNotEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
143                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats)));
144         model.setTimestamp(0);
145         assertEquals(0, model.compareTo(new AxEngineModel(modelKey, new AxContextSchemas(schemasKey),
146                 new AxKeyInformation(keyInfoKey), new AxContextAlbums(albumKey), AxEngineState.READY, stats)));
147
148         model.setTimestamp(timestamp);
149         AxValidationResult result = new AxValidationResult();
150         result = model.validate(result);
151         assertEquals(ValidationResult.VALID, result.getValidationResult());
152
153         model.setTimestamp(-1);
154         result = new AxValidationResult();
155         result = model.validate(result);
156         assertEquals(ValidationResult.INVALID, result.getValidationResult());
157
158         model.setTimestamp(timestamp);
159         result = new AxValidationResult();
160         result = model.validate(result);
161         assertEquals(ValidationResult.VALID, result.getValidationResult());
162
163         model.setState(AxEngineState.UNDEFINED);
164         result = new AxValidationResult();
165         result = model.validate(result);
166         assertEquals(ValidationResult.INVALID, result.getValidationResult());
167
168         model.setState(AxEngineState.READY);
169         result = new AxValidationResult();
170         result = model.validate(result);
171         assertEquals(ValidationResult.VALID, result.getValidationResult());
172     }
173 }