Fix Sonar vulnerabilities
[clamp.git] / src / test / java / org / onap / clamp / clds / client / DcaeInventoryServicesTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 Huawei Technologies Co., Ltd.
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  * ============LICENSE_END=========================================================
19  * ================================================================================
20  *
21  */
22
23 package org.onap.clamp.clds.client;
24
25 import static org.hamcrest.MatcherAssert.assertThat;
26 import static org.hamcrest.core.Is.is;
27 import static org.hamcrest.core.IsNull.nullValue;
28 import static org.onap.clamp.clds.client.DcaeInventoryServices.DCAE_INVENTORY_RETRY_INTERVAL;
29 import static org.onap.clamp.clds.client.DcaeInventoryServices.DCAE_INVENTORY_RETRY_LIMIT;
30 import static org.onap.clamp.clds.client.DcaeInventoryServices.DCAE_INVENTORY_URL;
31 import static org.powermock.api.mockito.PowerMockito.when;
32
33 import java.io.IOException;
34
35 import org.json.simple.parser.ParseException;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mock;
39 import org.mockito.runners.MockitoJUnitRunner;
40 import org.onap.clamp.clds.config.ClampProperties;
41 import org.onap.clamp.clds.model.dcae.DcaeInventoryResponse;
42 import org.onap.clamp.clds.model.dcae.DcaeLinks;
43 import org.onap.clamp.clds.model.dcae.DcaeOperationStatusResponse;
44 import org.onap.clamp.util.HttpConnectionManager;
45
46
47 @RunWith(MockitoJUnitRunner.class)
48 public class DcaeInventoryServicesTest {
49
50     @Mock
51     private HttpConnectionManager httpConnectionManager;
52
53     @Mock
54     private ClampProperties properties;
55
56     private static final String resourceUuid = "023a3f0d-1161-45ff-b4cf-8918a8ccf3ad";
57     private static final String serviceUuid = "4cc5b45a-1f63-4194-8100-cd8e14248c92";
58     private static final String artifactName = "tca_2.yaml";
59     private static final String queryString = "?asdcResourceId=" + resourceUuid + "&asdcServiceId=" + serviceUuid
60             + "&typeName=" + artifactName;
61     private static final String url = "http://localhost:8085" + "/dcae-service-types" + queryString;
62
63     @Test
64     public void testDcaeInventoryResponse() throws ParseException, InterruptedException, IOException {
65         when(properties.getStringValue(DCAE_INVENTORY_URL)).thenReturn("http://localhost:8085");
66         when(properties.getStringValue(DCAE_INVENTORY_RETRY_LIMIT)).thenReturn("1");
67         when(properties.getStringValue(DCAE_INVENTORY_RETRY_INTERVAL)).thenReturn("100");
68         String responseStr = "{\"totalCount\":1, "
69                 + "\"items\":[{\"typeId\":\"typeId-32147723-d323-48f9-a325-bcea8d728025\","
70                 + " \"typeName\":\"typeName-32147723-d323-48f9-a325-bcea8d728025\"}]}";
71         when(httpConnectionManager.doHttpRequest(url, "GET", null, null,
72                                                  "DCAE", null, null))
73                 .thenReturn(responseStr);
74
75         DcaeInventoryServices services = new DcaeInventoryServices(properties,
76                                                                    httpConnectionManager);
77         DcaeInventoryResponse response = services.getDcaeInformation(artifactName, serviceUuid, resourceUuid);
78         assertThat(response.getTypeId(),is("typeId-32147723-d323-48f9-a325-bcea8d728025"));
79         assertThat(response.getTypeName(),is("typeName-32147723-d323-48f9-a325-bcea8d728025"));
80     }
81
82     @Test
83     public void testDcaeInventoryResponseWithZeroCount() throws ParseException, InterruptedException, IOException {
84         when(properties.getStringValue(DCAE_INVENTORY_URL)).thenReturn("http://localhost:8085");
85         when(properties.getStringValue(DCAE_INVENTORY_RETRY_LIMIT)).thenReturn("1");
86         when(properties.getStringValue(DCAE_INVENTORY_RETRY_INTERVAL)).thenReturn("100");
87         when(httpConnectionManager.doHttpRequest(url, "GET", null, null,
88                                                  "DCAE", null, null))
89                 .thenReturn("{\"totalCount\":0}\"}]}");
90         DcaeInventoryServices services = new DcaeInventoryServices(properties,
91                                                                    httpConnectionManager);
92         DcaeInventoryResponse response = services.getDcaeInformation(artifactName, serviceUuid, resourceUuid);
93         assertThat(response, nullValue());
94     }
95
96     @Test
97     public void testDcaeInventoryResponsePojo() {
98         DcaeInventoryResponse response = new DcaeInventoryResponse();
99         response.setTypeId("typeId-32147723-d323-48f9-a325-bcea8d728025");
100         response.setTypeName("typeName-32147723-d323-48f9-a325-bcea8d728025");
101         assertThat(response.getTypeId(),is("typeId-32147723-d323-48f9-a325-bcea8d728025"));
102         assertThat(response.getTypeName(),is("typeName-32147723-d323-48f9-a325-bcea8d728025"));
103     }
104
105     @Test
106     public void testDcaeOperationStatusResponsePojo() {
107         DcaeLinks links = new DcaeLinks();
108         links.setSelf("selfUrl");
109         links.setStatus("state");
110         links.setUninstall("uninstallUrl");
111         DcaeOperationStatusResponse response = new DcaeOperationStatusResponse();
112         response.setRequestId("testId");
113         response.setError("errorMessage");
114         response.setLinks(links);
115         response.setOperationType("install");
116         response.setStatus("state");
117         assertThat(response.getRequestId(),is("testId"));
118         assertThat(response.getError(),is("errorMessage"));
119         assertThat(response.getOperationType(),is("install"));
120         assertThat(response.getStatus(),is("state"));
121         assertThat(response.getLinks().getSelf(),is("selfUrl"));
122         assertThat(response.getLinks().getStatus(),is("state"));
123         assertThat(response.getLinks().getUninstall(),is("uninstallUrl"));
124     }
125 }