Skip to content

Commit 3379a0c

Browse files
committed
Rewrite Owner address parsing to Java
1 parent 54e3dc6 commit 3379a0c

File tree

6 files changed

+170
-63
lines changed

6 files changed

+170
-63
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
* Copyright 2016-2024 Qameta Software Inc
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+
package io.qameta.allure.owner;
17+
18+
import lombok.Getter;
19+
20+
@Getter
21+
public class OwnerAddress {
22+
private final String displayName;
23+
private final String url;
24+
25+
public OwnerAddress(final String displayName, final String url) {
26+
this.displayName = displayName;
27+
this.url = url;
28+
}
29+
}
Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
/*
2+
* Copyright 2016-2024 Qameta Software Inc
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+
package io.qameta.allure.owner;
17+
18+
import java.net.MalformedURLException;
19+
import java.util.regex.Matcher;
20+
import java.util.regex.Pattern;
21+
22+
public final class OwnerAddressParser {
23+
private static final Pattern RFC2822_ADDRESS = Pattern.compile("^(.*) <(.*)>$");
24+
private static final Pattern LOOKS_LIKE_EMAIL = Pattern.compile("^[^@]+@[^@]+$");
25+
26+
private OwnerAddressParser() {
27+
}
28+
29+
public static OwnerAddress parseAddress(final String maybeAddress) {
30+
if (maybeAddress == null || maybeAddress.isEmpty()) {
31+
return null;
32+
}
33+
34+
final Matcher matcher = RFC2822_ADDRESS.matcher(maybeAddress);
35+
if (matcher.matches()) {
36+
final String displayName = matcher.group(1);
37+
final String url = toHref(matcher.group(2));
38+
return new OwnerAddress(displayName, url);
39+
}
40+
41+
return new OwnerAddress(maybeAddress, toHref(maybeAddress));
42+
}
43+
44+
private static String toHref(final String address) {
45+
if (isValidURL(address)) {
46+
return address;
47+
}
48+
49+
if (LOOKS_LIKE_EMAIL.matcher(address).matches()) {
50+
return "mailto:" + address;
51+
}
52+
53+
return null;
54+
}
55+
56+
private static boolean isValidURL(final String maybeURL) {
57+
try {
58+
new java.net.URL(maybeURL);
59+
return true;
60+
} catch (MalformedURLException e) {
61+
return false;
62+
}
63+
}
64+
}

allure-generator/src/main/java/io/qameta/allure/owner/OwnerPlugin.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@ public void aggregate(final Configuration configuration,
4444

4545
private void setOwner(final TestResult result) {
4646
result.findOneLabel(LabelName.OWNER)
47-
.ifPresent(owner -> result.addExtraBlock(OWNER_BLOCK_NAME, owner));
47+
.map(OwnerAddressParser::parseAddress)
48+
.ifPresent(ownerAddress ->
49+
result.addExtraBlock(OWNER_BLOCK_NAME, ownerAddress)
50+
);
4851
}
4952
}

allure-generator/src/main/javascript/plugins/testresult-owner/OwnerView.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { View } from "backbone.marionette";
22
import { className } from "../../decorators/index";
3-
import parseAddress from "../../utils/parseAddress";
43
import template from "./OwnerView.hbs";
54

65
@className("pane__section")
@@ -10,7 +9,7 @@ class OwnerView extends View {
109
serializeData() {
1110
const extra = this.model.get("extra");
1211
return {
13-
owner: extra ? parseAddress(extra.owner) : null,
12+
owner: extra ? extra.owner : null,
1413
};
1514
}
1615
}

allure-generator/src/main/javascript/utils/parseAddress.js

Lines changed: 0 additions & 60 deletions
This file was deleted.
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
/*
2+
* Copyright 2016-2024 Qameta Software Inc
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+
package io.qameta.allure.owner;
17+
18+
import org.junit.jupiter.api.Test;
19+
20+
import static org.junit.jupiter.api.Assertions.*;
21+
22+
class OwnerAddressParserTest {
23+
@Test
24+
void shouldReturnNullForNullInput() {
25+
assertNull(OwnerAddressParser.parseAddress(null));
26+
}
27+
28+
@Test
29+
void shouldReturnNullForEmptyInput() {
30+
assertNull(OwnerAddressParser.parseAddress(""));
31+
}
32+
33+
@Test
34+
void shouldParseRFC2822FormattedStringWithEmail() {
35+
String input = "John Doe <john.doe@example.com>";
36+
OwnerAddress expected = new OwnerAddress("John Doe", "mailto:john.doe@example.com");
37+
assertEquals(expected.getDisplayName(), OwnerAddressParser.parseAddress(input).getDisplayName());
38+
assertEquals(expected.getUrl(), OwnerAddressParser.parseAddress(input).getUrl());
39+
}
40+
41+
@Test
42+
void shouldParseRFC2822FormattedStringWithURL() {
43+
String input = "John Doe <https://github.com/john.doe>";
44+
OwnerAddress expected = new OwnerAddress("John Doe", "https://github.com/john.doe");
45+
assertEquals(expected.getDisplayName(), OwnerAddressParser.parseAddress(input).getDisplayName());
46+
assertEquals(expected.getUrl(), OwnerAddressParser.parseAddress(input).getUrl());
47+
}
48+
49+
@Test
50+
void shouldReturnDisplayNameForPlainTextInput() {
51+
String displayName = "John Doe";
52+
OwnerAddress expected = new OwnerAddress(displayName, null);
53+
assertEquals(expected.getDisplayName(), OwnerAddressParser.parseAddress(displayName).getDisplayName());
54+
assertNull(OwnerAddressParser.parseAddress(displayName).getUrl());
55+
}
56+
57+
@Test
58+
void shouldReturnDisplayNameAndUrlForEmailAddress() {
59+
String email = "john.doe@example.com";
60+
OwnerAddress expected = new OwnerAddress(email, "mailto:" + email);
61+
assertEquals(expected.getDisplayName(), OwnerAddressParser.parseAddress(email).getDisplayName());
62+
assertEquals(expected.getUrl(), OwnerAddressParser.parseAddress(email).getUrl());
63+
}
64+
65+
@Test
66+
void shouldReturnDisplayNameAndUrlForValidURL() {
67+
String validUrl = "https://github.com/john.doe";
68+
OwnerAddress expected = new OwnerAddress(validUrl, validUrl);
69+
assertEquals(expected.getDisplayName(), OwnerAddressParser.parseAddress(validUrl).getDisplayName());
70+
assertEquals(expected.getUrl(), OwnerAddressParser.parseAddress(validUrl).getUrl());
71+
}
72+
}

0 commit comments

Comments
 (0)