Category Entity and VO
continuous-integration/drone/push Build is passing Details

This commit is contained in:
qvalentin 2022-03-06 10:53:36 +01:00
parent d7bc76c5fa
commit d5e1ec0571
7 changed files with 182 additions and 0 deletions

View File

@ -0,0 +1,25 @@
package Category;
/**
* A Category that can be assigned to a link to group links with similar content.
* Categories need a unique name, by which they can be identified for the user.
* They can be created by the user.
*/
public class Category {
private CategoryName name;
private CategoryId id;
public Category(CategoryName name, CategoryId id) {
this.name = name;
this.id = id;
}
public CategoryName getName() {
return name;
}
public CategoryId getId() {
return id;
}
}

View File

@ -0,0 +1,4 @@
package Category;
public record CategoryId(int id) {
}

View File

@ -0,0 +1,43 @@
package Category;
import Exeptions.IllegalValueObjectArgument;
import java.util.Objects;
public class CategoryName {
private final String name;
public String getName() {
return name;
}
public CategoryName(final String name) throws IllegalValueObjectArgument {
validateName(name);
this.name = name;
}
private void validateName(final String name) throws IllegalValueObjectArgument {
if (name == null) {
throw new IllegalValueObjectArgument("A Category name can not be null.");
}
if (name.isBlank() || name.isEmpty() || name.length() < 3) {
throw new IllegalValueObjectArgument("A Category name must be a valid string of at least 3 characters.");
}
}
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CategoryName that = (CategoryName) o;
return Objects.equals(name, that.name);
}
@Override
public int hashCode() {
return name != null ? name.hashCode() : 0;
}
}

View File

@ -0,0 +1,8 @@
package Exeptions;
public class IllegalValueObjectArgument extends Exception {
public IllegalValueObjectArgument(String message) {
super(message);
}
}

View File

@ -0,0 +1,26 @@
package Category;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
class CategoryIdTest {
@Test
void constructorWorks() {
int id = 432;
CategoryId sut = new CategoryId(id);
assertEquals(id, sut.id());
}
@Test
void equalsWorks() {
int id = 432;
CategoryId first = new CategoryId(id);
CategoryId second = new CategoryId(id);
assertEquals(first, second);
}
}

View File

@ -0,0 +1,47 @@
package Category;
import Exeptions.IllegalValueObjectArgument;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
class CategoryNameTest {
@Test
void getNameWorks() throws IllegalValueObjectArgument {
String name = "libaries";
CategoryName sut = new CategoryName(name);
assertEquals(name, sut.getName());
}
@Test
void constructorThrowsNull() {
String nullString = null;
assertThrows(IllegalValueObjectArgument.class, () -> new CategoryName(nullString));
}
@Test
void constructorThrowsBlank() {
String blankString = " ";
assertThrows(IllegalValueObjectArgument.class, () -> new CategoryName(blankString));
}
@Test
void constructorThrowsEmpty() {
String emptyString = "";
assertThrows(IllegalValueObjectArgument.class, () -> new CategoryName(emptyString));
}
@Test
void constructorThrowsTooShort() {
String shortString = "da";
assertThrows(IllegalValueObjectArgument.class, () -> new CategoryName(shortString));
}
@Test
void testEquals() {
}
}

View File

@ -0,0 +1,29 @@
package Category;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.Mockito.mock;
class CategoryTest {
@Test
void getNameWorks() {
CategoryId id = new CategoryId(32);
CategoryName name = mock(CategoryName.class);
Category sut = new Category(name, id);
assertEquals(name, sut.getName());
}
@Test
void getIdWorks() {
CategoryId id = new CategoryId(32);
CategoryName name = mock(CategoryName.class);
Category sut = new Category(name, id);
assertEquals(id, sut.getId());
}
}