Last active
June 10, 2019 18:50
-
-
Save jbleduigou/fe08f619f2cbacea48182ccab6abef2e to your computer and use it in GitHub Desktop.
Junit Assertions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.jbleduigou.beer.model; | |
import com.fasterxml.jackson.annotation.JsonIgnore; | |
import lombok.AllArgsConstructor; | |
import lombok.Data; | |
import lombok.NoArgsConstructor; | |
import javax.persistence.Entity; | |
import javax.persistence.GeneratedValue; | |
import javax.persistence.GenerationType; | |
import javax.persistence.Id; | |
@AllArgsConstructor | |
@NoArgsConstructor | |
@Data | |
@Entity | |
public class Beer { | |
@Id | |
@GeneratedValue(strategy = GenerationType.IDENTITY) | |
private Long id; | |
private String name; | |
private Double alcoholByVolume; | |
@JsonIgnore | |
public boolean isAlcoholFree() { | |
return alcoholByVolume != null && alcoholByVolume <= 0.5; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.jbleduigou.beer.repository; | |
import com.github.jbleduigou.beer.model.Beer; | |
import org.springframework.data.jpa.repository.JpaRepository; | |
import org.springframework.stereotype.Repository; | |
@Repository | |
public interface BeerRepository extends JpaRepository<Beer, Long> { | |
Beer findByName(String name); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.github.jbleduigou.beer.service; | |
import com.github.jbleduigou.beer.exception.EntityNotFoundException; | |
import com.github.jbleduigou.beer.model.Beer; | |
import com.github.jbleduigou.beer.repository.BeerRepository; | |
import org.springframework.beans.factory.annotation.Autowired; | |
import org.springframework.stereotype.Service; | |
import javax.validation.constraints.NotNull; | |
import java.util.List; | |
import java.util.stream.Collectors; | |
@Service | |
public class BeerService { | |
private final BeerRepository repository; | |
@Autowired | |
public BeerService(BeerRepository repository) { | |
this.repository = repository; | |
} | |
public List<Beer> getAllBeers() { | |
return repository.findAll(); | |
} | |
public List<Beer> getAllNonAlcoholicBeers() { | |
return repository.findAll().stream() | |
.filter(Beer::isAlcoholFree) | |
.collect(Collectors.toList()); | |
} | |
public Beer getBeerById(@NotNull Long beerId) { | |
return repository.findById(beerId).orElseThrow(() -> new EntityNotFoundException("Beer", beerId)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment