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.web.servlet.MockMvc;
45 import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
48 * The RestInterface Test.
50 @SpringBootTest(classes = ApexEditor.class)
52 class RestInterfaceTest {
57 private static final String TESTMODELFILE = "models/PolicyModel.yaml";
59 private static String localModelString = null;
67 // load a test model locally
68 localModelString = ResourceUtils.getResourceAsString(TESTMODELFILE);
72 * Test to see that the message create Model with model id -1 .
75 void createSession() throws Exception {
80 * Helper method to invoke rest call using mock mvc, and return ApexApiResult.
82 private ApexApiResult apexRest(MockHttpServletRequestBuilder requestBuilder) throws Exception {
83 var response = mvc.perform(requestBuilder).andReturn().getResponse();
84 return new StandardCoder().decode(response.getContentAsString(), ApexApiResult.class);
88 * Creates a new session.
90 * @return the session ID
92 private int createNewSession() throws Exception {
93 final ApexApiResult responseMsg = apexRest(get("/policy/gui/v1/apex/editor/-1/Session/Create"));
94 assertEquals(ApexApiResult.Result.SUCCESS, responseMsg.getResult());
95 assertEquals(1, responseMsg.getMessages().size());
96 return Integer.parseInt(responseMsg.getMessages().get(0));
102 * @param sessionId the session ID
103 * @param modelAsJsonString the model as json string
105 private void uploadPolicy(final int sessionId, final String modelAsJsonString) throws Exception {
106 final ApexApiResult responseMsg = apexRest(put("/policy/gui/v1/apex/editor/" + sessionId + "/Model/Load")
107 .content(modelAsJsonString).contentType(APPLICATION_JSON));
108 assertTrue(responseMsg.isOk());
112 * Create a new session, Upload a test policy model, then get a policy, parse it, and compare it to the same policy
113 * in the original model.
115 * @throws ApexException if there is an Apex Error
118 void testUploadThenGet() throws Exception {
120 final int sessionId = createNewSession();
122 uploadPolicy(sessionId, localModelString);
124 final ApexApiResult responseMsg = apexRest(get("/policy/gui/v1/apex/editor/" + sessionId + "/Policy/Get")
125 .queryParam("name", "policy").queryParam("version", "0.0.1"));
126 assertTrue(responseMsg.isOk());
128 // The string in responseMsg.Messages[0] is a JSON representation of a AxPolicy
129 // object. Lets parse it
130 final String returnedPolicyAsString = responseMsg.getMessages().get(0);
131 ApexModelReader<AxPolicy> apexPolicyReader = new ApexModelReader<>(AxPolicy.class, false);
132 final AxPolicy returnedPolicy = apexPolicyReader.read(returnedPolicyAsString);
134 assertNotNull(returnedPolicy);
135 assertEquals("state", returnedPolicy.getFirstState());
136 assertEquals(1, returnedPolicy.getStateMap().size());