Merge "Add tests for AaiServiceImpl"
[vid.git] / vid-app-common / src / test / java / org / onap / vid / aai / PombaClientImplTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * VID
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright 2019 Nokia
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22 package org.onap.vid.aai;
23
24 import static org.mockito.BDDMockito.given;
25 import static org.mockito.BDDMockito.then;
26
27 import com.fasterxml.jackson.databind.ObjectMapper;
28 import com.google.common.collect.Lists;
29 import java.io.IOException;
30 import java.net.URL;
31 import org.junit.Test;
32 import org.junit.runner.RunWith;
33 import org.mockito.InjectMocks;
34 import org.mockito.Mock;
35 import org.mockito.junit.MockitoJUnitRunner;
36 import org.onap.vid.model.PombaInstance.PombaRequest;
37 import org.onap.vid.model.PombaInstance.ServiceInstance;
38 import org.onap.vid.utils.SystemPropertiesWrapper;
39
40 @RunWith(MockitoJUnitRunner.class)
41 public class PombaClientImplTest {
42
43     @Mock
44     private SystemPropertiesWrapper systemPropertiesWrapper;
45     @Mock
46     private PombaRestInterface pombaRestInterface;
47     @InjectMocks
48     private PombaClientImpl pombaClient;
49
50     @Test
51     public void should_doHttpPost_withGivenPombaRequest() throws IOException {
52         //Given
53         String expectedUrl = "http://localhost/dummyUrl";
54         given(systemPropertiesWrapper.getProperty("pomba.server.url")).willReturn(expectedUrl);
55         String expectedPayload = readExpectedPombaJsonRequest();
56         PombaRequest pombaRequest = createPombaRequest();
57
58         //When
59         pombaClient.verify(pombaRequest);
60
61         //Then
62         then(pombaRestInterface).should().RestPost("VidAaiController", expectedUrl, expectedPayload);
63     }
64
65     private String readExpectedPombaJsonRequest() throws IOException {
66         URL url = PombaClientImplTest.class.getClassLoader().getResource("pomba_request.json");
67         PombaRequest expectedPombaRequest = new ObjectMapper().readValue(url, PombaRequest.class);
68         return new ObjectMapper().writeValueAsString(expectedPombaRequest);
69     }
70
71     private PombaRequest createPombaRequest() {
72         ServiceInstance serviceInstance1 = createServiceInstance("serviceType1", "serviceInstanceId1", "customerId1",
73             "modelVersion1", "modelInvariantId1");
74         ServiceInstance serviceInstance2 = createServiceInstance("serviceType2", "serviceInstanceId2", "customerId2",
75             "modelVersion2", "modelInvariantId2");
76
77         PombaRequest pombaRequest = new PombaRequest();
78         pombaRequest.serviceInstanceList = Lists.newArrayList(serviceInstance1, serviceInstance2);
79         return pombaRequest;
80     }
81
82     private ServiceInstance createServiceInstance(String serviceType, String serviceInstanceId, String customerId,
83         String modelVersionId, String modelInvariantId) {
84         ServiceInstance serviceInstance = new ServiceInstance();
85         serviceInstance.serviceType = serviceType;
86         serviceInstance.serviceInstanceId = serviceInstanceId;
87         serviceInstance.customerId = customerId;
88         serviceInstance.modelInvariantId = modelInvariantId;
89         serviceInstance.modelVersionId = modelVersionId;
90         return serviceInstance;
91     }
92
93 }