vavr spring data (#2242)
* vavr spring data * formatting * formatting * formatting * formatting
This commit is contained in:
parent
4233d90902
commit
26fdeaac46
36
vavr/pom.xml
36
vavr/pom.xml
|
@ -6,10 +6,11 @@
|
|||
<version>1.0</version>
|
||||
<name>vavr</name>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-modules</artifactId>
|
||||
<version>1.0.0-SNAPSHOT</version>
|
||||
<parent>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-parent</artifactId>
|
||||
<version>2.0.0.BUILD-SNAPSHOT</version>
|
||||
<relativePath />
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
|
@ -24,9 +25,36 @@
|
|||
<artifactId>junit</artifactId>
|
||||
<version>${junit.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<repositories>
|
||||
<repository>
|
||||
<id>spring-snapshot</id>
|
||||
<name>Spring Snapshot Repository</name>
|
||||
<url>https://repo.spring.io/snapshot</url>
|
||||
<snapshots>
|
||||
<enabled>true</enabled>
|
||||
</snapshots>
|
||||
</repository>
|
||||
</repositories>
|
||||
|
||||
<properties>
|
||||
<java.version>1.8</java.version>
|
||||
<vavr.version>0.9.0</vavr.version>
|
||||
<junit.version>4.12</junit.version>
|
||||
</properties>
|
||||
|
|
|
@ -0,0 +1,11 @@
|
|||
package com.baeldung;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
|
||||
@SpringBootApplication
|
||||
public class Application {
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
}
|
|
@ -0,0 +1,18 @@
|
|||
package com.baeldung.repositories;
|
||||
|
||||
import org.springframework.data.repository.Repository;
|
||||
|
||||
import com.baeldung.vavr.User;
|
||||
|
||||
import io.vavr.collection.Seq;
|
||||
import io.vavr.control.Option;
|
||||
|
||||
public interface VavrUserRepository extends Repository<User, Long> {
|
||||
|
||||
Option<User> findById(long id);
|
||||
|
||||
Seq<User> findByName(String name);
|
||||
|
||||
User save(User user);
|
||||
|
||||
}
|
|
@ -0,0 +1,32 @@
|
|||
package com.baeldung.vavr;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private long id;
|
||||
|
||||
private String name;
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
}
|
|
@ -0,0 +1,47 @@
|
|||
package com.baeldung.vavr.repositories;
|
||||
|
||||
import com.baeldung.Application;
|
||||
import com.baeldung.repositories.VavrUserRepository;
|
||||
import com.baeldung.vavr.User;
|
||||
|
||||
import io.vavr.collection.Seq;
|
||||
import io.vavr.control.Option;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.Before;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
import static org.junit.Assert.*;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = Application.class)
|
||||
public class VavrRepositoryIntegrationTest {
|
||||
|
||||
@Autowired
|
||||
private VavrUserRepository userRepository;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
User user1 = new User();
|
||||
user1.setName("John");
|
||||
User user2 = new User();
|
||||
user2.setName("John");
|
||||
|
||||
userRepository.save(user1);
|
||||
userRepository.save(user2);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenAddUsers_thenGetUsers() {
|
||||
Option<User> user = userRepository.findById(1L);
|
||||
assertFalse(user.isEmpty());
|
||||
assertTrue(user.get().getName().equals("John"));
|
||||
|
||||
Seq<User> users = userRepository.findByName("John");
|
||||
assertEquals(2, users.size());
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue