summaryrefslogtreecommitdiffstats
path: root/so-cnfm/so-cnfm-lcm/so-cnfm-lcm-service/src/test/java/org/onap/so/cnfm/lcm/rest/AsLcmOperationOccurrencesControllerTest.java
blob: fc71ed92b7dbb1d764eac603b35d0f422067f8f0 (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
/*-
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2023 Nordix Foundation.
 * ================================================================================
 * 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.so.cnfm.lcm.rest;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import java.time.LocalDateTime;
import java.util.UUID;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.onap.so.cnfm.lcm.Constants;
import org.onap.so.cnfm.lcm.TestApplication;
import org.onap.so.cnfm.lcm.bpmn.flows.GsonProvider;
import org.onap.so.cnfm.lcm.database.beans.AsInst;
import org.onap.so.cnfm.lcm.database.beans.AsLcmOpType;
import org.onap.so.cnfm.lcm.database.beans.OperationStateEnum;
import org.onap.so.cnfm.lcm.database.beans.State;
import org.onap.so.cnfm.lcm.database.service.DatabaseServiceProvider;
import org.onap.so.cnfm.lcm.model.AsLcmOpOcc;
import org.onap.so.cnfm.lcm.model.ErrorDetails;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.web.client.TestRestTemplate;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.boot.web.server.LocalServerPort;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.http.converter.json.GsonHttpMessageConverter;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringRunner;
import com.google.gson.Gson;

/**
 *
 * @author Waqas Ikram (waqas.ikram@est.tech)
 *
 */
@RunWith(SpringRunner.class)
@SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@ActiveProfiles("test")
public class AsLcmOperationOccurrencesControllerTest {

    private static final String RANDOM_UUID = UUID.randomUUID().toString();

    private static final String AS_LCM_OP_OCCS = "/as_lcm_op_occs/";

    @LocalServerPort
    private int port;

    @Autowired
    private DatabaseServiceProvider databaseServiceProvider;

    @Autowired
    private GsonProvider gsonProvider;

    private TestRestTemplate testRestTemplate;

    @Before
    public void setUp() {
        final Gson gson = gsonProvider.getGson();
        testRestTemplate = new TestRestTemplate(
                new RestTemplateBuilder().additionalMessageConverters(new GsonHttpMessageConverter(gson)));
    }

    @Test
    public void testGetOperationStatus_validAsLcmOpOccId_returnsAsLcmOpOcc() {
        final String asLcmOpOccId = addDummyAsLcmOpOccToDatabase();
        final String baseUrl = getAsLcmBaseUrl() + AS_LCM_OP_OCCS + asLcmOpOccId;
        final HttpEntity<?> request = new HttpEntity<>(new HttpHeaders());
        final ResponseEntity<AsLcmOpOcc> responseEntity =
                testRestTemplate.exchange(baseUrl, HttpMethod.GET, request, AsLcmOpOcc.class);
        assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
        assertTrue(responseEntity.hasBody());
        assertNotNull(responseEntity.getBody());
    }

    @Test
    public void testGetOperationStatus_invalidAsLcmOpOccId_returnsErrorDetails() {
        final String baseUrl = getAsLcmBaseUrl() + AS_LCM_OP_OCCS + "123";
        final HttpEntity<?> request = new HttpEntity<>(new HttpHeaders());
        final ResponseEntity<ErrorDetails> responseEntity =
                testRestTemplate.exchange(baseUrl, HttpMethod.GET, request, ErrorDetails.class);
        assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
        assertTrue(responseEntity.hasBody());
        assertNotNull(responseEntity.getBody());
    }

    private String getAsLcmBaseUrl() {
        return "http://localhost:" + port + Constants.AS_LIFE_CYCLE_MANAGEMENT_BASE_URL;
    }

    private String addDummyAsLcmOpOccToDatabase() {
        final LocalDateTime now = LocalDateTime.now();
        final AsInst asInst = new AsInst().name("name").asdId(RANDOM_UUID).status(State.NOT_INSTANTIATED)
                .asdInvariantId(RANDOM_UUID).statusUpdatedTime(now).asApplicationName("asApplicationName")
                .asApplicationVersion("asApplicationVersion").asProvider("asProvider").serviceInstanceId(RANDOM_UUID)
                .serviceInstanceName("serviceInstanceName").cloudOwner("cloudOwner").cloudRegion("cloudRegion")
                .tenantId("tenantId").namespace("namespace");

        databaseServiceProvider.saveAsInst(asInst);

        final org.onap.so.cnfm.lcm.database.beans.AsLcmOpOcc databaseEntry =
                new org.onap.so.cnfm.lcm.database.beans.AsLcmOpOcc();

        databaseEntry.asInst(asInst).operationState(OperationStateEnum.PROCESSING).isCancelPending(false)
                .isAutoInvocation(false).operation(AsLcmOpType.INSTANTIATE).startTime(now).stateEnteredTime(now)
                .operationParams("");

        databaseServiceProvider.addAsLcmOpOcc(databaseEntry);

        return databaseEntry.getId();
    }

}