Profile: Use Optional for description

This commit is contained in:
Paul Schaub 2023-04-27 14:25:16 +02:00
parent aa88904711
commit c479cc8ef3
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
3 changed files with 171 additions and 7 deletions

View file

@ -38,8 +38,10 @@ public class ListProfilesExternal implements ListProfiles {
private static List<Profile> toProfiles(String output) { private static List<Profile> toProfiles(String output) {
List<Profile> profiles = new ArrayList<>(); List<Profile> profiles = new ArrayList<>();
for (String line : output.split("\n")) { for (String line : output.split("\n")) {
String[] split = line.split(": "); if (line.trim().isEmpty()) {
profiles.add(new Profile(split[0], split[1])); continue;
}
profiles.add(Profile.parse(line));
} }
return profiles; return profiles;
} }

View file

@ -4,8 +4,12 @@
package sop; package sop;
import sop.util.Optional;
import sop.util.UTF8Util; import sop.util.UTF8Util;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
/** /**
* Tuple class bundling a profile name and description. * Tuple class bundling a profile name and description.
* *
@ -15,7 +19,7 @@ import sop.util.UTF8Util;
public class Profile { public class Profile {
private final String name; private final String name;
private final String description; private final Optional<String> description;
/** /**
* Create a new {@link Profile} object. * Create a new {@link Profile} object.
@ -24,15 +28,62 @@ public class Profile {
* @param name profile name * @param name profile name
* @param description profile description * @param description profile description
*/ */
public Profile(String name, String description) { public Profile(@Nonnull String name, @Nullable String description) {
if (name.trim().isEmpty()) {
throw new IllegalArgumentException("Name cannot be empty.");
}
if (name.contains(":")) {
throw new IllegalArgumentException("Name cannot contain ':'.");
}
if (name.contains(" ") || name.contains("\n") || name.contains("\t") || name.contains("\r")) {
throw new IllegalArgumentException("Name cannot contain whitespace characters.");
}
this.name = name; this.name = name;
this.description = description;
if (description == null) {
this.description = Optional.ofEmpty();
} else {
String trimmedDescription = description.trim();
if (trimmedDescription.isEmpty()) {
this.description = Optional.ofEmpty();
} else {
this.description = Optional.of(trimmedDescription);
}
}
if (exceeds1000CharLineLimit(this)) { if (exceeds1000CharLineLimit(this)) {
throw new IllegalArgumentException("The line representation of a profile MUST NOT exceed 1000 bytes."); throw new IllegalArgumentException("The line representation of a profile MUST NOT exceed 1000 bytes.");
} }
} }
public Profile(String name) {
this(name, null);
}
/**
* Parse a {@link Profile} from its string representation.
*
* @param string string representation
* @return profile
*/
public static Profile parse(String string) {
if (string.contains(": ")) {
// description after colon, e.g. "default: Use implementers recommendations."
String name = string.substring(0, string.indexOf(": "));
String description = string.substring(string.indexOf(": ") + 2);
return new Profile(name, description.trim());
}
if (string.endsWith(":")) {
// empty description, e.g. "default:"
return new Profile(string.substring(0, string.length() - 1));
}
// no description
return new Profile(string.trim());
}
/** /**
* Return the name (also known as identifier) of the profile. * Return the name (also known as identifier) of the profile.
* A profile name is a UTF-8 string that has no whitespace in it. * A profile name is a UTF-8 string that has no whitespace in it.
@ -48,6 +99,7 @@ public class Profile {
* *
* @return name * @return name
*/ */
@Nonnull
public String getName() { public String getName() {
return name; return name;
} }
@ -57,17 +109,26 @@ public class Profile {
* *
* @return description * @return description
*/ */
public String getDescription() { @Nonnull
public Optional<String> getDescription() {
return description; return description;
} }
public boolean hasDescription() {
return description.isPresent();
}
/** /**
* Convert the profile into a String for displaying. * Convert the profile into a String for displaying.
* *
* @return string * @return string
*/ */
@Override
public String toString() { public String toString() {
return getName() + ": " + getDescription(); if (getDescription().isEmpty()) {
return getName();
}
return getName() + ": " + getDescription().get();
} }
/** /**

View file

@ -0,0 +1,101 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop;
import org.junit.jupiter.api.Test;
import sop.Profile;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class ProfileTest {
@Test
public void toStringFull() {
Profile profile = new Profile("default", "Use the implementers recommendations.");
assertEquals("default: Use the implementers recommendations.", profile.toString());
}
@Test
public void toStringNameOnly() {
Profile profile = new Profile("default");
assertEquals("default", profile.toString());
}
@Test
public void parseFull() {
String string = "default: Use the implementers recommendations.";
Profile profile = Profile.parse(string);
assertEquals("default", profile.getName());
assertTrue(profile.hasDescription());
assertEquals("Use the implementers recommendations.", profile.getDescription().get());
}
@Test
public void parseNameOnly() {
String string = "rfc4880";
Profile profile = Profile.parse(string);
assertEquals("rfc4880", profile.getName());
assertFalse(profile.hasDescription());
}
@Test
public void parseEmptyDescription() {
String string = "rfc4880: ";
Profile profile = Profile.parse(string);
assertEquals("rfc4880", profile.getName());
assertFalse(profile.hasDescription());
string = "rfc4880:";
profile = Profile.parse(string);
assertEquals("rfc4880", profile.getName());
assertFalse(profile.hasDescription());
}
@Test
public void parseTooLongProfile() {
// 1200 chars
String string = "longDescription: Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.";
assertThrows(IllegalArgumentException.class, () -> Profile.parse(string));
}
@Test
public void constructTooLongProfile() {
// name + description = 1200 chars
String name = "longDescription";
String description = "Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet. Lorem ipsum dolor sit amet, consetetur sadipscing elitr, sed diam nonumy eirmod tempor invidunt ut labore et dolore magna aliquyam erat, sed diam voluptua. At vero eos et accusam et justo duo dolores et ea rebum. Stet clita kasd gubergren, no sea takimata sanctus est Lorem ipsum dolor sit amet.";
assertThrows(IllegalArgumentException.class, () -> new Profile(name, description));
}
@Test
public void nameCannotBeEmpty() {
assertThrows(IllegalArgumentException.class, () -> new Profile(""));
assertThrows(IllegalArgumentException.class, () -> new Profile(""), "Description Text.");
}
@Test
public void nameCannotContainColons() {
assertThrows(IllegalArgumentException.class, () -> new Profile("default:"));
assertThrows(IllegalArgumentException.class, () -> new Profile("default:", "DescriptionText"));
assertThrows(IllegalArgumentException.class, () -> new Profile("rfc:4880"));
assertThrows(IllegalArgumentException.class, () -> new Profile("rfc:4880", "OpenPGP Message Format"));
}
@Test
public void nameCannotContainWhitespace() {
assertThrows(IllegalArgumentException.class, () -> new Profile("default profile"));
assertThrows(IllegalArgumentException.class, () -> new Profile("default profile", "With description."));
assertThrows(IllegalArgumentException.class, () -> new Profile("default\nprofile"));
assertThrows(IllegalArgumentException.class, () -> new Profile("default\nprofile", "With description"));
assertThrows(IllegalArgumentException.class, () -> new Profile("default\tprofile"));
assertThrows(IllegalArgumentException.class, () -> new Profile("default\tprofile", "With description"));
assertThrows(IllegalArgumentException.class, () -> new Profile("default\r\nprofile"));
assertThrows(IllegalArgumentException.class, () -> new Profile("default\r\nprofile", "With description"));
assertThrows(IllegalArgumentException.class, () -> new Profile("default\rprofile"));
assertThrows(IllegalArgumentException.class, () -> new Profile("default\rprofile", "With description"));
}
}