JoeOLeary | 924ab47 | 2019-02-15 13:46:01 +0000 | [diff] [blame^] | 1 | /*- |
| 2 | * ============LICENSE_START======================================================= |
| 3 | * Copyright (C) 2019 Nordix Foundation. |
| 4 | * ================================================================================ |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | * |
| 17 | * SPDX-License-Identifier: Apache-2.0 |
| 18 | * ============LICENSE_END========================================================= |
| 19 | */ |
| 20 | |
| 21 | package utils; |
| 22 | |
| 23 | import static org.junit.jupiter.api.Assertions.fail; |
| 24 | import static org.mockito.Mockito.RETURNS_DEEP_STUBS; |
| 25 | import static org.mockito.Mockito.mock; |
| 26 | |
| 27 | import com.google.gson.Gson; |
| 28 | import io.undertow.server.HttpServerExchange; |
| 29 | import java.io.IOException; |
| 30 | import java.nio.file.Files; |
| 31 | import java.nio.file.Path; |
| 32 | import java.util.HashMap; |
| 33 | import java.util.List; |
| 34 | import java.util.Map; |
| 35 | import java.util.stream.Collectors; |
| 36 | import java.util.stream.Stream; |
| 37 | |
| 38 | import org.onap.dcaegen2.services.pmmapper.model.Event; |
| 39 | import org.onap.dcaegen2.services.pmmapper.model.EventMetadata; |
| 40 | import org.slf4j.MDC; |
| 41 | |
| 42 | public class EventUtils { |
| 43 | |
| 44 | |
| 45 | /** |
| 46 | * reads contents of files inside the eventBodyDirectory, combines contents with metadata to make an Event Object. |
| 47 | * Fails test in the event of failure to read a file. |
| 48 | * @param eventBodyDirectory Path to directory with event body files. |
| 49 | * @param metadataPath Path to file with metadata object. |
| 50 | * @return List of Events containing the body as acquired from files inside eventBodyDirectory. |
| 51 | * @throws IOException in the event it fails to read from the files. |
| 52 | */ |
| 53 | public static List<Event> eventsFromDirectory(Path eventBodyDirectory, Path metadataPath) throws IOException { |
| 54 | EventMetadata eventMetadata = new Gson().fromJson(fileContentsToString(metadataPath), EventMetadata.class); |
| 55 | try (Stream<Path> eventFileStream = Files.walk(eventBodyDirectory)) { |
| 56 | return eventFileStream.filter(Files::isRegularFile) |
| 57 | .filter(Files::isReadable) |
| 58 | .map(EventUtils::fileContentsToString) |
| 59 | .map(body -> EventUtils.makeMockEvent(body, eventMetadata)) |
| 60 | .collect(Collectors.toList()); |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | /** |
| 65 | * reads contents of file into a string. |
| 66 | * fails a tests in the event failure occurs. |
| 67 | * @param path path to file. |
| 68 | * @return string containing files contents |
| 69 | */ |
| 70 | public static String fileContentsToString(Path path) { |
| 71 | try { |
| 72 | return new String(Files.readAllBytes(path)); |
| 73 | } catch (IOException exception) { |
| 74 | fail("IOException occurred while generating events."); |
| 75 | } |
| 76 | return null; |
| 77 | } |
| 78 | |
| 79 | /** |
| 80 | * @param body body for the event. |
| 81 | * @param eventMetadata metadata for the event. |
| 82 | * @return event with mock HttpServerExchange |
| 83 | */ |
| 84 | public static Event makeMockEvent(String body, EventMetadata eventMetadata) { |
| 85 | return new Event(mock(HttpServerExchange.class, RETURNS_DEEP_STUBS), body, eventMetadata, new HashMap<>()); |
| 86 | } |
| 87 | } |