1a6c76c4a2115bce58437ca46aa915dec5e377ca
[dcaegen2/services/prh.git] /
1 /*
2  * ============LICENSE_START=======================================================
3  * PNF-REGISTRATION-HANDLER
4  * ================================================================================
5  * Copyright (C) 2019-2021 NOKIA Intellectual Property. All rights reserved.
6  * Copyright (C) 2023 Deutsche Telekom Intellectual Property. All rights 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 package org.onap.dcaegen2.services.prh.integration;
23
24 import com.github.tomakehurst.wiremock.client.WireMock;
25 import com.google.gson.Gson;
26 import com.google.gson.JsonObject;
27 import com.jayway.jsonpath.JsonPath;
28 import org.junit.jupiter.api.BeforeEach;
29 import org.junit.jupiter.api.Test;
30 import org.onap.dcaegen2.services.prh.MainApp;
31 import org.onap.dcaegen2.services.prh.configuration.CbsConfiguration;
32 import org.onap.dcaegen2.services.prh.tasks.ScheduledTasks;
33 import org.onap.dcaegen2.services.prh.tasks.ScheduledTasksRunner;
34 import org.springframework.beans.factory.annotation.Autowired;
35 import org.springframework.beans.factory.annotation.Value;
36 import org.springframework.boot.test.context.SpringBootTest;
37 import org.springframework.boot.test.mock.mockito.MockBean;
38 import org.springframework.cloud.contract.wiremock.AutoConfigureWireMock;
39 import org.springframework.context.annotation.Bean;
40 import org.springframework.context.annotation.Configuration;
41 import org.springframework.context.annotation.Import;
42 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
43 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
44 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
45 import static com.github.tomakehurst.wiremock.client.WireMock.ok;
46 import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
47 import static com.github.tomakehurst.wiremock.client.WireMock.get;
48 import static com.github.tomakehurst.wiremock.client.WireMock.post;
49 import static com.github.tomakehurst.wiremock.client.WireMock.anyRequestedFor;
50 import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
51 import static com.github.tomakehurst.wiremock.client.WireMock.matchingJsonPath;
52 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
53 import static com.github.tomakehurst.wiremock.client.WireMock.verify;
54 import static com.github.tomakehurst.wiremock.client.WireMock.patch;
55
56
57
58
59
60
61 import java.nio.file.Files;
62 import java.nio.file.Paths;
63
64
65 import static java.lang.ClassLoader.getSystemResource;
66 import static java.util.Collections.singletonList;
67
68
69 @SpringBootTest
70 @AutoConfigureWireMock(port = 0)
71 class PrhWorkflowIntegrationTest {
72
73     @Autowired
74     private ScheduledTasks scheduledTasks;
75
76     @MockBean
77     private ScheduledTasksRunner scheduledTasksRunner;  // just to disable scheduling - some configurability in ScheduledTaskRunner not to start tasks at app startup would be welcome
78
79
80     @Configuration
81     @Import(MainApp.class)
82     static class CbsConfigTestConfig {
83
84         @Value("http://localhost:${wiremock.server.port}")
85         private String wiremockServerAddress;
86
87         @Bean
88         public CbsConfiguration cbsConfiguration() {
89             JsonObject cbsConfigJson = new Gson().fromJson(getResourceContent("configurationFromCbs.json")
90                             .replaceAll("https?://dmaap-mr[\\w.]*:\\d+", wiremockServerAddress)
91                             .replaceAll("https?://aai[\\w.]*:\\d+", wiremockServerAddress),
92                     JsonObject.class);
93
94             CbsConfiguration cbsConfiguration = new CbsConfiguration();
95             cbsConfiguration.parseCBSConfig(cbsConfigJson);
96             return cbsConfiguration;
97         }
98     }
99
100     @BeforeEach
101     void resetWireMock() {
102         WireMock.reset();
103     }
104
105
106     @Test
107     void whenThereAreNoEventsInDmaap_WorkflowShouldFinish() {
108         stubFor(get(urlEqualTo("/events/unauthenticated.VES_PNFREG_OUTPUT/OpenDCAE-c12/c12"))
109                 .willReturn(aResponse().withBody("[]")));
110
111         scheduledTasks.scheduleMainPrhEventTask();
112
113         verify(0, anyRequestedFor(urlPathMatching("/aai.*")));
114         verify(0, postRequestedFor(urlPathMatching("/events.*")));
115     }
116
117
118     @Test
119     void whenThereIsAnEventsInDmaap_ShouldSendPnfReadyNotification() {
120         String event = getResourceContent("integration/event.json");
121         String pnfName = JsonPath.read(event, "$.event.commonEventHeader.sourceName");
122
123         stubFor(get(urlEqualTo("/events/unauthenticated.VES_PNFREG_OUTPUT/OpenDCAE-c12/c12"))
124                 .willReturn(ok().withBody(new Gson().toJson(singletonList(event)))));
125         stubFor(get(urlEqualTo("/aai/v23/network/pnfs/pnf/" + pnfName)).willReturn(ok().withBody("{}")));
126         stubFor(patch(urlEqualTo("/aai/v23/network/pnfs/pnf/" + pnfName)));
127         stubFor(post(urlEqualTo("/events/unauthenticated.PNF_READY")));
128
129         scheduledTasks.scheduleMainPrhEventTask();
130
131         verify(1, postRequestedFor(urlEqualTo("/events/unauthenticated.PNF_READY"))
132                 .withRequestBody(matchingJsonPath("$[0].correlationId", equalTo(pnfName))));
133     }
134
135
136     private static String getResourceContent(String resourceName) {
137         try {
138             return new String(Files.readAllBytes(Paths.get(getSystemResource(resourceName).toURI())));
139         } catch (Exception e) {
140             throw new RuntimeException("failed loading content of '" + resourceName + "'", e);
141         }
142     }
143 }