Javaslang Fetched Upstream and Updated (#1368)
* javaslang updates * groovy * update and clean from upstream * upate from upstream and clean * update * cleanup * clean
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
package com.baeldung.spring_data.model;
|
||||
|
||||
import javaslang.collection.Seq;
|
||||
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
import javax.persistence.CollectionTable;
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.ElementCollection;
|
||||
import javax.persistence.Entity;
|
||||
|
||||
@Entity
|
||||
@Table(name = "book")
|
||||
public class Book {
|
||||
|
||||
@GeneratedValue
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
private String title;
|
||||
|
||||
private Seq<String> authors;
|
||||
|
||||
|
||||
public void setTitle(String title){
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getTitle(){
|
||||
return this.title;
|
||||
}
|
||||
|
||||
public Long getId(){
|
||||
return this.id;
|
||||
}
|
||||
|
||||
public void setAuthors(Seq authors){
|
||||
this.authors = authors;
|
||||
}
|
||||
|
||||
public Seq getAuthors(){
|
||||
return this.authors;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
package com.baeldung.spring_data.model;
|
||||
|
||||
import javax.persistence.ElementCollection;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.Table;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Entity
|
||||
@Table(name = "java_book")
|
||||
public class JavaBook {
|
||||
|
||||
@GeneratedValue
|
||||
@Id
|
||||
private Long id;
|
||||
|
||||
private String title;
|
||||
|
||||
@ElementCollection
|
||||
private List<String> authors;
|
||||
|
||||
|
||||
public void setAuthors(List<String> authors){
|
||||
this.authors = authors;
|
||||
}
|
||||
|
||||
public void setTitle(String title){
|
||||
this.title = title;
|
||||
}
|
||||
|
||||
public String getTitle(){
|
||||
return this.title;
|
||||
}
|
||||
|
||||
public Long getId(){
|
||||
return this.id;
|
||||
}
|
||||
}
|
||||
|
||||
+19
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.spring_data.repository;
|
||||
|
||||
import com.baeldung.spring_data.model.Book;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import javaslang.collection.Seq;
|
||||
import javaslang.control.Option;
|
||||
|
||||
@Repository
|
||||
public interface BookRepository extends JpaRepository<Book,Long>{
|
||||
Book save(Book book);
|
||||
|
||||
Option<Book> findById(Long id);
|
||||
|
||||
Option<Seq<Book>> findByTitleContaining(String title);
|
||||
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package com.baeldung.spring_data.repository;
|
||||
|
||||
import com.baeldung.spring_data.model.JavaBook;
|
||||
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.stereotype.Repository;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Repository
|
||||
public interface JavaBookRepository extends JpaRepository<JavaBook,Long>{
|
||||
JavaBook save(JavaBook book);
|
||||
|
||||
JavaBook findById(Long id);
|
||||
|
||||
List<JavaBook> findByTitleContaining(String title);
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
package com.baeldung.spring_data_app;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
import org.springframework.transaction.annotation.EnableTransactionManagement;
|
||||
|
||||
@Configuration
|
||||
@EnableJpaRepositories("com.baeldung.spring_data.repository")
|
||||
@EnableTransactionManagement
|
||||
@EntityScan("com.baeldung.spring_data.model")
|
||||
@SpringBootApplication
|
||||
public class MainApp {
|
||||
public static void main(String[] args){
|
||||
SpringApplication.run(MainApp.class, args);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,86 @@
|
||||
package com.baeldung.spring_data_tests;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
|
||||
import com.baeldung.spring_data_app.MainApp;
|
||||
import com.baeldung.spring_data.model.Book;
|
||||
import com.baeldung.spring_data.model.JavaBook;
|
||||
import com.baeldung.spring_data.repository.BookRepository;
|
||||
import com.baeldung.spring_data.repository.JavaBookRepository;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import javaslang.collection.Seq;
|
||||
import javaslang.collection.List;
|
||||
import javaslang.control.Option;
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = MainApp.class,webEnvironment = WebEnvironment.NONE)
|
||||
public class SpringTests {
|
||||
|
||||
@Autowired
|
||||
JavaBookRepository javaRepository;
|
||||
|
||||
@Autowired
|
||||
BookRepository repository;
|
||||
|
||||
@Test
|
||||
public void should_return_seq(){
|
||||
Seq authors = List.of("author1","author2");
|
||||
Book testBook = new Book();
|
||||
testBook.setTitle("Javaslang in Spring Data Seq Test Return");
|
||||
testBook.setAuthors(authors);
|
||||
Book book = repository.save(testBook);
|
||||
Option<Seq<Book>> books = repository.findByTitleContaining("Seq Test");
|
||||
assert(!books.isEmpty());
|
||||
}
|
||||
|
||||
|
||||
@Test
|
||||
public void should_return_option_with_book(){
|
||||
Seq authors = List.of("author1","author2");
|
||||
Book testBook = new Book();
|
||||
testBook.setTitle("Javaslang in Spring Data");
|
||||
testBook.setAuthors(authors);
|
||||
Book book = repository.save(testBook);
|
||||
Option<Book> retBook = repository.findById(1L);
|
||||
assert(retBook.isDefined() && !retBook.isEmpty());
|
||||
assert(retBook.get() != null);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_return_list(){
|
||||
ArrayList<String> authors = new ArrayList<String>();
|
||||
authors.add("author1");
|
||||
authors.add("author2");
|
||||
JavaBook testBook = new JavaBook();
|
||||
testBook.setTitle("Javaslang in Spring Data Seq Return");
|
||||
testBook.setAuthors(authors);
|
||||
JavaBook book = javaRepository.save(testBook);
|
||||
java.util.List<JavaBook> books = javaRepository.findByTitleContaining("Seq");
|
||||
assert(!books.isEmpty());
|
||||
assert(books.size() == 1);
|
||||
assert(books.get(0).getTitle().equals("Javaslang in Spring Data Seq Return"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void should_return_book(){
|
||||
ArrayList<String> authors = new ArrayList<String>();
|
||||
authors.add("author1");
|
||||
authors.add("author2");
|
||||
JavaBook testBook = new JavaBook();
|
||||
testBook.setTitle("Javaslang in Spring Data");
|
||||
testBook.setAuthors(authors);
|
||||
JavaBook book = javaRepository.save(testBook);
|
||||
JavaBook retBook = javaRepository.findById(1L);
|
||||
assert(retBook != null);
|
||||
assert(retBook.getId() == 1L);
|
||||
assert(retBook.getTitle().contains("Data"));
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user