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