Skip to content

Commit

Permalink
Rewrite Owner address parsing to Java
Browse files Browse the repository at this point in the history
  • Loading branch information
noomorph committed Apr 1, 2024
1 parent caca2f1 commit 4c28665
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 63 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* Copyright 2016-2024 Qameta Software Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.qameta.allure.owner;

import lombok.Getter;

@Getter
public class OwnerAddress {
private final String displayName;
private final String url;

public OwnerAddress(final String displayName, final String url) {
this.displayName = displayName;
this.url = url;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright 2016-2024 Qameta Software Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.qameta.allure.owner;

import java.net.MalformedURLException;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public final class OwnerAddressParser {
private static final Pattern RFC2822_ADDRESS = Pattern.compile("^(.*) <(.*)>$");
private static final Pattern LOOKS_LIKE_EMAIL = Pattern.compile("^[^@]+@[^@]+$");

private OwnerAddressParser() {
}

public static OwnerAddress parseAddress(final String maybeAddress) {
if (maybeAddress == null || maybeAddress.isEmpty()) {
return null;
}

final Matcher matcher = RFC2822_ADDRESS.matcher(maybeAddress);
if (matcher.matches()) {
final String displayName = matcher.group(1);
final String url = toHref(matcher.group(2));
return new OwnerAddress(displayName, url);
}

return new OwnerAddress(maybeAddress, toHref(maybeAddress));
}

private static String toHref(final String address) {
if (isValidURL(address)) {
return address;
}

if (LOOKS_LIKE_EMAIL.matcher(address).matches()) {
return "mailto:" + address;
}

return null;
}

private static boolean isValidURL(final String maybeURL) {
try {
new java.net.URL(maybeURL);
return true;
} catch (MalformedURLException e) {
return false;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,9 @@ public void aggregate(final Configuration configuration,

private void setOwner(final TestResult result) {
result.findOneLabel(LabelName.OWNER)
.ifPresent(owner -> result.addExtraBlock(OWNER_BLOCK_NAME, owner));
.map(OwnerAddressParser::parseAddress)
.ifPresent(ownerAddress ->
result.addExtraBlock(OWNER_BLOCK_NAME, ownerAddress)
);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { View } from "backbone.marionette";
import { className } from "../../decorators/index";
import parseAddress from "../../utils/parseAddress";
import template from "./OwnerView.hbs";

@className("pane__section")
Expand All @@ -10,7 +9,7 @@ class OwnerView extends View {
serializeData() {
const extra = this.model.get("extra");
return {
owner: extra ? parseAddress(extra.owner) : null,
owner: extra ? extra.owner : null,
};
}
}
Expand Down
60 changes: 0 additions & 60 deletions allure-generator/src/main/javascript/utils/parseAddress.js

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
/*
* Copyright 2016-2024 Qameta Software Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.qameta.allure.owner;

import org.junit.jupiter.api.Test;

import static org.junit.jupiter.api.Assertions.*;

class OwnerAddressParserTest {
@Test
void shouldReturnNullForNullInput() {
assertNull(OwnerAddressParser.parseAddress(null));
}

@Test
void shouldReturnNullForEmptyInput() {
assertNull(OwnerAddressParser.parseAddress(""));
}

@Test
void shouldParseRFC2822FormattedStringWithEmail() {
String input = "John Doe <[email protected]>";
OwnerAddress expected = new OwnerAddress("John Doe", "mailto:[email protected]");
assertEquals(expected.getDisplayName(), OwnerAddressParser.parseAddress(input).getDisplayName());
assertEquals(expected.getUrl(), OwnerAddressParser.parseAddress(input).getUrl());
}

@Test
void shouldParseRFC2822FormattedStringWithURL() {
String input = "John Doe <https://github.com/john.doe>";
OwnerAddress expected = new OwnerAddress("John Doe", "https://github.com/john.doe");
assertEquals(expected.getDisplayName(), OwnerAddressParser.parseAddress(input).getDisplayName());
assertEquals(expected.getUrl(), OwnerAddressParser.parseAddress(input).getUrl());
}

@Test
void shouldReturnDisplayNameForPlainTextInput() {
String displayName = "John Doe";
OwnerAddress expected = new OwnerAddress(displayName, null);
assertEquals(expected.getDisplayName(), OwnerAddressParser.parseAddress(displayName).getDisplayName());
assertNull(OwnerAddressParser.parseAddress(displayName).getUrl());
}

@Test
void shouldReturnDisplayNameAndUrlForEmailAddress() {
String email = "[email protected]";
OwnerAddress expected = new OwnerAddress(email, "mailto:" + email);
assertEquals(expected.getDisplayName(), OwnerAddressParser.parseAddress(email).getDisplayName());
assertEquals(expected.getUrl(), OwnerAddressParser.parseAddress(email).getUrl());
}

@Test
void shouldReturnDisplayNameAndUrlForValidURL() {
String validUrl = "https://github.com/john.doe";
OwnerAddress expected = new OwnerAddress(validUrl, validUrl);
assertEquals(expected.getDisplayName(), OwnerAddressParser.parseAddress(validUrl).getDisplayName());
assertEquals(expected.getUrl(), OwnerAddressParser.parseAddress(validUrl).getUrl());
}
}

0 comments on commit 4c28665

Please sign in to comment.