Add Link Entity
This commit is contained in:
parent
7452d3526b
commit
95afe14a7f
|
@ -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;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,5 @@
|
|||
package link;
|
||||
|
||||
public record LinkId(int id) {
|
||||
|
||||
}
|
|
@ -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();
|
||||
}
|
||||
}
|
|
@ -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
|
||||
}
|
||||
}
|
||||
}
|
|
@ -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());
|
||||
}
|
||||
}
|
Reference in New Issue