Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.github.egnaf.spring_boot_docker_example.service;

import com.github.egnaf.spring_boot_docker_example.domain.User;
import com.github.egnaf.spring_boot_docker_example.exception.UserExistsException;
import com.github.egnaf.spring_boot_docker_example.exception.UserNotFoundException;

import java.util.List;

public interface UserService {

List<User> getUsers();
User getUser(long id);
User addUser(String nickname, String email, String password);
User getUser(long id) throws UserNotFoundException;
User addUser(String nickname, String email, String password) throws UserExistsException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@
import com.github.egnaf.spring_boot_docker_example.exception.UserNotFoundException;
import com.github.egnaf.spring_boot_docker_example.repository.UserRepository;
import com.github.egnaf.spring_boot_docker_example.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Optional;

@Service
@Slf4j
public class UserServiceImpl implements UserService {

private final UserRepository userRepository;
Expand All @@ -33,6 +35,7 @@ public User getUser(long id) {
if (user.isPresent()) {
return user.get();
} else {
log.info(String.valueOf(user.orElse(null)));
throw new UserNotFoundException();
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package com.github.egnaf.spring_boot_docker_example.service.impl;

import com.github.egnaf.spring_boot_docker_example.domain.User;
import com.github.egnaf.spring_boot_docker_example.exception.UserExistsException;
import com.github.egnaf.spring_boot_docker_example.exception.UserNotFoundException;
import com.github.egnaf.spring_boot_docker_example.repository.UserRepository;
import com.github.egnaf.spring_boot_docker_example.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
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 java.util.ArrayList;
import java.util.List;

import static org.junit.Assert.assertEquals;

@Slf4j
Expand All @@ -38,14 +41,41 @@ public void setUp() {
}

@Test
public void getUserTest() throws UserNotFoundException {
User actual = userService.getUser(1L);
public void getUsers() {
//expected
List<User> expected = new ArrayList<>();
expected.add(new User(1L, "user1", "user1@mail.com", "pass"));
expected.add(new User(2L, "user2", "user2@mail.com", "test"));

//actual
List<User> actual = userService.getUsers();

//assert
assertEquals(expected, actual);
}

@Test
public void getUser() throws UserNotFoundException {
//expected
User expected = new User(1L, "user1", "user1@mail.com", "pass");

//actual
User actual = userService.getUser(1L);

//assert
assertEquals(expected, actual);
}

@After
public void tearDown() {
userRepository.deleteAll();
@Test
public void addUser() throws UserExistsException {
//expected
User expected = new User(3L, "user3", "user3@mail.com", "demo");

//actual
User actual = userService.addUser(expected.getNickname(), expected.getEmail(), expected.getPassword());
userRepository.deleteById(3L);

//assert
assertEquals(expected, actual);
}
}