start test for csv dao
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
parent
f558de5fa9
commit
f616b4b4bf
|
@ -24,6 +24,18 @@
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<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>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
|
79
0-Plugin/src/test/java/persistence/GenericCSVDAOTest.java
Normal file
79
0-Plugin/src/test/java/persistence/GenericCSVDAOTest.java
Normal 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());
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,12 @@
|
||||||
<version>1.0-SNAPSHOT</version>
|
<version>1.0-SNAPSHOT</version>
|
||||||
<scope>compile</scope>
|
<scope>compile</scope>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.junit.jupiter</groupId>
|
||||||
|
<artifactId>junit-jupiter</artifactId>
|
||||||
|
<version>RELEASE</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<properties>
|
<properties>
|
||||||
|
|
Reference in a new issue