Skip to content
Permalink
Browse files

Added Repository integration tests

  • Loading branch information...
borabilgin committed Jan 27, 2019
1 parent 5cd8c09 commit a747dd1d126bf07badc79a567a930264419d673b
@@ -29,7 +29,11 @@ public Feed() {
}

public Feed(Long id, String name, String address, FeedCategory category) {
this(name, address, category);
this.id = id;
}

public Feed(String name, String address, FeedCategory category) {
this.name = name;
this.address = address;
this.category = category;
@@ -37,7 +37,11 @@ public FeedCategory() {
}

public FeedCategory(Long id, String name) {
this(name);
this.id = id;
}

public FeedCategory(String name) {
this.name = name;
}

@@ -24,12 +24,18 @@
public Role() {
}


public Role(Long id, String description, String roleName) {
this(description, roleName);
this.id = id;
}

public Role(String description, String roleName) {
this.description = description;
this.roleName = roleName;
}


public Long getId() {
return id;
}
@@ -33,7 +33,11 @@ public User() {
}

public User(Long id, String username, String password, List<Role> roles) {
this(username, password, roles);
this.id = id;
}

public User(String username, String password, List<Role> roles) {
this.username = username;
this.password = password;
this.roles = roles;
@@ -0,0 +1,38 @@
package com.demo.imagebrowser.repository;

import com.demo.imagebrowser.domain.FeedCategory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.junit4.SpringRunner;

import static org.springframework.test.util.AssertionErrors.assertTrue;

@RunWith(SpringRunner.class)
@DataJpaTest
public class FeedCategoryRepositoryIntegrationTest {

@Autowired
private TestEntityManager entityManager;

@Autowired
private FeedCategoryRepository feedCategoryRepository;

@Test
public void shouldFindCategory_whenSearchByName() {
FeedCategory category = new FeedCategory("sample_category_1");
FeedCategory category2 = new FeedCategory("sample_category_2");

entityManager.persist(category);
entityManager.persist(category2);
entityManager.flush();

FeedCategory found1 = feedCategoryRepository.findByName(category.getName());
FeedCategory found2 = feedCategoryRepository.findByName(category2.getName());

assertTrue("Feed category should have been found", category.getName().equals(found1.getName()));
assertTrue("Feed category should have been found", category2.getName().equals(found2.getName()));
}
}
@@ -0,0 +1,50 @@
package com.demo.imagebrowser.repository;

import com.demo.imagebrowser.domain.Feed;
import com.demo.imagebrowser.domain.FeedCategory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.List;

import static org.junit.Assert.assertNotNull;
import static org.springframework.test.util.AssertionErrors.assertTrue;

@RunWith(SpringRunner.class)
@DataJpaTest
public class FeedRepositoryIntegrationTest {

@Autowired
private TestEntityManager entityManager;

@Autowired
private FeedRepository feedRepository;

@Test
public void shouldReturnFeed_whenSearchByCategory() {
FeedCategory category = new FeedCategory("sample_category_1");
FeedCategory category2 = new FeedCategory("sample_category_2");

entityManager.persist(category);
entityManager.persist(category2);
entityManager.flush();

Feed feed1 = new Feed("test", "https://test.com", category);
Feed feed2 = new Feed("test2", "https://test.com", category);
Feed feed3 = new Feed("test3", "https://test.com", category2);

entityManager.persist(feed1);
entityManager.persist(feed2);
entityManager.persist(feed3);
entityManager.flush();

List<Feed> feedFound = feedRepository.findByCategory(category);
assertNotNull("Should have found feed items", feedFound);
assertTrue("Should have found 2 feed items", feedFound.size() == 2);
}

}
@@ -0,0 +1,59 @@
package com.demo.imagebrowser.repository;

import com.demo.imagebrowser.domain.Role;
import com.demo.imagebrowser.domain.User;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.Collections;

import static org.junit.Assert.assertEquals;
import static org.springframework.test.util.AssertionErrors.assertTrue;

@RunWith(SpringRunner.class)
@DataJpaTest
public class UserRepositoryIntegrationTest {

@Autowired
private TestEntityManager entityManager;

@Autowired
private UserRepository userRepository;

@Test
public void shouldReturnUser_whenSearchByUsername() {
final String expectedUsername = "bora";

User user = new User(expectedUsername, "abc", Collections.emptyList());
entityManager.persist(user);
entityManager.flush();

User found = userRepository.findByUsername(expectedUsername);

assertEquals("Found user's name should be equal to inserted user's name", found.getUsername(), expectedUsername);
}

@Test
public void shouldIncludeRole_whenSearchByUsername() {
final String expectedUsername = "bora";
Role role = new Role();
role.setRoleName("user");
role.setDescription("test role");
entityManager.persist(role);
entityManager.flush();

User user = new User(expectedUsername, "abc", Arrays.asList(role));
entityManager.persist(user);
entityManager.flush();

User found = userRepository.findByUsername(expectedUsername);

assertTrue("Role size should be 1", found.getRoles().size() == 1);
assertEquals("Found user's role should be equal to inserted user's role", found.getRoles().get(0).getRoleName(), role.getRoleName());
}
}

0 comments on commit a747dd1

Please sign in to comment.
You can’t perform that action at this time.