Skip to content

Commit 6712470

Browse files
authored
Merge pull request #33 from snwiem/feature/remove_api
adds remove method to JsonDBTemplate without the EntityClass parameter
2 parents ba1c569 + 993fa1c commit 6712470

File tree

3 files changed

+39
-1
lines changed

3 files changed

+39
-1
lines changed

src/main/java/io/jsondb/JsonDBOperations.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -556,6 +556,16 @@ public interface JsonDBOperations {
556556
*/
557557
<T> void save(Object objectToSave, String collectionName);
558558

559+
/**
560+
* Remove the given object from the collection by id.
561+
*
562+
* @param objectToRemove the object to remove from the collection
563+
* @param <T> Type annotated with {@link io.jsondb.annotation.Document} annotation
564+
* and member of the baseScanPackage
565+
* @return the object that was actually removed or null
566+
*/
567+
<T> T remove(Object objectToRemove);
568+
559569
/**
560570
* Remove the given object from the collection by id.
561571
*

src/main/java/io/jsondb/JsonDBTemplate.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1001,6 +1001,14 @@ public <T> void save(Object objectToSave, String collectionName) {
10011001
}
10021002
}
10031003

1004+
/* (non-Javadoc)
1005+
* @see org.jsondb.JsonDBOperations#remove(java.lang.Object)
1006+
*/
1007+
@Override
1008+
public <T> T remove(Object objectToRemove) {
1009+
return remove(objectToRemove, Util.determineEntityCollectionName(objectToRemove));
1010+
}
1011+
10041012
/* (non-Javadoc)
10051013
* @see org.jsondb.JsonDBOperations#remove(java.lang.Object, java.lang.Class)
10061014
*/

src/test/java/io/jsondb/tests/RemoveTests.java

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ public void testRemoveBatch_FromNonExistingCollection() {
142142
* Test to remove a single object from a collection
143143
*/
144144
@Test
145-
public void testRemove_ValidObject() {
145+
public void testRemove_ValidObjectWithClass() {
146146
List<Instance> instances = jsonDBTemplate.getCollection(Instance.class);
147147
int size = instances.size();
148148

@@ -158,6 +158,26 @@ public void testRemove_ValidObject() {
158158
assertEquals("05", removedObject.getId());
159159
}
160160

161+
/**
162+
* Test to remove a single object from a collection
163+
*/
164+
@Test
165+
public void testRemove_ValidObjectWithoutClass() {
166+
List<Instance> instances = jsonDBTemplate.getCollection(Instance.class);
167+
int size = instances.size();
168+
169+
Instance instance = new Instance();
170+
instance.setId("05");
171+
172+
Instance removedObject = jsonDBTemplate.remove(instance);
173+
174+
instances = jsonDBTemplate.getCollection(Instance.class);
175+
assertNotNull(instances);
176+
assertEquals(size-1, instances.size());
177+
assertNotNull(removedObject);
178+
assertEquals("05", removedObject.getId());
179+
}
180+
161181
/**
162182
* Test to remove a batch of objects from collection
163183
*/

0 commit comments

Comments
 (0)