3 * Copyright (C) 2009-2024 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.alm.client.github;
22 import java.util.Optional;
23 import org.apache.commons.lang.RandomStringUtils;
24 import org.junit.Test;
26 import static java.lang.String.join;
27 import static org.assertj.core.api.Assertions.assertThat;
28 import static org.sonar.alm.client.github.SonarQubeIssueKeyFormatter.SONAR_ISSUE_KEY_PREFIX;
29 import static org.sonar.alm.client.github.SonarQubeIssueKeyFormatter.SONAR_ISSUE_KEY_SUFFIX;
31 public class SonarQubeIssueKeyFormatterTest {
34 public void should_serializeIssueKey() {
35 String issueKey = RandomStringUtils.randomAlphanumeric(20);
37 String serialized = SonarQubeIssueKeyFormatter.serialize(issueKey);
39 String expectedSerializedKey = join("", SONAR_ISSUE_KEY_PREFIX, issueKey, SONAR_ISSUE_KEY_SUFFIX);
40 assertThat(serialized).isEqualTo(expectedSerializedKey);
44 public void should_deserializeIssueKey() {
45 String issueKey = RandomStringUtils.randomAlphanumeric(20);
46 String message = join("", SONAR_ISSUE_KEY_PREFIX, issueKey, SONAR_ISSUE_KEY_SUFFIX, "a message");
48 Optional<String> deserialized = SonarQubeIssueKeyFormatter.deserialize(message);
50 assertThat(deserialized).hasValue(issueKey);
54 public void should_notDeserializeIssueKey_when_messageHasWrongFormat() {
55 String issueKey = RandomStringUtils.randomAlphanumeric(20);
56 String messageWithoutSuffix = join("", SONAR_ISSUE_KEY_PREFIX, issueKey, "a message");
57 String messageWithoutPrefix = join("", issueKey, SONAR_ISSUE_KEY_SUFFIX, "a message");
58 String messageWithPrefixSuffixReversed = join("", SONAR_ISSUE_KEY_SUFFIX, issueKey, SONAR_ISSUE_KEY_PREFIX, "a message");
59 String messageWithNoPrefixSuffix = join("", issueKey, "a message");
61 assertThat(SonarQubeIssueKeyFormatter.deserialize(messageWithoutSuffix)).isEmpty();
62 assertThat(SonarQubeIssueKeyFormatter.deserialize(messageWithoutPrefix)).isEmpty();
63 assertThat(SonarQubeIssueKeyFormatter.deserialize(messageWithPrefixSuffixReversed)).isEmpty();
64 assertThat(SonarQubeIssueKeyFormatter.deserialize(messageWithNoPrefixSuffix)).isEmpty();