-
Notifications
You must be signed in to change notification settings - Fork 9
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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")))); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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()); | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you remove this useless blank line? |
||
} | ||
|
||
|
||
|
||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
} | ||
} |
There was a problem hiding this comment.
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