2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2016-2018 Ericsson. All rights reserved.
4 * Modifications Copyright (C) 2020-2022 Nordix Foundation.
5 * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.gui.editors.apex.rest;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertNotNull;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28 import static org.springframework.http.MediaType.APPLICATION_JSON;
29 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
30 import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
32 import org.junit.jupiter.api.BeforeAll;
33 import org.junit.jupiter.api.Test;
34 import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
35 import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader;
36 import org.onap.policy.apex.model.modelapi.ApexApiResult;
37 import org.onap.policy.apex.model.policymodel.concepts.AxPolicy;
38 import org.onap.policy.common.utils.coder.StandardCoder;
39 import org.onap.policy.common.utils.resources.ResourceUtils;
40 import org.onap.policy.gui.editors.apex.ApexEditor;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
43 import org.springframework.boot.test.context.SpringBootTest;
44 import org.springframework.test.context.ContextConfiguration;
45 import org.springframework.test.web.servlet.MockMvc;
46 import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
49 * The RestInterface Test.
51 @SpringBootTest(classes = ApexEditor.class)
53 class RestInterfaceTest {
58 private static final String TESTMODELFILE = "models/PolicyModel.yaml";
60 private static String localModelString = null;
68 // load a test model locally
69 localModelString = ResourceUtils.getResourceAsString(TESTMODELFILE);
73 * Test to see that the message create Model with model id -1 .
76 void createSession() throws Exception {
81 * Helper method to invoke rest call using mock mvc, and return ApexApiResult.
83 private ApexApiResult apexRest(MockHttpServletRequestBuilder requestBuilder) throws Exception {
84 var response = mvc.perform(requestBuilder).andReturn().getResponse();
85 return new StandardCoder().decode(response.getContentAsString(), ApexApiResult.class);
89 * Creates a new session.
91 * @return the session ID
93 private int createNewSession() throws Exception {
94 final ApexApiResult responseMsg = apexRest(get("/policy/gui/v1/apex/editor/-1/Session/Create"));
95 assertEquals(ApexApiResult.Result.SUCCESS, responseMsg.getResult());
96 assertEquals(1, responseMsg.getMessages().size());
97 return Integer.parseInt(responseMsg.getMessages().get(0));
103 * @param sessionId the session ID
104 * @param modelAsJsonString the model as json string
106 private void uploadPolicy(final int sessionId, final String modelAsJsonString) throws Exception {
107 final ApexApiResult responseMsg = apexRest(put("/policy/gui/v1/apex/editor/" + sessionId + "/Model/Load")
108 .content(modelAsJsonString).contentType(APPLICATION_JSON));
109 assertTrue(responseMsg.isOk());
113 * Create a new session, Upload a test policy model, then get a policy, parse it, and compare it to the same policy
114 * in the original model.
116 * @throws ApexException if there is an Apex Error
119 void testUploadThenGet() throws Exception {
121 final int sessionId = createNewSession();
123 uploadPolicy(sessionId, localModelString);
125 final ApexApiResult responseMsg = apexRest(get("/policy/gui/v1/apex/editor/" + sessionId + "/Policy/Get")
126 .queryParam("name", "policy").queryParam("version", "0.0.1"));
127 assertTrue(responseMsg.isOk());
129 // The string in responseMsg.Messages[0] is a JSON representation of a AxPolicy
130 // object. Lets parse it
131 final String returnedPolicyAsString = responseMsg.getMessages().get(0);
132 ApexModelReader<AxPolicy> apexPolicyReader = new ApexModelReader<>(AxPolicy.class, false);
133 final AxPolicy returnedPolicy = apexPolicyReader.read(returnedPolicyAsString);
135 assertNotNull(returnedPolicy);
136 assertEquals("state", returnedPolicy.getFirstState());
137 assertEquals(1, returnedPolicy.getStateMap().size());