|
| 1 | +/* |
| 2 | + * Copyright 2024 EPAM Systems |
| 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 | + |
| 17 | +package com.epam.reportportal.extension.slack.utils; |
| 18 | + |
| 19 | +import static com.epam.reportportal.extension.slack.event.launch.SlackLaunchFinishEventListener.PLUGIN_NOTIFICATION_TYPE; |
| 20 | +import static com.epam.reportportal.extension.slack.event.launch.SlackLaunchFinishEventListener.SLACK_NOTIFICATION_ATTRIBUTE; |
| 21 | +import static com.epam.reportportal.extension.slack.event.launch.SlackLaunchFinishEventListener.WEBHOOK_DETAILS; |
| 22 | +import static com.epam.ta.reportportal.entity.enums.ProjectAttributeEnum.NOTIFICATIONS_ENABLED; |
| 23 | + |
| 24 | +import com.epam.ta.reportportal.entity.attribute.Attribute; |
| 25 | +import com.epam.ta.reportportal.entity.enums.SendCase; |
| 26 | +import com.epam.ta.reportportal.entity.launch.Launch; |
| 27 | +import com.epam.ta.reportportal.entity.project.Project; |
| 28 | +import com.epam.ta.reportportal.entity.project.ProjectAttribute; |
| 29 | +import com.epam.ta.reportportal.entity.project.email.SenderCase; |
| 30 | +import com.epam.ta.reportportal.entity.project.email.SenderCaseOptions; |
| 31 | +import java.io.IOException; |
| 32 | +import java.io.InputStream; |
| 33 | +import java.nio.charset.StandardCharsets; |
| 34 | +import java.nio.file.Files; |
| 35 | +import java.nio.file.Paths; |
| 36 | +import java.util.Collections; |
| 37 | +import java.util.Map; |
| 38 | +import java.util.Set; |
| 39 | +import java.util.stream.Stream; |
| 40 | +import org.apache.commons.io.IOUtils; |
| 41 | + |
| 42 | +public class MockData { |
| 43 | + |
| 44 | + private static final String LAUNCH_NAME_1 = "Launch name 1"; |
| 45 | + |
| 46 | + public static Project getProjectSample() { |
| 47 | + var project = new Project(); |
| 48 | + |
| 49 | + project.setId(1L); |
| 50 | + project.setSenderCases(Collections.singleton(getSenderCase())); |
| 51 | + project.setProjectAttributes(getProjectAttributes()); |
| 52 | + return project; |
| 53 | + } |
| 54 | + |
| 55 | + private static Set<ProjectAttribute> getProjectAttributes() { |
| 56 | + var attribute1 = new Attribute(); |
| 57 | + attribute1.setId(13L); |
| 58 | + attribute1.setName(NOTIFICATIONS_ENABLED.getAttribute()); |
| 59 | + |
| 60 | + var attribute2 = new Attribute(); |
| 61 | + attribute2.setId(21L); |
| 62 | + attribute2.setName(SLACK_NOTIFICATION_ATTRIBUTE); |
| 63 | + |
| 64 | + ProjectAttribute projectAttribute1 = new ProjectAttribute() |
| 65 | + .withAttribute(attribute1) |
| 66 | + .withProject(new Project()) |
| 67 | + .withValue("true"); |
| 68 | + |
| 69 | + ProjectAttribute projectAttribute2 = new ProjectAttribute() |
| 70 | + .withAttribute(attribute2) |
| 71 | + .withProject(new Project()) |
| 72 | + .withValue("true"); |
| 73 | + |
| 74 | + return Set.of(projectAttribute1, projectAttribute2); |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + |
| 79 | + public static SenderCase getSenderCase() { |
| 80 | + var senderCase = new SenderCase(); |
| 81 | + senderCase.setEnabled(true); |
| 82 | + senderCase.setType(PLUGIN_NOTIFICATION_TYPE); |
| 83 | + senderCase.setSendCase(SendCase.ALWAYS); |
| 84 | + senderCase.setLaunchNames(Set.of(LAUNCH_NAME_1)); |
| 85 | + senderCase.setRuleDetails(getSenderCaseOptions()); |
| 86 | + return senderCase; |
| 87 | + } |
| 88 | + |
| 89 | + public static Launch getLaunch() { |
| 90 | + var launch = new Launch(); |
| 91 | + launch.setId(20L); |
| 92 | + launch.setName(LAUNCH_NAME_1); |
| 93 | + return launch; |
| 94 | + } |
| 95 | + |
| 96 | + public static SenderCaseOptions getSenderCaseOptions() { |
| 97 | + SenderCaseOptions senderCaseOptions = new SenderCaseOptions(); |
| 98 | + senderCaseOptions.setOptions( |
| 99 | + Map.of(WEBHOOK_DETAILS, |
| 100 | + "https://hooks.slack.com/services/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")); |
| 101 | + return senderCaseOptions; |
| 102 | + } |
| 103 | + |
| 104 | + public static String readFileToString(String path) throws IOException { |
| 105 | + |
| 106 | + try (InputStream resourceAsStream = MockData.class.getClassLoader().getResourceAsStream(path)) { |
| 107 | + if (resourceAsStream != null) { |
| 108 | + return IOUtils.toString(resourceAsStream, StandardCharsets.UTF_8); |
| 109 | + } else { |
| 110 | + StringBuilder sb = new StringBuilder(); |
| 111 | + try (Stream<String> lines = Files.lines(Paths.get(path))) { |
| 112 | + lines.forEach(sb::append); |
| 113 | + } |
| 114 | + return sb.toString(); |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
0 commit comments