From e71a13ad242425646015e8d3be0d7ca0d61db891 Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Thu, 21 Sep 2023 12:39:06 +0000 Subject: [PATCH] feat: coding end to end --- .../docker/docker-compose.yml | 25 ++++++--- boot-opensearch-sample/pom.xml | 2 +- .../opensearch/entities/Restaurant.java | 8 +-- .../repositories/RestaurantRepository.java | 3 +- .../services/RestaurantService.java | 4 +- .../web/controllers/RestaurantController.java | 6 +-- .../src/main/resources/application.properties | 21 ++------ .../opensearch/common/ContainersConfig.java | 52 ++++++++++++------- .../services/RestaurantServiceTest.java | 16 +++--- .../controllers/RestaurantControllerIT.java | 8 +-- .../controllers/RestaurantControllerTest.java | 20 +++---- 11 files changed, 88 insertions(+), 77 deletions(-) diff --git a/boot-opensearch-sample/docker/docker-compose.yml b/boot-opensearch-sample/docker/docker-compose.yml index d5676d455..1edb0c219 100644 --- a/boot-opensearch-sample/docker/docker-compose.yml +++ b/boot-opensearch-sample/docker/docker-compose.yml @@ -1,12 +1,23 @@ version: '3.8' services: - postgresqldb: - image: postgres:15.4-alpine - environment: - - POSTGRES_USER=appuser - - POSTGRES_PASSWORD=secret - - POSTGRES_DB=appdb + opensearch: + image: opensearchproject/opensearch:1.1.0 + container_name: opensearch + hostname: opensearch ports: - - "5432:5432" + - "9200:9200" + - "9600:9600" + environment: + - discovery.type=single-node + - "DISABLE_SECURITY_PLUGIN=true" + - "OPENSEARCH_JAVA_OPTS=-Xms512m -Xmx512m" + opensearch-dashboards: + image: opensearchproject/opensearch-dashboards:1.1.0 + container_name: opensearch_dashboards + ports: + - "5601:5601" + environment: + OPENSEARCH_HOSTS: '["http://opensearch:9200"]' + DISABLE_SECURITY_DASHBOARDS_PLUGIN: "true" diff --git a/boot-opensearch-sample/pom.xml b/boot-opensearch-sample/pom.xml index a820b6bd2..5c4e353c5 100644 --- a/boot-opensearch-sample/pom.xml +++ b/boot-opensearch-sample/pom.xml @@ -7,7 +7,7 @@ org.springframework.boot spring-boot-starter-parent - 3.1.3 + 3.1.4 com.example.opensearch diff --git a/boot-opensearch-sample/src/main/java/com/example/opensearch/entities/Restaurant.java b/boot-opensearch-sample/src/main/java/com/example/opensearch/entities/Restaurant.java index dcb5e107a..eff3e8b5e 100644 --- a/boot-opensearch-sample/src/main/java/com/example/opensearch/entities/Restaurant.java +++ b/boot-opensearch-sample/src/main/java/com/example/opensearch/entities/Restaurant.java @@ -8,23 +8,23 @@ @Document(indexName = "restaurants") public class Restaurant { - @Id private Long id; + @Id private String id; @NotEmpty(message = "Name cannot be empty") private String name; public Restaurant() {} - public Restaurant(Long id, String name) { + public Restaurant(String id, String name) { this.id = id; this.name = name; } - public Long getId() { + public String getId() { return id; } - public void setId(Long id) { + public void setId(String id) { this.id = id; } diff --git a/boot-opensearch-sample/src/main/java/com/example/opensearch/repositories/RestaurantRepository.java b/boot-opensearch-sample/src/main/java/com/example/opensearch/repositories/RestaurantRepository.java index 7e8158286..c04e19f80 100644 --- a/boot-opensearch-sample/src/main/java/com/example/opensearch/repositories/RestaurantRepository.java +++ b/boot-opensearch-sample/src/main/java/com/example/opensearch/repositories/RestaurantRepository.java @@ -7,4 +7,5 @@ @Repository public interface RestaurantRepository - extends ElasticsearchRepository, ListCrudRepository {} + extends ElasticsearchRepository, + ListCrudRepository {} diff --git a/boot-opensearch-sample/src/main/java/com/example/opensearch/services/RestaurantService.java b/boot-opensearch-sample/src/main/java/com/example/opensearch/services/RestaurantService.java index 14f683e56..867592cea 100644 --- a/boot-opensearch-sample/src/main/java/com/example/opensearch/services/RestaurantService.java +++ b/boot-opensearch-sample/src/main/java/com/example/opensearch/services/RestaurantService.java @@ -37,7 +37,7 @@ public PagedResult findAllRestaurants( return new PagedResult<>(restaurantsPage); } - public Optional findRestaurantById(Long id) { + public Optional findRestaurantById(String id) { return restaurantRepository.findById(id); } @@ -45,7 +45,7 @@ public Restaurant saveRestaurant(Restaurant restaurant) { return restaurantRepository.save(restaurant); } - public void deleteRestaurantById(Long id) { + public void deleteRestaurantById(String id) { restaurantRepository.deleteById(id); } } diff --git a/boot-opensearch-sample/src/main/java/com/example/opensearch/web/controllers/RestaurantController.java b/boot-opensearch-sample/src/main/java/com/example/opensearch/web/controllers/RestaurantController.java index e14d3b7c7..3fb665efc 100644 --- a/boot-opensearch-sample/src/main/java/com/example/opensearch/web/controllers/RestaurantController.java +++ b/boot-opensearch-sample/src/main/java/com/example/opensearch/web/controllers/RestaurantController.java @@ -60,7 +60,7 @@ public PagedResult getAllRestaurants( } @GetMapping("/{id}") - public ResponseEntity getRestaurantById(@PathVariable Long id) { + public ResponseEntity getRestaurantById(@PathVariable String id) { return restaurantService .findRestaurantById(id) .map(ResponseEntity::ok) @@ -75,7 +75,7 @@ public Restaurant createRestaurant(@RequestBody @Validated Restaurant restaurant @PutMapping("/{id}") public ResponseEntity updateRestaurant( - @PathVariable Long id, @RequestBody Restaurant restaurant) { + @PathVariable String id, @RequestBody Restaurant restaurant) { return restaurantService .findRestaurantById(id) .map( @@ -87,7 +87,7 @@ public ResponseEntity updateRestaurant( } @DeleteMapping("/{id}") - public ResponseEntity deleteRestaurant(@PathVariable Long id) { + public ResponseEntity deleteRestaurant(@PathVariable String id) { return restaurantService .findRestaurantById(id) .map( diff --git a/boot-opensearch-sample/src/main/resources/application.properties b/boot-opensearch-sample/src/main/resources/application.properties index 2be288223..fe80559a6 100644 --- a/boot-opensearch-sample/src/main/resources/application.properties +++ b/boot-opensearch-sample/src/main/resources/application.properties @@ -9,20 +9,7 @@ spring.mvc.problemdetails.enabled=true management.endpoints.web.exposure.include=configprops,env,health,info,logfile,loggers,metrics,prometheus management.endpoint.health.show-details=always -################ Database ##################### -spring.jpa.show-sql=false -spring.jpa.open-in-view=false -spring.data.jpa.repositories.bootstrap-mode=deferred -spring.datasource.hikari.auto-commit=false -spring.jpa.hibernate.ddl-auto=none -#spring.jpa.properties.hibernate.format_sql=true -spring.jpa.properties.hibernate.jdbc.time_zone=UTC -spring.jpa.properties.hibernate.generate_statistics=false -spring.jpa.properties.hibernate.jdbc.batch_size=25 -spring.jpa.properties.hibernate.order_inserts=true -spring.jpa.properties.hibernate.order_updates=true -spring.jpa.properties.hibernate.query.fail_on_pagination_over_collection_fetch=true -spring.jpa.properties.hibernate.query.in_clause_parameter_padding=true -spring.jpa.properties.hibernate.query.plan_cache_max_size=4096 -spring.jpa.properties.hibernate.connection.provider_disables_autocommit=true -spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true +################ opensearch ##################### +opensearch.uris=http://localhost:9200 +opensearch.username=admin +opensearch.password=admin \ No newline at end of file diff --git a/boot-opensearch-sample/src/test/java/com/example/opensearch/common/ContainersConfig.java b/boot-opensearch-sample/src/test/java/com/example/opensearch/common/ContainersConfig.java index 0c8cb2339..f0c2a6b64 100644 --- a/boot-opensearch-sample/src/test/java/com/example/opensearch/common/ContainersConfig.java +++ b/boot-opensearch-sample/src/test/java/com/example/opensearch/common/ContainersConfig.java @@ -3,31 +3,43 @@ import java.net.HttpURLConnection; import java.time.Duration; import org.springframework.boot.test.context.TestConfiguration; -import org.springframework.boot.testcontainers.service.connection.ServiceConnection; -import org.springframework.context.annotation.Bean; +import org.springframework.test.context.DynamicPropertyRegistry; +import org.springframework.test.context.DynamicPropertySource; import org.testcontainers.containers.GenericContainer; import org.testcontainers.containers.wait.strategy.HttpWaitStrategy; @TestConfiguration(proxyBeanMethods = false) public class ContainersConfig { - @Bean - @ServiceConnection("elasticsearch") - GenericContainer createOpenSearchContainer() { - return new GenericContainer<>("opensearchproject/opensearch:1.1.0") - .withEnv("discovery.type", "single-node") - .withEnv("DISABLE_SECURITY_PLUGIN", "true") - .withEnv("OPENSEARCH_JAVA_OPTS", "-Xms512m -Xmx512m") - .withExposedPorts(9200, 9600) - .waitingFor( - new HttpWaitStrategy() - .forPort(9200) - .forStatusCodeMatching( - response -> - response == HttpURLConnection.HTTP_OK - || response - == HttpURLConnection - .HTTP_UNAUTHORIZED) - .withStartupTimeout(Duration.ofMinutes(2))); + static final GenericContainer openSearchContainer = + new GenericContainer<>("opensearchproject/opensearch:1.1.0") + .withEnv("discovery.type", "single-node") + .withEnv("DISABLE_SECURITY_PLUGIN", "true") + .withEnv("OPENSEARCH_JAVA_OPTS", "-Xms512m -Xmx512m") + .withExposedPorts(9200, 9600) + .waitingFor( + new HttpWaitStrategy() + .forPort(9200) + .forStatusCodeMatching( + response -> + response == HttpURLConnection.HTTP_OK + || response + == HttpURLConnection + .HTTP_UNAUTHORIZED) + .withStartupTimeout(Duration.ofMinutes(4))); + + static { + openSearchContainer.start(); + } + + @DynamicPropertySource + static void setApplicationProperties(DynamicPropertyRegistry dynamicPropertyRegistry) { + dynamicPropertyRegistry.add( + "opensearch.uris", + () -> + "http://%s:%d" + .formatted( + openSearchContainer.getHost(), + openSearchContainer.getMappedPort(9200))); } } diff --git a/boot-opensearch-sample/src/test/java/com/example/opensearch/services/RestaurantServiceTest.java b/boot-opensearch-sample/src/test/java/com/example/opensearch/services/RestaurantServiceTest.java index 083c0d67a..1f8a7de2f 100644 --- a/boot-opensearch-sample/src/test/java/com/example/opensearch/services/RestaurantServiceTest.java +++ b/boot-opensearch-sample/src/test/java/com/example/opensearch/services/RestaurantServiceTest.java @@ -55,13 +55,13 @@ void findAllRestaurants() { @Test void findRestaurantById() { // given - given(restaurantRepository.findById(1L)).willReturn(Optional.of(getRestaurant())); + given(restaurantRepository.findById("1")).willReturn(Optional.of(getRestaurant())); // when - Optional optionalRestaurant = restaurantService.findRestaurantById(1L); + Optional optionalRestaurant = restaurantService.findRestaurantById("1"); // then assertThat(optionalRestaurant).isPresent(); Restaurant restaurant = optionalRestaurant.get(); - assertThat(restaurant.getId()).isEqualTo(1L); + assertThat(restaurant.getId()).isEqualTo("1"); assertThat(restaurant.getName()).isEqualTo("junitTest"); } @@ -73,23 +73,23 @@ void saveRestaurant() { Restaurant persistedRestaurant = restaurantService.saveRestaurant(getRestaurant()); // then assertThat(persistedRestaurant).isNotNull(); - assertThat(persistedRestaurant.getId()).isEqualTo(1L); + assertThat(persistedRestaurant.getId()).isEqualTo("1"); assertThat(persistedRestaurant.getName()).isEqualTo("junitTest"); } @Test void deleteRestaurantById() { // given - willDoNothing().given(restaurantRepository).deleteById(1L); + willDoNothing().given(restaurantRepository).deleteById("1"); // when - restaurantService.deleteRestaurantById(1L); + restaurantService.deleteRestaurantById("1"); // then - verify(restaurantRepository, times(1)).deleteById(1L); + verify(restaurantRepository, times(1)).deleteById("1"); } private Restaurant getRestaurant() { Restaurant restaurant = new Restaurant(); - restaurant.setId(1L); + restaurant.setId("1"); restaurant.setName("junitTest"); return restaurant; } diff --git a/boot-opensearch-sample/src/test/java/com/example/opensearch/web/controllers/RestaurantControllerIT.java b/boot-opensearch-sample/src/test/java/com/example/opensearch/web/controllers/RestaurantControllerIT.java index 61c0786e3..9c59273c6 100644 --- a/boot-opensearch-sample/src/test/java/com/example/opensearch/web/controllers/RestaurantControllerIT.java +++ b/boot-opensearch-sample/src/test/java/com/example/opensearch/web/controllers/RestaurantControllerIT.java @@ -56,12 +56,12 @@ void shouldFetchAllRestaurants() throws Exception { @Test void shouldFindRestaurantById() throws Exception { Restaurant restaurant = restaurantList.get(0); - Long restaurantId = restaurant.getId(); + String restaurantId = restaurant.getId(); this.mockMvc .perform(get("/api/restaurants/{id}", restaurantId)) .andExpect(status().isOk()) - .andExpect(jsonPath("$.id", is(restaurant.getId()), Long.class)) + .andExpect(jsonPath("$.id", is(restaurant.getId()), String.class)) .andExpect(jsonPath("$.name", is(restaurant.getName()))); } @@ -111,7 +111,7 @@ void shouldUpdateRestaurant() throws Exception { .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(restaurant))) .andExpect(status().isOk()) - .andExpect(jsonPath("$.id", is(restaurant.getId()), Long.class)) + .andExpect(jsonPath("$.id", is(restaurant.getId()), String.class)) .andExpect(jsonPath("$.name", is(restaurant.getName()))); } @@ -122,7 +122,7 @@ void shouldDeleteRestaurant() throws Exception { this.mockMvc .perform(delete("/api/restaurants/{id}", restaurant.getId())) .andExpect(status().isOk()) - .andExpect(jsonPath("$.id", is(restaurant.getId()), Long.class)) + .andExpect(jsonPath("$.id", is(restaurant.getId()), String.class)) .andExpect(jsonPath("$.name", is(restaurant.getName()))); } } diff --git a/boot-opensearch-sample/src/test/java/com/example/opensearch/web/controllers/RestaurantControllerTest.java b/boot-opensearch-sample/src/test/java/com/example/opensearch/web/controllers/RestaurantControllerTest.java index cf4f7b155..3cc57f998 100644 --- a/boot-opensearch-sample/src/test/java/com/example/opensearch/web/controllers/RestaurantControllerTest.java +++ b/boot-opensearch-sample/src/test/java/com/example/opensearch/web/controllers/RestaurantControllerTest.java @@ -48,9 +48,9 @@ class RestaurantControllerTest { @BeforeEach void setUp() { this.restaurantList = new ArrayList<>(); - this.restaurantList.add(new Restaurant(1L, "text 1")); - this.restaurantList.add(new Restaurant(2L, "text 2")); - this.restaurantList.add(new Restaurant(3L, "text 3")); + this.restaurantList.add(new Restaurant("1", "text 1")); + this.restaurantList.add(new Restaurant("2", "text 2")); + this.restaurantList.add(new Restaurant("3", "text 3")); } @Test @@ -75,7 +75,7 @@ void shouldFetchAllRestaurants() throws Exception { @Test void shouldFindRestaurantById() throws Exception { - Long restaurantId = 1L; + String restaurantId = "1"; Restaurant restaurant = new Restaurant(restaurantId, "text 1"); given(restaurantService.findRestaurantById(restaurantId)) .willReturn(Optional.of(restaurant)); @@ -88,7 +88,7 @@ void shouldFindRestaurantById() throws Exception { @Test void shouldReturn404WhenFetchingNonExistingRestaurant() throws Exception { - Long restaurantId = 1L; + String restaurantId = "1"; given(restaurantService.findRestaurantById(restaurantId)).willReturn(Optional.empty()); this.mockMvc @@ -101,7 +101,7 @@ void shouldCreateNewRestaurant() throws Exception { given(restaurantService.saveRestaurant(any(Restaurant.class))) .willAnswer((invocation) -> invocation.getArgument(0)); - Restaurant restaurant = new Restaurant(1L, "some text"); + Restaurant restaurant = new Restaurant("1", "some text"); this.mockMvc .perform( post("/api/restaurants") @@ -136,7 +136,7 @@ void shouldReturn400WhenCreateNewRestaurantWithoutText() throws Exception { @Test void shouldUpdateRestaurant() throws Exception { - Long restaurantId = 1L; + String restaurantId = "1"; Restaurant restaurant = new Restaurant(restaurantId, "Updated text"); given(restaurantService.findRestaurantById(restaurantId)) .willReturn(Optional.of(restaurant)); @@ -154,7 +154,7 @@ void shouldUpdateRestaurant() throws Exception { @Test void shouldReturn404WhenUpdatingNonExistingRestaurant() throws Exception { - Long restaurantId = 1L; + String restaurantId = "1"; given(restaurantService.findRestaurantById(restaurantId)).willReturn(Optional.empty()); Restaurant restaurant = new Restaurant(restaurantId, "Updated text"); @@ -168,7 +168,7 @@ void shouldReturn404WhenUpdatingNonExistingRestaurant() throws Exception { @Test void shouldDeleteRestaurant() throws Exception { - Long restaurantId = 1L; + String restaurantId = "1"; Restaurant restaurant = new Restaurant(restaurantId, "Some text"); given(restaurantService.findRestaurantById(restaurantId)) .willReturn(Optional.of(restaurant)); @@ -182,7 +182,7 @@ void shouldDeleteRestaurant() throws Exception { @Test void shouldReturn404WhenDeletingNonExistingRestaurant() throws Exception { - Long restaurantId = 1L; + String restaurantId = "1"; given(restaurantService.findRestaurantById(restaurantId)).willReturn(Optional.empty()); this.mockMvc