Skip to content

Commit

Permalink
Merge pull request #555 from kdhrubo/feature/548_mongodb_bulk_insert
Browse files Browse the repository at this point in the history
#548 Mongodb bulk insert
  • Loading branch information
kdhrubo authored May 10, 2024
2 parents 529ae8b + e281f54 commit 5c9141e
Show file tree
Hide file tree
Showing 5 changed files with 183 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import com.homihq.db2rest.config.Db2RestConfigProperties;
import com.homihq.db2rest.core.dto.CountResponse;
import com.homihq.db2rest.core.dto.CreateBulkResponse;
import com.homihq.db2rest.core.dto.CreateResponse;
import com.homihq.db2rest.core.dto.DeleteResponse;
import com.homihq.db2rest.core.dto.ExistsResponse;
Expand Down Expand Up @@ -42,6 +43,13 @@ public CreateResponse save(String collectionName,

}

@Override
public CreateBulkResponse saveAll(String collectionName,
List<String> includeFields,
List<Map<String, Object>> dataList) {
return mongoRepository.saveAll(collectionName, includeFields, dataList);
}

@Override
public UpdateResponse patch(String collectionName, Map<String, Object> data, String filter) {
log.debug("Filter - {}", filter);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.homihq.db2rest.mongo.rest.api;

import com.homihq.db2rest.core.dto.CountResponse;
import com.homihq.db2rest.core.dto.CreateBulkResponse;
import com.homihq.db2rest.core.dto.CreateResponse;
import com.homihq.db2rest.core.dto.DeleteResponse;
import com.homihq.db2rest.core.dto.ExistsResponse;
Expand All @@ -25,6 +26,12 @@ CreateResponse save(@PathVariable String collectionName,
@RequestParam(name = "fields", required = false) List<String> includeFields,
@RequestBody Map<String, Object> data);

@ResponseStatus(HttpStatus.CREATED)
@PostMapping("/{collectionName}/bulk")
CreateBulkResponse saveAll(@PathVariable String collectionName,
@RequestParam(name = "fields", required = false) List<String> includeFields,
@RequestBody List<Map<String, Object>> dataList);

@ResponseStatus(HttpStatus.OK)
@PatchMapping("/{collectionName}")
UpdateResponse patch(@PathVariable String collectionName,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,16 @@
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;

import java.util.List;
import java.util.Map;

import static org.hamcrest.Matchers.allOf;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.hasToString;
import static org.hamcrest.Matchers.notNullValue;
import static org.springframework.data.mongodb.core.query.Query.query;
import static org.springframework.http.MediaType.APPLICATION_JSON;
import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document;
Expand Down Expand Up @@ -67,6 +71,9 @@ class MongoDBControllerTest extends MongodbContainerConfiguration {
@GivenJsonResource("/testdata/CREATE_ACTOR_REQUEST.json")
Map<String, Object> CREATE_ACTOR_REQUEST;

@GivenJsonResource("/testdata/BULK_CREATE_ACTOR_REQUEST.json")
List<Map<String, Object>> BULK_CREATE_ACTOR_REQUEST;

@GivenJsonResource("/testdata/UPDATE_ACTOR_REQUEST.json")
Map<String, Object> UPDATE_ACTOR_REQUEST;

Expand Down Expand Up @@ -103,6 +110,86 @@ void create() throws Exception {

@Test
@Order(2)
@DisplayName("Create an actor with with include fields")
void createWithIncludeFields() throws Exception {
mockMvc.perform(post("/Sakila_actors")
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.param("fields", "FirstName, LastName")
.content(objectMapper.writeValueAsString(CREATE_ACTOR_REQUEST))
)
.andExpect(status().isCreated())
.andExpect(jsonPath("$.row", equalTo(1)))
.andExpect(jsonPath("$.keys.timestamp").exists())
.andDo(document("mongodb-create-an-actor-with-include-fields"));

mongoTemplate.remove(query(Criteria
.where("FirstName")
.is("KEVIN")),
"Sakila_actors");
}

@Test
@Order(3)
@DisplayName("Create many actors")
void createBulk() throws Exception {
mockMvc.perform(post("/Sakila_actors/bulk")
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.content(objectMapper.writeValueAsString(BULK_CREATE_ACTOR_REQUEST))
)
.andExpect(status().isCreated())
.andExpect(jsonPath("$.rows").isArray())
.andExpect(jsonPath("$.rows", hasSize(2)))
.andExpect(jsonPath("$.rows", hasItem(1)))

.andExpect(jsonPath("$.keys").isArray())
.andExpect(jsonPath("$.keys", hasSize(2)))
.andExpect(jsonPath("$.keys", allOf(notNullValue())))
.andDo(document("mongodb-bulk-create-actors"));

mongoTemplate.remove(query(Criteria
.where("FirstName")
.is("VIVIEN")),
"Sakila_actors");
mongoTemplate.remove(query(Criteria
.where("FirstName")
.is("CUBA")),
"Sakila_actors");
}

@Test
@Order(4)
@DisplayName("Create many actors with include fields")
void createBulkWithIncludeFields() throws Exception {
mockMvc.perform(post("/Sakila_actors/bulk")
.contentType(APPLICATION_JSON)
.accept(APPLICATION_JSON)
.param("fields", "FirstName, LastName")
.content(objectMapper.writeValueAsString(BULK_CREATE_ACTOR_REQUEST))
)
.andExpect(status().isCreated())
.andExpect(jsonPath("$.rows").isArray())
.andExpect(jsonPath("$.rows", hasSize(2)))
.andExpect(jsonPath("$.rows", hasItem(1)))

.andExpect(jsonPath("$.keys").isArray())
.andExpect(jsonPath("$.keys", hasSize(2)))
.andExpect(jsonPath("$.keys", allOf(notNullValue())))
.andDo(document("mongodb-bulk-create-actors-with-include-fields"));

mongoTemplate.remove(query(Criteria
.where("FirstName")
.is("VIVIEN")),
"Sakila_actors");
mongoTemplate.remove(query(Criteria
.where("FirstName")
.is("CUBA")),
"Sakila_actors");
}

@Test
@Order(5)
@DisplayName("Update an existing Actor")
void updateExistingActor() throws Exception {
mockMvc.perform(patch("/Sakila_actors")
Expand All @@ -117,7 +204,7 @@ void updateExistingActor() throws Exception {
}

@Test
@Order(3)
@Order(6)
@DisplayName("Update a non-existing Actor")
void updateNonExistingActor() throws Exception {
mockMvc.perform(patch("/Sakila_actors")
Expand All @@ -132,7 +219,7 @@ void updateNonExistingActor() throws Exception {
}

@Test
@Order(4)
@Order(7)
@DisplayName("Update non-existing Collection")
void updateNonExistingCollection() throws Exception {
mockMvc.perform(patch("/unknown_collection")
Expand All @@ -147,7 +234,7 @@ void updateNonExistingCollection() throws Exception {
}

@Test
@Order(5)
@Order(8)
@DisplayName("Update multiple Actors")
void updateExistingActors() throws Exception {
mockMvc.perform(patch("/Sakila_actors")
Expand All @@ -162,7 +249,7 @@ void updateExistingActors() throws Exception {
}

@Test
@Order(6)
@Order(9)
@DisplayName("Test find all actors - all fields")
void findAllActors() throws Exception {
mockMvc.perform(get("/Sakila_actors")
Expand All @@ -175,7 +262,7 @@ void findAllActors() throws Exception {
}

@Test
@Order(7)
@Order(10)
@DisplayName("Test find all actors - 2 fields")
void findAllActorsWithTwoFields() throws Exception {
mockMvc.perform(get("/Sakila_actors")
Expand All @@ -192,7 +279,7 @@ void findAllActorsWithTwoFields() throws Exception {
}

@Test
@Order(8)
@Order(11)
@DisplayName("Test find all actors with filter")
void findAllActorsWithFilter() throws Exception {
mockMvc.perform(get("/Sakila_actors")
Expand All @@ -209,7 +296,7 @@ void findAllActorsWithFilter() throws Exception {
}

@Test
@Order(9)
@Order(12)
@DisplayName("Test find all actors with sorting")
void findAllActorsWithSorting() throws Exception {
mockMvc.perform(get("/Sakila_actors")
Expand All @@ -227,7 +314,7 @@ void findAllActorsWithSorting() throws Exception {
}

@Test
@Order(10)
@Order(13)
@DisplayName("Test find all actors with pagination")
void findAllActorsWithPagination() throws Exception {
mockMvc.perform(get("/Sakila_actors")
Expand All @@ -247,7 +334,7 @@ void findAllActorsWithPagination() throws Exception {
}

@Test
@Order(11)
@Order(14)
@DisplayName("Find one actor - 2 fields")
void findOneActor() throws Exception {
mockMvc.perform(get("/Sakila_actors/one")
Expand All @@ -263,7 +350,7 @@ void findOneActor() throws Exception {
}

@Test
@Order(12)
@Order(15)
@DisplayName("Total number of Actors")
void countAll() throws Exception {
mockMvc.perform(get("/Sakila_actors/count")
Expand All @@ -275,7 +362,7 @@ void countAll() throws Exception {
}

@Test
@Order(13)
@Order(16)
@DisplayName("Number of Actors by LastName")
void countActorsByLastName() throws Exception {
mockMvc.perform(get("/Sakila_actors/count")
Expand All @@ -288,7 +375,7 @@ void countActorsByLastName() throws Exception {
}

@Test
@Order(14)
@Order(17)
@DisplayName("Actor exists By FirstName")
void existsByLastName() throws Exception {
mockMvc.perform(get("/Sakila_actors/exists")
Expand All @@ -301,7 +388,7 @@ void existsByLastName() throws Exception {
}

@Test
@Order(15)
@Order(18)
@DisplayName("Actor exists By unknown name")
void existsByUnknownName() throws Exception {
mockMvc.perform(get("/Sakila_actors/exists")
Expand All @@ -314,7 +401,7 @@ void existsByUnknownName() throws Exception {
}

@Test
@Order(16)
@Order(19)
@DisplayName("Actor exists throws exception when 'filter' is not present")
void existsThrowsException() throws Exception {
mockMvc.perform(get("/Sakila_actors/exists")
Expand All @@ -327,7 +414,7 @@ void existsThrowsException() throws Exception {
}

@Test
@Order(17)
@Order(20)
@DisplayName("Delete all documents while allowSafeDelete=true")
void delete_all_documents_with_allow_safe_delete_true() throws Exception {
mockMvc.perform(delete("/Sakila_actors")
Expand All @@ -339,7 +426,7 @@ void delete_all_documents_with_allow_safe_delete_true() throws Exception {
}

@Test
@Order(18)
@Order(21)
@DisplayName("Delete an actor")
void delete_single_record() throws Exception {
mockMvc.perform(delete("/Sakila_actors")
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[
{
"FirstName" : "VIVIEN",
"LastName" : "BERGEN",
"phone" : "XXX-XXXX-XXX",
"address" : "XXXXXXXXXXXXXXXXXXXXXXXXXXXX"
},
{
"FirstName" : "CUBA",
"LastName" : "OLIVIER",
"phone" : "XXX-XXXX-XXX",
"address" : "XXXXXXXXXXXXXXXXXXXXXXXXXXXX"
}
]
Loading

0 comments on commit 5c9141e

Please sign in to comment.