Skip to content

Commit

Permalink
feat : adds more assertions
Browse files Browse the repository at this point in the history
  • Loading branch information
rajadilipkolli committed Jul 6, 2024
1 parent dafc961 commit e208dff
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 11 deletions.
11 changes: 10 additions & 1 deletion .github/workflows/jpa-boot-data-custom-sequences.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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())));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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())));
}

Expand All @@ -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)))
Expand Down

0 comments on commit e208dff

Please sign in to comment.