aboutsummaryrefslogtreecommitdiffstats
path: root/runtime/src/test/java/org/onap/policy/clamp/runtime/RuntimeInstantiationResponseItTestCase.java
blob: d655257c664dc73233edaabc64bd7d0f153e15ce (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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
/*
 * ============LICENSE_START=======================================================
 *  Copyright (C) 2021 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.policy.clamp.runtime;

import static org.assertj.core.api.Assertions.assertThat;

import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.ProducerTemplate;
import org.apache.camel.builder.ExchangeBuilder;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.onap.policy.clamp.clds.Application;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.http.HttpStatus;
import org.springframework.test.context.junit4.SpringRunner;

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Application.class)
public class RuntimeInstantiationResponseItTestCase {
    @Autowired
    CamelContext camelContext;

    private static final String DIRECT_GET_TOSCA_INSTANTIATION = "direct:get-tosca-instantiation";

    private static final String DIRECT_POST_TOSCA_INSTANCE_PROPERTIES = "direct:post-tosca-instance-properties";

    private static final String DIRECT_DELETE_TOSCA_INSTANCE_PROPERTIES = "direct:delete-tosca-instance-properties";

    private static final String DIRECT_PUT_TOSCA_INSTANCE_PROPERTIES = "direct:put-tosca-instantiation";

    private static final String SERVICE_TEMPLATE_NAME = "name";

    private static final String SERVICE_TEMPLATE_VERSION = "version";

    private static final String RAISE_HTTP_EXCEPTION_FLAG = "raiseHttpExceptionFlag";

    private static final String SAMPLE_CONTROL_LOOP_LIST =
        "{\"automationCompositionList\": [{\"name\": \"PMSHInstance0\","
        + "\"version\": \"1.0.1\",\"definition\": {},\"state\": \"UNINITIALISED\",\"orderedState\": \"UNINITIALISED\","
        + "\"description\": \"PMSH Automation Composition instance 0\",\"elements\": {}}]}";

    private static final String SAMPLE_TOSCA_TEMPLATE =
        "{\"tosca_definitions_version\": \"tosca_simple_yaml_1_1_0\","
            + "\"data_types\": {},\"node_types\": {}, \"policy_types\": {},"
            + " \"topology_template\": {},"
            + " \"name\": \"ToscaServiceTemplateSimple\", \"version\": \"1.0.0\", \"metadata\": {}}";

    private static final String SAMPLE_TOSCA_CHANGE_ORDER_STATE = "{\"orderedState\":\"PASSIVE\","
        + "\"controlLoopIdentifierList\":[{\"name\":\"PMSH_Instance1\",\"version\":\"2.3.1\"}]}";

    @Test
    public void testToscaServiceTemplateStatus() {
        ProducerTemplate prodTemplate = camelContext.createProducerTemplate();

        Exchange exchangeResponse =
            prodTemplate.send(DIRECT_GET_TOSCA_INSTANTIATION, ExchangeBuilder.anExchange(camelContext)
                .withProperty(SERVICE_TEMPLATE_NAME, "ToscaServiceTemplate")
                .withProperty(SERVICE_TEMPLATE_VERSION, "1.0.0")
                .withProperty(RAISE_HTTP_EXCEPTION_FLAG, "true")
                .build());

        assertThat(HttpStatus.valueOf((Integer) exchangeResponse.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE))
            .is2xxSuccessful()).isTrue();
    }

    @Test
    public void testToscaInstantiationGetResponseBody() {
        ProducerTemplate prodTemplate = camelContext.createProducerTemplate();

        Exchange exchangeResponse =
            prodTemplate.send(DIRECT_GET_TOSCA_INSTANTIATION, ExchangeBuilder.anExchange(camelContext)
                .withProperty(SERVICE_TEMPLATE_NAME, "ToscaServiceTemplate")
                .withProperty(SERVICE_TEMPLATE_VERSION, "1.0.0")
                .withProperty(RAISE_HTTP_EXCEPTION_FLAG, "true")
                .build());

        assertThat(exchangeResponse.getIn().getBody()).hasToString(SAMPLE_CONTROL_LOOP_LIST);
    }

    @Test
    public void testToscaInstantiationStatus() {
        ProducerTemplate prodTemplate = camelContext.createProducerTemplate();

        Exchange exchangeResponse =
            prodTemplate.send(DIRECT_GET_TOSCA_INSTANTIATION, ExchangeBuilder.anExchange(camelContext)
                .withBody(SAMPLE_CONTROL_LOOP_LIST)
                .withProperty(RAISE_HTTP_EXCEPTION_FLAG, "true")
                .build());

        assertThat(HttpStatus.valueOf((Integer) exchangeResponse.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE))
            .is2xxSuccessful()).isTrue();
    }

    @Test
    public void testCreateToscaInstancePropertiesStatus() {
        ProducerTemplate prodTemplate = camelContext.createProducerTemplate();

        Exchange exchangeResponse =
            prodTemplate.send(DIRECT_POST_TOSCA_INSTANCE_PROPERTIES, ExchangeBuilder.anExchange(camelContext)
                .withBody(SAMPLE_TOSCA_TEMPLATE)
                .withProperty("raiseHttpExceptionFlag", "true")
                .build());

        assertThat(HttpStatus.valueOf((Integer) exchangeResponse.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE))
            .is2xxSuccessful()).isTrue();
    }

    @Test
    public void testDeleteToscaInstancePropertiesStatus() {
        ProducerTemplate prodTemplate = camelContext.createProducerTemplate();

        Exchange exchangeResponse =
            prodTemplate.send(DIRECT_DELETE_TOSCA_INSTANCE_PROPERTIES, ExchangeBuilder.anExchange(camelContext)
                .withProperty("name", "PMSH_Instance1")
                .withProperty("version", "2.3.1")
                .withProperty("raiseHttpExceptionFlag", "true")
                .build());

        assertThat(HttpStatus.valueOf((Integer) exchangeResponse.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE))
            .is2xxSuccessful()).isTrue();
    }

    @Test
    public void testChangeOrderStateStatus() {
        ProducerTemplate prodTemplate = camelContext.createProducerTemplate();

        Exchange exchangeResponse =
            prodTemplate.send(DIRECT_PUT_TOSCA_INSTANCE_PROPERTIES, ExchangeBuilder.anExchange(camelContext)
                .withBody(SAMPLE_TOSCA_CHANGE_ORDER_STATE)
                .withProperty("raiseHttpExceptionFlag", "true")
                .build());

        assertThat(HttpStatus.valueOf((Integer) exchangeResponse.getIn().getHeader(Exchange.HTTP_RESPONSE_CODE))
            .is2xxSuccessful()).isTrue();
    }
}