f97897a2dc2c9ceb22fd32374fd60550882690de
[policy/gui.git] /
1 /*-
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.gui.editors.apex.rest;
24
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;
31
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;
47
48 /**
49  * The RestInterface Test.
50  */
51 @SpringBootTest(classes = ApexEditor.class)
52 @AutoConfigureMockMvc
53 class RestInterfaceTest {
54
55     @Autowired
56     private MockMvc mvc;
57
58     private static final String TESTMODELFILE = "models/PolicyModel.yaml";
59
60     private static String localModelString = null;
61
62     /**
63      * Sets up the tests.
64      *
65      */
66     @BeforeAll
67     static void setUp() {
68         // load a test model locally
69         localModelString = ResourceUtils.getResourceAsString(TESTMODELFILE);
70     }
71
72     /**
73      * Test to see that the message create Model with model id -1 .
74      */
75     @Test
76     void createSession() throws Exception {
77         createNewSession();
78     }
79
80     /**
81      * Helper method to invoke rest call using mock mvc, and return ApexApiResult.
82      */
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);
86     }
87
88     /**
89      * Creates a new session.
90      *
91      * @return the session ID
92      */
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));
98     }
99
100     /**
101      * Upload policy.
102      *
103      * @param sessionId         the session ID
104      * @param modelAsJsonString the model as json string
105      */
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());
110     }
111
112     /**
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.
115      *
116      * @throws ApexException if there is an Apex Error
117      **/
118     @Test
119     void testUploadThenGet() throws Exception {
120
121         final int sessionId = createNewSession();
122
123         uploadPolicy(sessionId, localModelString);
124
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());
128
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);
134
135         assertNotNull(returnedPolicy);
136         assertEquals("state", returnedPolicy.getFirstState());
137         assertEquals(1, returnedPolicy.getStateMap().size());
138     }
139 }