Skip to content

Commit

Permalink
Add basic integration tests to sample
Browse files Browse the repository at this point in the history
  • Loading branch information
ghillert committed Oct 17, 2022
1 parent b53115b commit 9994170
Show file tree
Hide file tree
Showing 15 changed files with 391 additions and 98 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2021, Oracle and/or its affiliates.
* Copyright (c) 2020, 2022, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* http://oss.oracle.com/licenses/upl.
Expand All @@ -11,13 +11,8 @@
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;

import com.oracle.coherence.hibernate.demo.configuration.EventMixin;
import com.oracle.coherence.hibernate.demo.model.Event;

/**
*
* @author Gunnar Hillert
*
*/
@SpringBootApplication
public class CoherenceHibernateDemoApplication {
Expand All @@ -29,7 +24,7 @@ public static void main(String[] args) {
@Bean
public Jackson2ObjectMapperBuilderCustomizer addCustomBigDecimalDeserialization() {
return builder -> {
builder.mixIn(Event.class, EventMixin.class);
// Customize if needed
};
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates.
* Copyright (c) 2020, 2022, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
*/
package com.oracle.coherence.hibernate.demo.controller;

import java.util.Date;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.oracle.coherence.hibernate.demo.model.Event;
import com.oracle.coherence.hibernate.demo.service.EventService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
Expand All @@ -19,43 +20,38 @@
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

//import com.hillert.s1.plants.controller.problems.PlantNotFoundProblem;
//import com.hillert.s1.plants.dto.PlantDto;
//import com.hillert.s1.plants.dto.PlantDtoDistanceComparator;
//import com.hillert.s1.plants.model.Image;
//import com.hillert.s1.plants.model.Plant;
//import com.hillert.s1.plants.service.PlantService;
//import com.hillert.s1.plants.support.DistanceUtils;
import com.oracle.coherence.hibernate.demo.model.Event;
import com.oracle.coherence.hibernate.demo.service.EventService;
import java.time.LocalDate;

/**
* Explicit controller for retrieving Events.
*
* @author Gunnar Hillert
*
*/
@RestController
@RequestMapping(path="/api/events")
@RequestMapping(path = "/api/events")
public class EventController {

@Autowired
private EventService eventService;

@Autowired
ObjectMapper om;

@GetMapping
public Page<Event> getEvents(Pageable pageable) {
return eventService.listEvents(pageable);
}

@PostMapping
public Long createEvent(
@RequestParam("title") String title,
@RequestParam("date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date date) {
@RequestParam("title") String title,
@RequestParam("date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
return eventService.createAndStoreEvent(title, date);
}

@GetMapping("/{id}")
public Event getEvent(@PathVariable Long id) {
return eventService.getEvent(id);
public Event getEvent(@PathVariable Long id, @RequestParam(required = false, defaultValue = "false") boolean withParticipants) {
Event event = eventService.getEvent(id, withParticipants);
return event;
}
}
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates.
* Copyright (c) 2020, 2022, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
*/
package com.oracle.coherence.hibernate.demo.dao;

import com.oracle.coherence.hibernate.demo.model.Event;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.QueryHints;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import com.oracle.coherence.hibernate.demo.model.Event;
import javax.persistence.QueryHint;
import java.util.Optional;

/**
*
* @author Gunnar Hillert
*
*/
@Repository
public interface EventRepository extends JpaRepository<Event, Long> {

@Query("SELECT event FROM Event event join fetch event.participants p join fetch p.events where event.id = :eventId")
@QueryHints(@QueryHint(name = org.hibernate.annotations.QueryHints.CACHEABLE, value = "true"))
Optional<Event> getEventWithParticipants(@Param("eventId") Long eventId);
}
Original file line number Diff line number Diff line change
@@ -1,48 +1,52 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates.
* Copyright (c) 2020, 2022, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
*/
package com.oracle.coherence.hibernate.demo.model;

import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.NaturalId;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;
import org.hibernate.annotations.NaturalId;
import java.time.LocalDate;
import java.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;

/**
*
* @author Gunnar Hillert
*
*/
@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Table(name="EVENTS")
@Table(name = "EVENTS")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id")
public class Event {

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

@NaturalId
private String title;

@NaturalId
private Date date;
private LocalDate date;

@ManyToMany(targetEntity = Person.class)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@JsonIdentityReference(alwaysAsId = true)
private Set<Person> participants = new HashSet<>();

public Event() {
Expand All @@ -56,11 +60,11 @@ public void setId(Long id) {
this.id = id;
}

public Date getDate() {
public LocalDate getDate() {
return date;
}

public void setDate(Date date) {
public void setDate(LocalDate date) {
this.date = date;
}

Expand Down Expand Up @@ -90,4 +94,10 @@ public void removeParticipant(Person person) {
person.getEvents().remove(this);
}

@JsonProperty("participants")
public void setParticipantIds(Set<Long> participantIds) {
this.participants.addAll(participantIds.stream().map(id -> {
return new Person(id);
}).collect(Collectors.toList()));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates.
* Copyright (c) 2020, 2022, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
Expand All @@ -21,6 +21,10 @@
import javax.persistence.ManyToMany;
import javax.persistence.Table;

import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;
import org.hibernate.annotations.Cache;
import org.hibernate.annotations.CacheConcurrencyStrategy;

Expand All @@ -32,6 +36,8 @@
@Entity
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@Table(name="PEOPLE")
@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "id", scope = Person.class
)
public class Person {

@Id
Expand All @@ -43,9 +49,11 @@ public class Person {

@ManyToMany(targetEntity = Event.class)
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
@JsonIdentityReference
@JsonIgnore
private Set<Event> events = new HashSet<>();

@ElementCollection(fetch = FetchType.LAZY)
@ElementCollection(fetch = FetchType.EAGER)
@CollectionTable(name = "PERSON_EMAIL_ADDR", joinColumns = @JoinColumn(name = "PERSON_ID"))
@Column(name = "EMAIL_ADDR")
@Cache(usage = CacheConcurrencyStrategy.READ_WRITE)
Expand All @@ -54,6 +62,10 @@ public class Person {
public Person() {
}

public Person(Long id) {
this.id = id;
}

public Long getId() {
return id;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,39 +1,34 @@
/*
* Copyright (c) 2020, Oracle and/or its affiliates.
* Copyright (c) 2020, 2022, Oracle and/or its affiliates.
*
* Licensed under the Universal Permissive License v 1.0 as shown at
* https://oss.oracle.com/licenses/upl.
*/
package com.oracle.coherence.hibernate.demo.service;

import java.util.Date;

import com.oracle.coherence.hibernate.demo.model.Event;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;

import com.oracle.coherence.hibernate.demo.model.Event;
import java.time.LocalDate;

/**
*
* @author Gunnar Hillert
*
*/
public interface EventService {

/**
*
* @return
*/
Page<Event> listEvents(Pageable pageable);

/**
*
* @param title
* @param date
* @return
*/
Long createAndStoreEvent(String title, Date date);
Long createAndStoreEvent(String title, LocalDate date);

Event getEvent(Long id);
Event getEvent(Long id, boolean withParticipants);

}

0 comments on commit 9994170

Please sign in to comment.