d5b8456589ef142432311c626cf8b1fdd6c806d8
[policy/apex-pdp.git] / model / model-api / src / test / java / org / onap / policy / apex / model / modelapi / ModelFacadeTest.java
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.modelapi;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertNotNull;
25 import static org.junit.Assert.fail;
26
27 import java.util.Properties;
28 import java.util.UUID;
29
30 import org.junit.Test;
31 import org.onap.policy.apex.model.modelapi.impl.ModelFacade;
32
33 public class ModelFacadeTest {
34
35     @Test
36     public void testModelFacade() {
37         try {
38             new ModelFacade(null, null, false);
39             fail("test should throw an exception here");
40         } catch (final Exception e) {
41             assertEquals("apexModel may not be null", e.getMessage());
42         }
43
44         final ApexModel apexModel = new ApexModelFactory().createApexModel(null, false);
45
46         try {
47             new ModelFacade(apexModel, null, false);
48             fail("test should throw an exception here");
49         } catch (final Exception e) {
50             assertEquals("apexProperties may not be null", e.getMessage());
51         }
52
53         final Properties modelProperties = new Properties();
54         final ModelFacade mf = new ModelFacade(apexModel, modelProperties, false);
55
56         ApexApiResult result = mf.createModel(null, null, null, null);
57         assertEquals(ApexApiResult.Result.FAILED, result.getResult());
58
59         result = mf.createModel("ModelName", null, null, null);
60         assertEquals(ApexApiResult.Result.FAILED, result.getResult());
61
62         result = mf.createModel("ModelName", "0.0.1", null, null);
63         assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
64
65         modelProperties.setProperty("DEFAULT_CONCEPT_VERSION", "");
66         result = mf.createModel("ModelName", null, null, null);
67         assertEquals(ApexApiResult.Result.FAILED, result.getResult());
68
69         modelProperties.setProperty("DEFAULT_CONCEPT_VERSION", "£$£$£$");
70         result = mf.createModel("ModelName", null, null, null);
71         assertEquals(ApexApiResult.Result.FAILED, result.getResult());
72
73         modelProperties.setProperty("DEFAULT_CONCEPT_VERSION", "0.0.1");
74         result = mf.createModel("ModelName", null, null, null);
75         assertEquals(ApexApiResult.Result.CONCEPT_EXISTS, result.getResult());
76         result = mf.deleteModel();
77         assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
78         result = mf.createModel("ModelName", null, null, null);
79         assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
80
81         result = mf.updateModel("ModelName", null, UUID.randomUUID().toString(), "New Description");
82         assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
83
84         result = mf.updateModel("ModelName", "0.0.1", UUID.randomUUID().toString(), "New Description");
85         assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
86
87         modelProperties.remove("DEFAULT_CONCEPT_VERSION");
88         result = mf.updateModel("ModelName", null, UUID.randomUUID().toString(), "New Description");
89         assertEquals(ApexApiResult.Result.FAILED, result.getResult());
90
91         result = mf.updateModel(null, null, UUID.randomUUID().toString(), "New Description");
92         assertEquals(ApexApiResult.Result.FAILED, result.getResult());
93
94         result = mf.deleteModel();
95         assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
96         result = mf.updateModel("name", "0.0.1", UUID.randomUUID().toString(), "New Description");
97         assertEquals(ApexApiResult.Result.CONCEPT_DOES_NOT_EXIST, result.getResult());
98
99         result = mf.getModelKey();
100         assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
101
102         result = mf.listModel();
103         assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
104         assertEquals("AxPolicyModel:(AxPolicyModel:(key=AxArtifactKey:(n", result.getMessage().substring(0, 50));
105
106         result = mf.deleteModel();
107         assertEquals(ApexApiResult.Result.SUCCESS, result.getResult());
108         assertNotNull(mf);
109     }
110 }