Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added test cases for backend module for serialisation #11

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions shielddb-gson-backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,15 @@
<artifactId>gson</artifactId>
<version>2.8.5</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest</artifactId>
<scope>test</scope>
</dependency>
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you rebase your branch on master, these dependencies was already added

</dependencies>
</project>
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package nf.fr.k49.sheilddb.gson;


import com.google.gson.*;
import nf.fr.k49.shielddb.gson.ShieldDBGson;
import static org.junit.jupiter.api.Assertions.*;
import org.junit.jupiter.api.Test;

import java.lang.reflect.Type;
import java.util.Arrays;
import java.util.List;
import java.util.Objects;

public class ShieldDBGsonTest {

static ShieldDBGson<User> shieldDBGson;

@Test
public void testDefaultStorageInstantiation(){
assertDoesNotThrow(()->new ShieldDBGson<User>()
,"must be able to be instantiated with default constructor");
}

@Test
public void testStorageInstantiationWithGson(){
Gson gson = new GsonBuilder()
.registerTypeAdapter(User.class,new UserDeserializer())
.create();
shieldDBGson = new ShieldDBGson<User>(gson);
assertDoesNotThrow(()->shieldDBGson
,"must be able to be instantiated with default constructor");

assertEquals("[{\"name\":\"shieldDB\"}]",shieldDBGson.toJson
(Arrays.asList(new User("shieldDB"))));

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you remove this useless blank line?

}

@Test
public void testStorageInstantiationWithGsonAndType(){
Gson gson = new GsonBuilder()
.registerTypeAdapter(User.class,new UserDeserializer())
.create();
ShieldDBGson shieldDBGson = new ShieldDBGson<User>(gson,User.class);
assertDoesNotThrow(()-> shieldDBGson
,"must be able to be with gson + type constructor");

assertEquals("[{\"name\":\"shieldDB\"}]",shieldDBGson.toJson
(Arrays.asList(new User("shieldDB"))));
}


@Test
public void testToList(){
Gson gson = new GsonBuilder()
.registerTypeAdapter(User.class,new UserDeserializer())
.create();
ShieldDBGson shieldDBGson = new ShieldDBGson<User>(gson);

List<User> user = shieldDBGson.toList("[{\"name\":\"shieldDB\"}]");

assertEquals(1,user.size());

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you remove this useless blank line?

}




Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you remove these useless blank lines?

}

class UserDeserializer implements JsonDeserializer<User>{

@Override
public User deserialize(JsonElement jsonElement, Type type, JsonDeserializationContext jsonDeserializationContext) throws JsonParseException {
JsonObject jsonObject = jsonElement.getAsJsonObject();
String name = jsonObject.get("name").getAsString();
return new User(name);
}
}

class User{

String name;

User(){}

User(String name){
this.name = name;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
User user = (User) o;
return Objects.equals(name, user.name);
}

@Override
public int hashCode() {
return Objects.hash(name);
}
}