9adb88907ed1c46122fe4cb5e1716da49591233c
[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.web.servlet.MockMvc;
45 import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
46
47 /**
48  * The RestInterface Test.
49  */
50 @SpringBootTest(classes = ApexEditor.class)
51 @AutoConfigureMockMvc
52 class RestInterfaceTest {
53
54     @Autowired
55     private MockMvc mvc;
56
57     private static final String TESTMODELFILE = "models/PolicyModel.yaml";
58
59     private static String localModelString = null;
60
61     /**
62      * Sets up the tests.
63      *
64      */
65     @BeforeAll
66     static void setUp() {
67         // load a test model locally
68         localModelString = ResourceUtils.getResourceAsString(TESTMODELFILE);
69     }
70
71     /**
72      * Test to see that the message create Model with model id -1 .
73      */
74     @Test
75     void createSession() throws Exception {
76         createNewSession();
77     }
78
79     /**
80      * Helper method to invoke rest call using mock mvc, and return ApexApiResult.
81      */
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);
85     }
86
87     /**
88      * Creates a new session.
89      *
90      * @return the session ID
91      */
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));
97     }
98
99     /**
100      * Upload policy.
101      *
102      * @param sessionId         the session ID
103      * @param modelAsJsonString the model as json string
104      */
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());
109     }
110
111     /**
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.
114      *
115      * @throws ApexException if there is an Apex Error
116      **/
117     @Test
118     void testUploadThenGet() throws Exception {
119
120         final int sessionId = createNewSession();
121
122         uploadPolicy(sessionId, localModelString);
123
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());
127
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);
133
134         assertNotNull(returnedPolicy);
135         assertEquals("state", returnedPolicy.getFirstState());
136         assertEquals(1, returnedPolicy.getStateMap().size());
137     }
138 }