这是indexloc提供的服务,不要输入任何密码
Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.baeldung.partitionkey;

import java.math.BigDecimal;
import java.time.LocalDate;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Controller {

@Autowired
SalesRepository salesRepository;

@GetMapping
public ResponseEntity<List<Sales>> getAllPartition() {
return ResponseEntity.ok()
.body(salesRepository.findAll());
}

@GetMapping("add")
public ResponseEntity testPartition() {
return ResponseEntity.ok()
.body(salesRepository.save(new Sales(104L, LocalDate.of(2024, 02, 01), BigDecimal.valueOf(Double.parseDouble("8476.34d")))));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package com.baeldung.partitionkey;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.PropertySource;

@SpringBootApplication
@PropertySource("classpath:application-partition.properties")
public class DemoHibernatePartitionApplication {

public static void main(String[] args) {
SpringApplication.run(DemoHibernatePartitionApplication.class, args);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package com.baeldung.partitionkey;

import java.math.BigDecimal;
import java.time.LocalDate;

import jakarta.persistence.Entity;
import jakarta.persistence.GeneratedValue;
import jakarta.persistence.GenerationType;
import jakarta.persistence.Id;
import jakarta.persistence.Table;

import org.hibernate.annotations.PartitionKey;

@Entity
@Table(name = "sales")
public class Sales {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@PartitionKey
private LocalDate saleDate;

private BigDecimal amount;

public Long getId() {
return id;
}

public void setId(Long id) {
this.id = id;
}

public LocalDate getSaleDate() {
return saleDate;
}

public void setSaleDate(LocalDate saleDate) {
this.saleDate = saleDate;
}

public BigDecimal getAmount() {
return amount;
}

public void setAmount(BigDecimal amount) {
this.amount = amount;
}

public Sales(Long id, LocalDate saleDate, BigDecimal amount) {
this.id = id;
this.saleDate = saleDate;
this.amount = amount;
}

public Sales() {
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.baeldung.partitionkey;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.stereotype.Repository;

@Repository
public interface SalesRepository extends JpaRepository<Sales, Long> {

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
spring.application.name=partitionKeyDemo
# PostgreSQL connection properties
spring.datasource.url=jdbc:postgresql://localhost:6000/salesTest
spring.datasource.username=username
spring.datasource.password=password
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package com.baeldung.partitionkey;

import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import java.math.BigDecimal;
import java.time.LocalDate;

import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.http.MediaType;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.test.web.servlet.MockMvc;

@WebMvcTest(controllers = Controller.class, excludeAutoConfiguration = { DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class })
public class SalesControllerUnitTest {

@Autowired
private MockMvc mockMvc;

@MockBean
private SalesRepository salesRepository;

@Test
void givenValidSalesObject_whenAddingSales_thenPersistsAndReturnsCorrectly() throws Exception {
Sales sales = new Sales(104L, LocalDate.of(2024, 2, 1), BigDecimal.valueOf(8476.34d));
when(salesRepository.save(ArgumentMatchers.any(Sales.class))).thenReturn(sales);
mockMvc.perform(get("/add").contentType(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("$.id").value(104L))
.andExpect(jsonPath("$.saleDate").value("2024-02-01"))
.andExpect(jsonPath("$.amount").value(8476.34));
}
}