From e208dffc6571a82654fdafe5353b9cc7e20f3cd4 Mon Sep 17 00:00:00 2001 From: Raja Kolli Date: Sat, 6 Jul 2024 18:48:22 +0000 Subject: [PATCH] feat : adds more assertions --- .github/workflows/jpa-boot-data-custom-sequences.yml | 11 ++++++++++- ...ingPrefixedNumberFormattedSequenceIdGenerator.java | 8 ++------ .../example/custom/sequence/entities/Customer.java | 2 +- .../web/controllers/CustomerControllerIT.java | 6 ++++-- .../sequence/web/controllers/OrderControllerIT.java | 8 +++++++- 5 files changed, 24 insertions(+), 11 deletions(-) diff --git a/.github/workflows/jpa-boot-data-custom-sequences.yml b/.github/workflows/jpa-boot-data-custom-sequences.yml index 2bfaab38c..150fa5cec 100644 --- a/.github/workflows/jpa-boot-data-custom-sequences.yml +++ b/.github/workflows/jpa-boot-data-custom-sequences.yml @@ -31,5 +31,14 @@ jobs: java-version: 21 distribution: "temurin" cache: "maven" + - name: Cache SonarCloud packages + uses: actions/cache@v3 + with: + path: ~/.sonar/cache + key: ${{ runner.os }}-sonar + restore-keys: ${{ runner.os }}-sonar - name: Build and analyze - run: ./mvnw clean verify + env: + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any + SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }} + run: ./mvnw -B verify sonar:sonar -Dsonar.projectKey=rajadilipkolli_my-spring-boot-experiments diff --git a/jpa/boot-data-customsequence/src/main/java/com/example/custom/sequence/config/StringPrefixedNumberFormattedSequenceIdGenerator.java b/jpa/boot-data-customsequence/src/main/java/com/example/custom/sequence/config/StringPrefixedNumberFormattedSequenceIdGenerator.java index 234936d81..12dcf44a5 100644 --- a/jpa/boot-data-customsequence/src/main/java/com/example/custom/sequence/config/StringPrefixedNumberFormattedSequenceIdGenerator.java +++ b/jpa/boot-data-customsequence/src/main/java/com/example/custom/sequence/config/StringPrefixedNumberFormattedSequenceIdGenerator.java @@ -14,12 +14,8 @@ public class StringPrefixedNumberFormattedSequenceIdGenerator extends SequenceStyleGenerator { - private String valuePrefix; - private String numberFormat; - - StringPrefixedNumberFormattedSequenceIdGenerator() { - super(); - } + private final String valuePrefix; + private final String numberFormat; public StringPrefixedNumberFormattedSequenceIdGenerator( StringPrefixedSequence config, diff --git a/jpa/boot-data-customsequence/src/main/java/com/example/custom/sequence/entities/Customer.java b/jpa/boot-data-customsequence/src/main/java/com/example/custom/sequence/entities/Customer.java index 044287d76..7eb30749b 100644 --- a/jpa/boot-data-customsequence/src/main/java/com/example/custom/sequence/entities/Customer.java +++ b/jpa/boot-data-customsequence/src/main/java/com/example/custom/sequence/entities/Customer.java @@ -29,7 +29,7 @@ public class Customer { @Id @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "custom_seq") - @StringPrefixedSequence(valuePrefix = "CUS_", numberFormat = "%05d") + @StringPrefixedSequence(valuePrefix = "CUS", numberFormat = "%05d") private String id; @Column(nullable = false) diff --git a/jpa/boot-data-customsequence/src/test/java/com/example/custom/sequence/web/controllers/CustomerControllerIT.java b/jpa/boot-data-customsequence/src/test/java/com/example/custom/sequence/web/controllers/CustomerControllerIT.java index 3dd269537..82dcdf7af 100644 --- a/jpa/boot-data-customsequence/src/test/java/com/example/custom/sequence/web/controllers/CustomerControllerIT.java +++ b/jpa/boot-data-customsequence/src/test/java/com/example/custom/sequence/web/controllers/CustomerControllerIT.java @@ -1,8 +1,9 @@ package com.example.custom.sequence.web.controllers; import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.Matchers.containsString; +import static org.hamcrest.Matchers.hasLength; import static org.hamcrest.Matchers.hasSize; +import static org.hamcrest.Matchers.startsWith; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; @@ -74,7 +75,8 @@ void shouldCreateNewCustomer() throws Exception { .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(customer))) .andExpect(status().isCreated()) - .andExpect(jsonPath("$.id", containsString("CUS_"))) + .andExpect(jsonPath("$.id", startsWith("CUS"))) + .andExpect(jsonPath("$.id", hasLength(8))) .andExpect(jsonPath("$.text", is(customer.getText()))); } diff --git a/jpa/boot-data-customsequence/src/test/java/com/example/custom/sequence/web/controllers/OrderControllerIT.java b/jpa/boot-data-customsequence/src/test/java/com/example/custom/sequence/web/controllers/OrderControllerIT.java index feb3988e6..179a88868 100644 --- a/jpa/boot-data-customsequence/src/test/java/com/example/custom/sequence/web/controllers/OrderControllerIT.java +++ b/jpa/boot-data-customsequence/src/test/java/com/example/custom/sequence/web/controllers/OrderControllerIT.java @@ -2,6 +2,7 @@ import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.CoreMatchers.notNullValue; +import static org.hamcrest.Matchers.hasLength; import static org.hamcrest.Matchers.hasSize; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; @@ -21,6 +22,7 @@ import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.HttpHeaders; import org.springframework.http.MediaType; class OrderControllerIT extends AbstractIntegrationTest { @@ -82,6 +84,7 @@ void shouldCreateNewOrder() throws Exception { .content(objectMapper.writeValueAsString(order))) .andExpect(status().isCreated()) .andExpect(jsonPath("$.id", notNullValue(), String.class)) + .andExpect(jsonPath("$.id", hasLength(9))) .andExpect(jsonPath("$.text", is(order.getText()))); } @@ -95,7 +98,10 @@ void shouldReturn400WhenCreateNewOrderWithoutText() throws Exception { .contentType(MediaType.APPLICATION_JSON) .content(objectMapper.writeValueAsString(order))) .andExpect(status().isBadRequest()) - .andExpect(header().string("Content-Type", is("application/problem+json"))) + .andExpect( + header().string( + HttpHeaders.CONTENT_TYPE, + is(MediaType.APPLICATION_PROBLEM_JSON_VALUE))) .andExpect(jsonPath("$.type", is("about:blank"))) .andExpect(jsonPath("$.title", is("Constraint Violation"))) .andExpect(jsonPath("$.status", is(400)))