Merge "Turn role management off by default"
[vid.git] / vid-app-common / src / test / java / org / onap / vid / aai / PombaClientImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2019 Nokia
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 package org.onap.vid.aai;
21
22 import static org.assertj.core.api.Assertions.assertThatCode;
23 import static org.mockito.BDDMockito.given;
24 import static org.mockito.BDDMockito.then;
25
26 import com.fasterxml.jackson.databind.ObjectMapper;
27 import com.google.common.collect.Lists;
28 import java.io.IOException;
29 import java.net.URL;
30 import org.junit.Test;
31 import org.junit.runner.RunWith;
32 import org.mockito.InjectMocks;
33 import org.mockito.Mock;
34 import org.mockito.junit.MockitoJUnitRunner;
35 import org.onap.vid.model.PombaInstance.PombaRequest;
36 import org.onap.vid.model.PombaInstance.ServiceInstance;
37 import org.onap.vid.utils.SystemPropertiesWrapper;
38
39 @RunWith(MockitoJUnitRunner.class)
40 public class PombaClientImplTest {
41
42     @Mock
43     private SystemPropertiesWrapper systemPropertiesWrapper;
44     @Mock
45     private PombaRestInterface pombaRestInterface;
46     @InjectMocks
47     private PombaClientImpl pombaClient;
48
49     @Test
50     public void should_doHttpPost_withGivenPombaRequest() throws IOException {
51         //Given
52         String expectedUrl = "http://localhost/dummyUrl";
53         given(systemPropertiesWrapper.getProperty("pomba.server.url")).willReturn(expectedUrl);
54         String expectedPayload = readExpectedPombaJsonRequest();
55         PombaRequest pombaRequest = createPombaRequest();
56
57         //When
58         pombaClient.verify(pombaRequest);
59
60         //Then
61         then(pombaRestInterface).should().RestPost("VidAaiController", expectedUrl, expectedPayload);
62     }
63
64     @Test
65     public void should_handleException_withoutRethrowing() throws IOException {
66         //Given
67         String expectedUrl = "http://localhost/dummyUrl";
68         String expectedPayload = readExpectedPombaJsonRequest();
69         given(systemPropertiesWrapper.getProperty("pomba.server.url")).willReturn(expectedUrl);
70         given(pombaRestInterface.RestPost("VidAaiController", expectedUrl, expectedPayload))
71             .willThrow(new NullPointerException());
72         PombaRequest pombaRequest = createPombaRequest();
73
74         //When //Then
75         assertThatCode(() -> pombaClient.verify(pombaRequest)).doesNotThrowAnyException();
76     }
77
78     private String readExpectedPombaJsonRequest() throws IOException {
79         URL url = PombaClientImplTest.class.getClassLoader().getResource("pomba_request.json");
80         PombaRequest expectedPombaRequest = new ObjectMapper().readValue(url, PombaRequest.class);
81         return new ObjectMapper().writeValueAsString(expectedPombaRequest);
82     }
83
84     private PombaRequest createPombaRequest() {
85         ServiceInstance serviceInstance1 = createServiceInstance("serviceType1", "serviceInstanceId1", "customerId1",
86             "modelVersion1", "modelInvariantId1");
87         ServiceInstance serviceInstance2 = createServiceInstance("serviceType2", "serviceInstanceId2", "customerId2",
88             "modelVersion2", "modelInvariantId2");
89
90         PombaRequest pombaRequest = new PombaRequest(Lists.newArrayList(serviceInstance1, serviceInstance2));
91         return pombaRequest;
92     }
93
94     private ServiceInstance createServiceInstance(String serviceType, String serviceInstanceId, String customerId,
95         String modelVersionId, String modelInvariantId) {
96         ServiceInstance serviceInstance = new ServiceInstance(
97             serviceInstanceId,
98             modelVersionId,
99             modelInvariantId,
100             customerId,
101             serviceType);
102         return serviceInstance;
103     }
104
105 }