Merge "Validate ids"
[clamp.git] / src / test / java / org / onap / clamp / loop / DcaeComponentTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.loop;
25
26 import static org.assertj.core.api.Assertions.assertThat;
27
28 import com.google.gson.Gson;
29 import com.google.gson.JsonObject;
30
31 import java.io.IOException;
32 import java.util.HashSet;
33
34 import org.junit.Test;
35 import org.onap.clamp.clds.model.dcae.DcaeOperationStatusResponse;
36 import org.onap.clamp.loop.components.external.DcaeComponent;
37 import org.onap.clamp.policy.microservice.MicroServicePolicy;
38
39 public class DcaeComponentTest {
40
41     private Loop createTestLoop() {
42         String yaml = "imports:\n" + "  - \"http://www.getcloudify.org/spec/cloudify/3.4/types.yaml\"\n"
43             + "node_templates:\n" + "  docker_service_host:\n" + "    type: dcae.nodes.SelectedDockerHost";
44
45         Loop loopTest = new Loop("ControlLoopTest", yaml, "<xml></xml>");
46         loopTest.setGlobalPropertiesJson(
47             new Gson().fromJson("{\"dcaeDeployParameters\":" + "{\"policy_id\": \"name\"}}", JsonObject.class));
48         loopTest.setLastComputedState(LoopState.DESIGN);
49         loopTest.setDcaeDeploymentId("123456789");
50         loopTest.setDcaeDeploymentStatusUrl("http4://localhost:8085");
51         loopTest.setDcaeBlueprintId("UUID-blueprint");
52
53         MicroServicePolicy microServicePolicy = new MicroServicePolicy("configPolicyTest", "",
54             "tosca_definitions_version: tosca_simple_yaml_1_0_0", true,
55             new Gson().fromJson("{\"configtype\":\"json\"}", JsonObject.class), new HashSet<>());
56         microServicePolicy.setProperties(new Gson().fromJson("{\"param1\":\"value1\"}", JsonObject.class));
57
58         loopTest.addMicroServicePolicy(microServicePolicy);
59         return loopTest;
60     }
61
62     @Test
63     public void convertDcaeResponseTest() throws IOException {
64         String dcaeFakeResponse = "{'requestId':'testId','operationType':'install','status':'state','error':'errorMessage', 'links':{'self':'selfUrl','uninstall':'uninstallUrl'}}";
65         DcaeOperationStatusResponse responseObject = DcaeComponent.convertDcaeResponse(dcaeFakeResponse);
66         assertThat(responseObject.getRequestId()).isEqualTo("testId");
67         assertThat(responseObject.getOperationType()).isEqualTo("install");
68         assertThat(responseObject.getStatus()).isEqualTo("state");
69         assertThat(responseObject.getError()).isEqualTo("errorMessage");
70         assertThat(responseObject.getLinks()).isNotNull();
71         assertThat(responseObject.getLinks().getSelf()).isEqualTo("selfUrl");
72         assertThat(responseObject.getLinks().getUninstall()).isEqualTo("uninstallUrl");
73
74         assertThat(responseObject.getLinks().getStatus()).isNull();
75     }
76
77     @Test
78     public void testGetDeployPayload() throws IOException {
79         Loop loop = this.createTestLoop();
80         String deploymentPayload = DcaeComponent.getDeployPayload(loop);
81         String expectedPayload = "{\"serviceTypeId\":\"UUID-blueprint\",\"inputs\":{\"policy_id\":\"name\"}}";
82         assertThat(deploymentPayload).isEqualTo(expectedPayload);
83     }
84
85     @Test
86     public void testGetUndeployPayload() throws IOException {
87         Loop loop = this.createTestLoop();
88         String unDeploymentPayload = DcaeComponent.getUndeployPayload(loop);
89         String expectedPayload = "{\"serviceTypeId\":\"UUID-blueprint\"}";
90         assertThat(unDeploymentPayload).isEqualTo(expectedPayload);
91     }
92
93 }