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