b2158ce4727d16bac05d110c9cbb11baec81b6ba
[policy/gui.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2021 Nordix Foundation.
4  *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
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.gui.editors.apex.rest.handling;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25
26 import java.io.ByteArrayOutputStream;
27 import java.io.IOException;
28 import java.io.PrintStream;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.util.Random;
32 import org.junit.Before;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.onap.policy.gui.editors.apex.rest.ApexEditorMain;
36
37 public class RestSessionTest {
38
39     private int sessionId;
40     private RestSession restSession;
41
42     @BeforeClass
43     public static void beforeClass() {
44         // Initialize ApexEditor
45         final String[] args = {"--time-to-live", "10", "--port", "12321", "--listen", "127.0.0.1"};
46         final var outBaStream = new ByteArrayOutputStream();
47         final var outStream = new PrintStream(outBaStream);
48         new ApexEditorMain(args, outStream);
49     }
50
51     @Before
52     public void setUp() {
53         sessionId = new Random().nextInt();
54         restSession = new RestSession(sessionId);
55     }
56
57     @Test
58     public void testGetSessionId() {
59         final var actual = restSession.getSessionId();
60         assertThat(actual).isEqualTo(sessionId);
61     }
62
63     @Test
64     public void testCommitChangesNoChanges() {
65         final var apexApiResult = restSession.commitChanges();
66         assertThat(apexApiResult.isNok()).isTrue();
67     }
68
69     @Test
70     public void testCommitChanges() {
71         restSession.editModel();
72         final var apexApiResult = restSession.commitChanges();
73         assertThat(apexApiResult.isOk()).isTrue();
74     }
75
76     @Test
77     public void testDiscardChangesNotEdited() {
78         final var apexApiResult = restSession.discardChanges();
79         assertThat(apexApiResult.isNok()).isTrue();
80     }
81
82     @Test
83     public void testDiscardChanges() {
84         restSession.editModel();
85         final var apexApiResult = restSession.discardChanges();
86         assertThat(apexApiResult.isOk()).isTrue();
87         assertThat(restSession.getApexModelEdited()).isNull();
88     }
89
90     @Test
91     public void testDownloadModel() {
92         final var actual = restSession.downloadModel();
93         assertThat(actual.isOk()).isTrue();
94     }
95
96     @Test
97     public void testEditModel() {
98         final var original = restSession.getApexModelEdited();
99         final var apexApiResult = restSession.editModel();
100         final var apexModelEdited = restSession.getApexModelEdited();
101         final var apexModel = restSession.getApexModel();
102         assertThat(apexApiResult.isOk()).isTrue();
103         assertThat(original).isNull();
104         assertThat(apexModelEdited).isNotNull();
105         assertThat(apexModel).isNotNull();
106     }
107
108     @Test
109     public void testEditModelAlreadyEdited() {
110         restSession.editModel();
111         final var apexApiResult = restSession.editModel();
112         assertThat(apexApiResult.isNok()).isTrue();
113     }
114
115     @Test
116     public void testLoadFromString() throws IOException {
117         restSession.editModel();
118         final var toscaPath = Path.of("src/test/resources/models/PolicyModel.yaml");
119         final var toscaString = Files.readString(toscaPath);
120         final var apexApiResult = restSession.loadFromString(toscaString);
121         assertThat(apexApiResult.isOk()).isTrue();
122         final var apexModelEdited = restSession.getApexModelEdited();
123         assertThat(apexModelEdited).isNotNull();
124     }
125
126     @Test
127     public void testLoadFromStringNoPolicies() throws IOException {
128         restSession.editModel();
129         final var toscaPath = Path.of("src/test/resources/models/PolicyModelNoPolicies.yaml");
130         final var toscaString = Files.readString(toscaPath);
131         final var apexApiResult = restSession.loadFromString(toscaString);
132         assertThat(apexApiResult.isNok()).isTrue();
133         assertThat(apexApiResult.getMessage()).contains("no policies");
134     }
135
136     @Test
137     public void testUploadModel() throws IOException {
138         restSession.editModel();
139         final var toscaPath = Path.of("src/test/resources/models/PolicyModel.yaml");
140         final var toscaString = Files.readString(toscaPath);
141         restSession.loadFromString(toscaString);
142         final var apexApiResult = restSession.uploadModel();
143         assertThat(apexApiResult.isNok()).isTrue();
144         assertThat(apexApiResult.getMessage()).contains("Model upload is disabled");
145     }
146 }