17e4459ea6522eeb672a9df4ebd31aaf31b680cb
[usecase-ui/server.git] /
1 /**
2  * Copyright 2025 Deutsche Telekom.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package org.onap.usecaseui.server.service.intent.impl;
17
18 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
19 import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
20 import static com.github.tomakehurst.wiremock.client.WireMock.equalToJson;
21 import static com.github.tomakehurst.wiremock.client.WireMock.get;
22 import static com.github.tomakehurst.wiremock.client.WireMock.getRequestedFor;
23 import static com.github.tomakehurst.wiremock.client.WireMock.post;
24 import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
25 import static com.github.tomakehurst.wiremock.client.WireMock.put;
26 import static com.github.tomakehurst.wiremock.client.WireMock.putRequestedFor;
27 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
28 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
29 import static org.junit.Assert.assertEquals;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.when;
32
33 import java.nio.charset.StandardCharsets;
34 import java.nio.file.Files;
35 import java.nio.file.Paths;
36
37 import org.hibernate.Session;
38 import org.hibernate.SessionFactory;
39 import org.hibernate.Transaction;
40 import org.junit.jupiter.api.BeforeEach;
41 import org.junit.jupiter.api.Test;
42 import org.mockito.Mock;
43 import org.onap.usecaseui.server.bean.intent.CCVPNInstance;
44 import org.onap.usecaseui.server.config.AAIClientConfig;
45 import org.onap.usecaseui.server.config.SOClientConfig;
46 import org.onap.usecaseui.server.service.csmf.SlicingService;
47 import org.onap.usecaseui.server.service.csmf.config.SlicingProperties;
48 import org.onap.usecaseui.server.service.csmf.impl.SlicingServiceImpl;
49 import org.onap.usecaseui.server.service.intent.IntentAaiClient;
50 import org.onap.usecaseui.server.service.intent.IntentSoService;
51 import org.onap.usecaseui.server.service.intent.config.IntentProperties;
52 import org.onap.usecaseui.server.service.lcm.impl.DefaultServiceLcmService;
53 import org.onap.usecaseui.server.service.nsmf.impl.ResourceMgtServiceImpl;
54 import org.springframework.beans.factory.annotation.Autowired;
55 import org.springframework.beans.factory.annotation.Value;
56 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
57 import org.springframework.boot.context.properties.EnableConfigurationProperties;
58 import org.springframework.boot.test.context.SpringBootTest;
59 import org.springframework.boot.test.mock.mockito.MockBean;
60 import org.springframework.http.HttpHeaders;
61 import org.wiremock.spring.EnableWireMock;
62
63 import com.github.tomakehurst.wiremock.client.WireMock;
64
65 import lombok.SneakyThrows;
66
67 @EnableWireMock
68 @EnableAutoConfiguration
69 @SpringBootTest(classes = {
70     AAIClientConfig.class,
71     SOClientConfig.class,
72     SlicingServiceImpl.class,
73     SlicingProperties.class,
74     IntentProperties.class
75 }, properties = {
76     "uui-server.client.aai.baseUrl=${wiremock.server.baseUrl}",
77     "uui-server.client.aai.username=AAI",
78     "uui-server.client.aai.password=AAI",
79     "uui-server.client.so.baseUrl=${wiremock.server.baseUrl}",
80     "uui-server.client.so.username=InfraPortalClient",
81     "uui-server.client.so.password=password1",
82     "uui-server.slicing.service-invariant-uuid=someServiceInvariantUuid",
83     "uui-server.slicing.service-uuid=someServiceUuid",
84     "uui-server.slicing.global-subscriber-id=someGlobalSubscriberId",
85     "uui-server.slicing.service-type=someServiceType",
86     "uui-server.ccvpn.globalCustomerId=defaultGlobalCustomerId"
87 })
88 @EnableConfigurationProperties
89 public class IntentInstanceServiceIntegrationTest {
90
91   @Value("${uui-server.client.so.username}")
92   String soUsername;
93
94   @Value("${uui-server.client.so.password}")
95   String soPassword;
96
97   @Value("${uui-server.client.aai.username}")
98   String aaiUsername;
99
100   @Value("${uui-server.client.aai.password}")
101   String aaiPassword;
102
103   @Value("${uui-server.client.aai.apiVersion}")
104   String apiVersion;
105
106   @Mock
107   ResourceMgtServiceImpl resourceMgtServiceImpl;
108
109   @MockBean
110   DefaultServiceLcmService lcmService;
111
112   @Autowired
113   SlicingService slicingService;
114
115   @Autowired
116   IntentAaiClient intentAaiClient;
117
118   @Autowired
119   IntentSoService intentSoService;
120
121   @Autowired
122   IntentProperties intentProperties;
123
124   @Mock
125   Session session;
126
127   @BeforeEach
128   void setup() {
129     SessionFactory sessionFactory = mock(SessionFactory.class);
130     Transaction transaction = mock(Transaction.class);
131     when(sessionFactory.openSession()).thenReturn(session);
132     when(session.beginTransaction()).thenReturn(transaction);
133     this.intentService = new IntentInstanceServiceImpl(slicingService, intentAaiClient, intentSoService, sessionFactory,
134         resourceMgtServiceImpl, intentProperties);
135   }
136
137   IntentInstanceServiceImpl intentService;
138
139   @Test
140   @SneakyThrows
141   void thatCCVPNInstanceCanBeCreated() {
142     byte[] requestBytes = Files.readAllBytes(Paths.get("src/test/resources/__files/requests/createIntentRequest.json"));
143     String expectedRequestBody = new String(requestBytes, StandardCharsets.UTF_8);
144     stubFor(
145         post("/so/infra/serviceIntent/v1/create")
146             .withBasicAuth(soUsername, soPassword)
147             .withHeader(HttpHeaders.ACCEPT, equalTo("application/json"))
148             .withHeader("X-TransactionId", equalTo("9999"))
149             .withHeader("X-FromAppId", equalTo("onap-cli"))
150             .withRequestBody(equalToJson(expectedRequestBody))
151             .willReturn(
152                 aResponse().withBodyFile("createIntentResponse.json")));
153
154     stubFor(
155         get("/aai/%s/business/customers/customer/defaultGlobalCustomerId".formatted(apiVersion))
156             .withBasicAuth(aaiUsername, aaiPassword)
157             .withHeader(HttpHeaders.ACCEPT, equalTo("application/json"))
158             .withHeader("X-TransactionId", equalTo("7777"))
159             .withHeader("X-FromAppId", equalTo("uui"))
160             .willReturn(
161                 aResponse().withBodyFile("customersResponse.json")));
162
163     stubFor(
164         get("/aai/%s/business/customers/customer/defaultGlobalCustomerId/service-subscriptions/service-subscription/defaultServiceType".formatted(apiVersion))
165             .withBasicAuth(aaiUsername, aaiPassword)
166             .withHeader(HttpHeaders.ACCEPT, equalTo("application/json"))
167             .withHeader("X-TransactionId", equalTo("7777"))
168             .withHeader("X-FromAppId", equalTo("uui"))
169             .willReturn(
170                 aResponse().withBodyFile("customersResponse.json")));
171
172     stubFor(
173         put("/aai/%s/business/customers/customer/defaultGlobalCustomerId/service-subscriptions/service-subscription/defaultServiceType/service-instances/service-instance/IBN-someInstanceId".formatted(apiVersion))
174             .withBasicAuth(aaiUsername, aaiPassword)
175             .withHeader(HttpHeaders.ACCEPT, equalTo("application/json"))
176             .withHeader("X-TransactionId", equalTo("7777"))
177             .withHeader("X-FromAppId", equalTo("uui"))
178             .willReturn(
179                 aResponse().withBodyFile("customersResponse.json")));
180
181     CCVPNInstance ccVpnInstance = new CCVPNInstance();
182     ccVpnInstance.setInstanceId("someInstanceId");
183     ccVpnInstance.setAccessPointOneName("accessPointOneName");
184     int result = intentService.createCCVPNInstance(ccVpnInstance);
185     assertEquals(1, result);
186     WireMock.verify(postRequestedFor(urlEqualTo("/so/infra/serviceIntent/v1/create")));
187     WireMock.verify(getRequestedFor(urlEqualTo("/aai/%s/business/customers/customer/defaultGlobalCustomerId".formatted(apiVersion))));
188     WireMock.verify(getRequestedFor(urlEqualTo(
189         "/aai/%s/business/customers/customer/defaultGlobalCustomerId/service-subscriptions/service-subscription/defaultServiceType".formatted(apiVersion))));
190     WireMock.verify(putRequestedFor(urlEqualTo(
191         "/aai/%s/business/customers/customer/defaultGlobalCustomerId/service-subscriptions/service-subscription/defaultServiceType/service-instances/service-instance/IBN-someInstanceId".formatted(apiVersion))));
192   }
193
194 }