Skip to content
Permalink
Browse files

Added Service integration tests

  • Loading branch information...
borabilgin committed Jan 27, 2019
1 parent a747dd1 commit f8b193d889f32a47ea0b3a14bca136305fae456e
@@ -2,6 +2,7 @@

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Objects;

@Entity
@Table(name = "FEED")
@@ -38,4 +39,49 @@ public Feed(String name, String address, FeedCategory category) {
this.address = address;
this.category = category;
}

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;
}

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public FeedCategory getCategory() {
return category;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Feed feed = (Feed) o;
return Objects.equals(id, feed.id);
}

@Override
public int hashCode() {
return Objects.hash(id);
}

public void setCategory(FeedCategory category) {
this.category = category;
}
}
@@ -0,0 +1,80 @@
package com.demo.imagebrowser.service.impl;

import com.demo.imagebrowser.domain.Feed;
import com.demo.imagebrowser.domain.FeedCategory;
import com.demo.imagebrowser.repository.FeedCategoryRepository;
import com.demo.imagebrowser.repository.FeedRepository;
import com.demo.imagebrowser.service.FeedService;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.*;

@RunWith(SpringRunner.class)
public class FeedServiceImplTest {

private static String CATEGORY_NAME = "TEST CATEGORY";
private static String FEED_NAME = "TEST FEED";

private Feed testFeed;
private FeedCategory testFeedCategory;

@TestConfiguration
static class FeedServiceImplTestContextConfiguration {
@Bean
public FeedService feedService() {
return new FeedServiceImpl();
}
}

@Autowired
private FeedService feedService;

@MockBean
private FeedRepository feedRepository;

@MockBean
private FeedCategoryRepository feedCategoryRepository;

@Before
public void setUp() throws Exception {
testFeedCategory = new FeedCategory(CATEGORY_NAME);
testFeed = new Feed(FEED_NAME, "test", testFeedCategory);

Mockito.when(feedCategoryRepository.findByName(CATEGORY_NAME)).thenReturn(testFeedCategory);
Mockito.when(feedRepository.findByCategory(testFeedCategory)).thenReturn(Arrays.asList(testFeed));
Mockito.when(feedCategoryRepository.findByName(CATEGORY_NAME)).thenReturn(testFeedCategory);
Mockito.when(feedRepository.save(testFeed)).thenReturn(testFeed);
}

@Test
public void shouldReturnFeed_whenSearchByCategoryName() {
List<Feed> feeds = feedService.findByCategoryName(CATEGORY_NAME);

assertNotNull("Should return feed", feeds);
assertTrue("Category should have 1 feed", feeds.size() == 1);
assertEquals("Correct feed should have been returned", FEED_NAME, feeds.get(0).getName());
}

@Test
public void shouldReturnTrue_whenSearchByCategoryName() {
assertTrue("Category should exist", feedService.categoryExists(CATEGORY_NAME));
}

@Test
public void shouldSaveFeed_whenGivenFeedEntity() {
assertEquals("Feed should be saved", testFeed, feedService.addFeed(testFeed));
}


}
@@ -0,0 +1,65 @@
package com.demo.imagebrowser.service.impl;

import com.demo.imagebrowser.domain.Role;
import com.demo.imagebrowser.domain.User;
import com.demo.imagebrowser.repository.UserRepository;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.TestConfiguration;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.context.annotation.Bean;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.test.context.junit4.SpringRunner;

import java.util.Arrays;
import java.util.List;

import static org.junit.Assert.*;

@RunWith(SpringRunner.class)
public class UserDetailsServiceIntegrationTest {

private static String USERNAME = "TEST_USER";
private static String ROLE = "ELEVATED_USER_ROLE";

private List<Role> roles = Arrays.asList(new Role("description", ROLE));

private User testUser = new User(USERNAME, "password", roles);

@TestConfiguration
static class UserDetailsServiceIntegrationTestContextConfiguration {
@Bean
public UserDetailsService userDetailsService() {
return new UserDetailsService();
}
}

@Autowired
private UserDetailsService userDetailsService;

@MockBean
private UserRepository userRepository;

@Before
public void setUp() throws Exception {
Mockito.when(userRepository.findByUsername(USERNAME)).thenReturn(testUser);
}

@Test
public void shouldReturnUser_whenSearchByUsername(){
assertNotNull("Should find user", userDetailsService.loadUserByUsername(USERNAME));
}

@Test
public void shouldMatchUsername_whenSearchByUsername(){
assertEquals("Should find by username", testUser.getUsername(), userDetailsService.loadUserByUsername(USERNAME).getUsername());
}

@Test
public void shouldMatchRoles_whenSearchByUsername(){
roles.stream().forEach(r -> assertTrue("Role should match", userDetailsService.loadUserByUsername(USERNAME).getAuthorities().stream().anyMatch(a -> ((GrantedAuthority) a).getAuthority().equals(r.getRoleName()))));
}
}

0 comments on commit f8b193d

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