Added some tests for friend methods

This commit is contained in:
Merel Steenbergen
2019-03-26 10:47:12 +01:00
parent ba5e261c7f
commit 7a4a031b1d
5 changed files with 65 additions and 1 deletions

View File

@@ -94,4 +94,18 @@ public class UserService {
.encode().toUri(), String.class);
return result;
}
@SuppressWarnings("Duplicates")
public String updaaddFriend(String name, String friend) {
HttpHeaders headers = new HttpHeaders();
headers.set("Accept", MediaType.APPLICATION_JSON_VALUE);
UriComponentsBuilder builder = UriComponentsBuilder.fromHttpUrl("http://localhost:8080/setInput")
.queryParam("name", name)
.queryParam("friend",friend);
HttpEntity<?> entity = new HttpEntity<>(headers);
System.out.println(builder.build().encode().toUri());
String result = this.restTemplate.getForObject(builder.build()
.encode().toUri(), String.class);
return result;
}
}

View File

@@ -1,5 +1,6 @@
package greenify.server.data.model;
import greenify.common.ApplicationException;
import greenify.server.InputValidator;
import lombok.Data;
@@ -14,6 +15,7 @@ import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
import java.util.*;
import java.util.logging.Logger;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
@@ -57,6 +59,7 @@ public class User {
this.name = name;
this.password = password;
this.setFootPrintInputs(InputValidator.getDefaultValues());
this.friends = new ArrayList<User>();
}
/**
@@ -115,6 +118,16 @@ public class User {
return (ArrayList<User>)this.friends;
}
public void addFriend(User user){
if(user.equals(this)){
throw new ApplicationException("Cannot add yourself as a friend");
}
else{
friends.add(user);
System.out.print("Friend added!");
}
}
public void setFootPrint(Float footPrint) {
this.footPrint = footPrint;
}

View File

@@ -59,6 +59,21 @@ public class UserService {
return new UserDto(user.getId(), user.getName());
}
/**
* Adds a friend to the friendlist of the user.
* @param name the username of the user
* @param friend the name of the friend you want to add.
* @return a userDTO of the logged in user
*/
public void addFriend(String name, String friend) {
User user = userRepository.findByName(name);
User add = userRepository.findByName(friend);
if (add == null) {
throw new ApplicationException("User does not exist");
}
user.addFriend(add);
}
/**
* The method sets input value.
* @param name of the user

View File

@@ -2,5 +2,5 @@ spring.datasource.url=jdbc:h2:file:~/spring-boot-h2.db;DB_CLOSE_ON_EXIT=FALSE
spring.datasource.username=sa
spring.datasource.password=
spring.datasource.driver-class-name=org.h2.Driver
spring.jpa.hibernate.ddl-auto=create-drop
spring.jpa.hibernate.ddl-auto=update
spring.h2.console.enabled=true

View File

@@ -6,6 +6,8 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
import org.junit.Test;
import java.util.ArrayList;
public class UserTest {
@Test
public void setAndGetTest() {
@@ -78,5 +80,25 @@ public class UserTest {
assertEquals(first, second);
assertEquals(first.hashCode(), second.hashCode());
}
@Test
public void getFriendEmpty(){
User first = new User(1L, "greenify", "password");
User second = new User(1L, "merel", "password");
assertEquals(first.getFriends(), second.getFriends());
assertEquals(first.getFriends(), new ArrayList<User>());
}
@Test
public void addFriend(){
User first = new User(1L, "greenify", "password");
User second = new User(1L, "merel", "password");
assertEquals(first.getFriends(), second.getFriends());
System.out.print(second);
first.addFriend(second);
ArrayList<User> test = new ArrayList<User>();
test.add(second);
assertEquals(first.getFriends(), test);
}
}