Add Link Entity

This commit is contained in:
qvalentin 2022-03-06 15:55:37 +01:00
parent 7452d3526b
commit 95afe14a7f
5 changed files with 112 additions and 0 deletions

View File

@ -0,0 +1,24 @@
package link;
import category.CategoryId;
import tag.TagMatcher;
import user.Username;
import java.util.Set;
public class Link {
private final LinkId id;
private final Username creator;
private final LinkUrl url;
private final Set<CategoryId> categoryIds;
private final Set<TagMatcher> tags;
public Link(LinkId id, Username creator, LinkUrl url, Set<CategoryId> categoryIds, Set<TagMatcher> tags) {
this.id = id;
this.creator = creator;
this.url = url;
this.categoryIds = categoryIds;
this.tags = tags;
}
}

View File

@ -0,0 +1,5 @@
package link;
public record LinkId(int id) {
}

View File

@ -0,0 +1,29 @@
package link;
import exeptions.IllegalValueObjectArgument;
import java.net.MalformedURLException;
import java.net.URL;
public class LinkUrl {
private final URL url;
public LinkUrl(String urlString) throws IllegalValueObjectArgument {
try {
this.url = new URL(urlString);
}
catch (MalformedURLException e) {
throw new IllegalValueObjectArgument(e.getMessage());
}
}
public URL getUrl() {
return url;
}
@Override
public String toString() {
return url.toString();
}
}

View File

@ -0,0 +1,25 @@
package link;
import java.io.IOException;
import java.net.HttpURLConnection;
public class OnlineCheck {
static Boolean of(LinkUrl url) {
int responseCode = getResponseCode(url);
return (responseCode < 400 || responseCode == 401 || responseCode == 403);
}
private static int getResponseCode(LinkUrl url) {
try {
HttpURLConnection http = (HttpURLConnection) url.getUrl().openConnection();
http.setRequestMethod("HEAD");
http.disconnect();
return http.getResponseCode();
}
catch (IOException e) {
return 500; //TODO: seems smelly
}
}
}

View File

@ -0,0 +1,29 @@
package link;
import exeptions.IllegalValueObjectArgument;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
class LinkUrlTest {
@Test
void constructorWorks() {
assertDoesNotThrow(() -> new LinkUrl("https://tea.filefighter.de/qvalentin/LinkDitch"));
}
@Test
void constructorThrows() {
assertThrows(IllegalValueObjectArgument.class, () -> new LinkUrl("tea.filefighter.de/qvalentin/LinkDitch"));
}
@Test
void testToString() throws IllegalValueObjectArgument {
String urlString = "https://tea.filefighter.de/qvalentin/LinkDitch";
LinkUrl sut = new LinkUrl(urlString);
assertEquals(urlString, sut.toString());
}
}