aboutsummaryrefslogtreecommitdiffstats
path: root/gui-editors/gui-editor-apex/src/test/java/org/onap/policy/gui/editors/apex/rest/RestInterfaceTest.java
blob: f97897a2dc2c9ceb22fd32374fd60550882690de (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
 *  Modifications Copyright (C) 2020-2022 Nordix Foundation.
 *  Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 *
 * SPDX-License-Identifier: Apache-2.0
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.gui.editors.apex.rest;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;

import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.onap.policy.apex.model.basicmodel.concepts.ApexException;
import org.onap.policy.apex.model.basicmodel.handling.ApexModelReader;
import org.onap.policy.apex.model.modelapi.ApexApiResult;
import org.onap.policy.apex.model.policymodel.concepts.AxPolicy;
import org.onap.policy.common.utils.coder.StandardCoder;
import org.onap.policy.common.utils.resources.ResourceUtils;
import org.onap.policy.gui.editors.apex.ApexEditor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;

/**
 * The RestInterface Test.
 */
@SpringBootTest(classes = ApexEditor.class)
@AutoConfigureMockMvc
class RestInterfaceTest {

    @Autowired
    private MockMvc mvc;

    private static final String TESTMODELFILE = "models/PolicyModel.yaml";

    private static String localModelString = null;

    /**
     * Sets up the tests.
     *
     */
    @BeforeAll
    static void setUp() {
        // load a test model locally
        localModelString = ResourceUtils.getResourceAsString(TESTMODELFILE);
    }

    /**
     * Test to see that the message create Model with model id -1 .
     */
    @Test
    void createSession() throws Exception {
        createNewSession();
    }

    /**
     * Helper method to invoke rest call using mock mvc, and return ApexApiResult.
     */
    private ApexApiResult apexRest(MockHttpServletRequestBuilder requestBuilder) throws Exception {
        var response = mvc.perform(requestBuilder).andReturn().getResponse();
        return new StandardCoder().decode(response.getContentAsString(), ApexApiResult.class);
    }

    /**
     * Creates a new session.
     *
     * @return the session ID
     */
    private int createNewSession() throws Exception {
        final ApexApiResult responseMsg = apexRest(get("/policy/gui/v1/apex/editor/-1/Session/Create"));
        assertEquals(ApexApiResult.Result.SUCCESS, responseMsg.getResult());
        assertEquals(1, responseMsg.getMessages().size());
        return Integer.parseInt(responseMsg.getMessages().get(0));
    }

    /**
     * Upload policy.
     *
     * @param sessionId         the session ID
     * @param modelAsJsonString the model as json string
     */
    private void uploadPolicy(final int sessionId, final String modelAsJsonString) throws Exception {
        final ApexApiResult responseMsg = apexRest(put("/policy/gui/v1/apex/editor/" + sessionId + "/Model/Load")
            .content(modelAsJsonString).contentType(APPLICATION_JSON));
        assertTrue(responseMsg.isOk());
    }

    /**
    * Create a new session, Upload a test policy model, then get a policy, parse it, and compare it to the same policy
     * in the original model.
     *
     * @throws ApexException if there is an Apex Error
     **/
    @Test
    void testUploadThenGet() throws Exception {

        final int sessionId = createNewSession();

        uploadPolicy(sessionId, localModelString);

        final ApexApiResult responseMsg = apexRest(get("/policy/gui/v1/apex/editor/" + sessionId + "/Policy/Get")
            .queryParam("name", "policy").queryParam("version", "0.0.1"));
        assertTrue(responseMsg.isOk());

        // The string in responseMsg.Messages[0] is a JSON representation of a AxPolicy
        // object. Lets parse it
        final String returnedPolicyAsString = responseMsg.getMessages().get(0);
        ApexModelReader<AxPolicy> apexPolicyReader = new ApexModelReader<>(AxPolicy.class, false);
        final AxPolicy returnedPolicy = apexPolicyReader.read(returnedPolicyAsString);

        assertNotNull(returnedPolicy);
        assertEquals("state", returnedPolicy.getFirstState());
        assertEquals(1, returnedPolicy.getStateMap().size());
    }
}