start test for csv dao
continuous-integration/drone/push Build is passing Details

This commit is contained in:
qvalentin 2022-05-13 19:13:02 +02:00
parent f558de5fa9
commit f616b4b4bf
4 changed files with 114 additions and 2 deletions

View File

@ -24,7 +24,19 @@
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
</dependencies> <dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties> <properties>
<maven.compiler.source>17</maven.compiler.source> <maven.compiler.source>17</maven.compiler.source>

View File

@ -0,0 +1,79 @@
package persistence;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import persistence.category.CategoryEntity;
import java.io.File;
import java.io.IOException;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.*;
class GenericCSVDAOTest {
GenericCSVDAO<CategoryEntity> sut;
public void beforeEachDirty() throws IOException {
File file = new File("test.csv");
if (file.exists()){
file.delete();
}
file.createNewFile();
this.sut = new GenericCSVDAO<CategoryEntity>(file,CategoryEntity::new);
}
@Test
public void addWorks() throws IOException {
beforeEachDirty();
assertEquals(0, sut.getALl().size());
var entityToAdd = new CategoryEntity("categoryName",99);
sut.add(entityToAdd);
var foundEntity = sut.getALl().stream().findFirst().orElseThrow();
assertEquals(entityToAdd,foundEntity);
}
@Test
public void removeWorks() throws IOException {
beforeEachDirty();
var entityToAdd = new CategoryEntity("categoryName",99);
sut.add(entityToAdd);
sut.remove(entityToAdd);
assertEquals(0, sut.getALl().size());
}
@Test
public void removeAllWorks() throws IOException {
beforeEachDirty();
var entityToAdd1 = new CategoryEntity("categoryName1",101);
sut.add(entityToAdd1);
var entityToAdd2 = new CategoryEntity("categoryName2",102);
sut.add(entityToAdd2);
assertEquals(2, sut.getALl().size());
sut.removeAll();
assertEquals(0, sut.getALl().size());
}
}

View File

@ -5,6 +5,8 @@ import category.CategoryId;
import category.CategoryName; import category.CategoryName;
import persistence.csv.CSVSerializable; import persistence.csv.CSVSerializable;
import java.util.Objects;
public class CategoryEntity implements CSVSerializable { public class CategoryEntity implements CSVSerializable {
private final String name; private final String name;
@ -36,4 +38,17 @@ public class CategoryEntity implements CSVSerializable {
public String toCSVString() { public String toCSVString() {
return name + CSVSerializable.seperator + Integer.toString(id); return name + CSVSerializable.seperator + Integer.toString(id);
} }
@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
CategoryEntity that = (CategoryEntity) o;
return id == that.id && Objects.equals(name, that.name);
}
@Override
public int hashCode() {
return Objects.hash(name, id);
}
} }

View File

@ -19,7 +19,13 @@
<version>1.0-SNAPSHOT</version> <version>1.0-SNAPSHOT</version>
<scope>compile</scope> <scope>compile</scope>
</dependency> </dependency>
</dependencies> <dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties> <properties>
<maven.compiler.source>17</maven.compiler.source> <maven.compiler.source>17</maven.compiler.source>