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