f5033ca29beefb6c7503de78789a3b775614667c
[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 org.springframework.test.context.ActiveProfiles;
43 import static com.github.tomakehurst.wiremock.client.WireMock.urlEqualTo;
44 import static com.github.tomakehurst.wiremock.client.WireMock.aResponse;
45 import static com.github.tomakehurst.wiremock.client.WireMock.urlPathMatching;
46 import static com.github.tomakehurst.wiremock.client.WireMock.ok;
47 import static com.github.tomakehurst.wiremock.client.WireMock.equalTo;
48 import static com.github.tomakehurst.wiremock.client.WireMock.get;
49 import static com.github.tomakehurst.wiremock.client.WireMock.post;
50 import static com.github.tomakehurst.wiremock.client.WireMock.anyRequestedFor;
51 import static com.github.tomakehurst.wiremock.client.WireMock.postRequestedFor;
52 import static com.github.tomakehurst.wiremock.client.WireMock.matchingJsonPath;
53 import static com.github.tomakehurst.wiremock.client.WireMock.stubFor;
54 import static com.github.tomakehurst.wiremock.client.WireMock.verify;
55 import static com.github.tomakehurst.wiremock.client.WireMock.patch;
56 import java.nio.file.Files;
57 import java.nio.file.Paths;
58 import static java.lang.ClassLoader.getSystemResource;
59 import static java.util.Collections.singletonList;
60
61
62 @SpringBootTest
63 @AutoConfigureWireMock(port = 0)
64 @ActiveProfiles(value = "prod")
65 class PrhWorkflowIntegrationTest {
66
67     @Autowired
68     private ScheduledTasks scheduledTasks;
69
70     @MockBean
71     private ScheduledTasksRunner scheduledTasksRunner;  // just to disable scheduling - some configurability in ScheduledTaskRunner not to start tasks at app startup would be welcome
72
73
74     @Configuration
75     @Import(MainApp.class)
76     static class CbsConfigTestConfig {
77
78         @Value("http://localhost:${wiremock.server.port}")
79         private String wiremockServerAddress;
80
81         @Bean
82         public CbsConfiguration cbsConfiguration() {
83             JsonObject cbsConfigJson = new Gson().fromJson(getResourceContent("configurationFromCbs.json")
84                             .replaceAll("https?://dmaap-mr[\\w.]*:\\d+", wiremockServerAddress)
85                             .replaceAll("https?://aai[\\w.]*:\\d+", wiremockServerAddress),
86                     JsonObject.class);
87
88             CbsConfiguration cbsConfiguration = new CbsConfiguration();
89             cbsConfiguration.parseCBSConfig(cbsConfigJson);
90             return cbsConfiguration;
91         }
92         
93
94     }
95
96     @BeforeEach
97     void resetWireMock() {
98         WireMock.reset();
99     }
100
101
102     @Test
103     void whenThereAreNoEventsInDmaap_WorkflowShouldFinish() {
104         stubFor(get(urlEqualTo("/events/unauthenticated.VES_PNFREG_OUTPUT/OpenDCAE-c12/c12"))
105                 .willReturn(aResponse().withBody("[]")));
106
107         scheduledTasks.scheduleMainPrhEventTask();
108
109         verify(0, anyRequestedFor(urlPathMatching("/aai.*")));
110         verify(0, postRequestedFor(urlPathMatching("/events.*")));
111     }
112
113
114     @Test
115     void whenThereIsAnEventsInDmaap_ShouldSendPnfReadyNotification() {
116         String event = getResourceContent("integration/event.json");
117         String pnfName = JsonPath.read(event, "$.event.commonEventHeader.sourceName");
118
119         stubFor(get(urlEqualTo("/events/unauthenticated.VES_PNFREG_OUTPUT/OpenDCAE-c12/c12"))
120                 .willReturn(ok().withBody(new Gson().toJson(singletonList(event)))));
121         stubFor(get(urlEqualTo("/aai/v23/network/pnfs/pnf/" + pnfName)).willReturn(ok().withBody("{}")));
122         stubFor(patch(urlEqualTo("/aai/v23/network/pnfs/pnf/" + pnfName)));
123         stubFor(post(urlEqualTo("/events/unauthenticated.PNF_READY")));
124
125         scheduledTasks.scheduleMainPrhEventTask();
126
127         verify(1, postRequestedFor(urlEqualTo("/events/unauthenticated.PNF_READY"))
128                 .withRequestBody(matchingJsonPath("$[0].correlationId", equalTo(pnfName))));
129     }
130
131
132     private static String getResourceContent(String resourceName) {
133         try {
134             return new String(Files.readAllBytes(Paths.get(getSystemResource(resourceName).toURI())));
135         } catch (Exception e) {
136             throw new RuntimeException("failed loading content of '" + resourceName + "'", e);
137         }
138     }
139 }