Finish activity class and add tests
This commit is contained in:
@@ -1,25 +0,0 @@
|
||||
package greenify.client.rest;
|
||||
|
||||
import greenify.common.ActivityDTO;
|
||||
import greenify.common.ApplicationException;
|
||||
import greenify.common.UserDTO;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
|
||||
public class ActivityService {
|
||||
|
||||
Logger logger = LoggerFactory.getLogger(greenify.server.service.ActivityService.class);
|
||||
@Autowired
|
||||
UserRepository userRepository;
|
||||
|
||||
/**
|
||||
* Adds the activity to the history of the user and adds the score.
|
||||
* @param name the name of the activity
|
||||
* @return a activityDTO of activity.
|
||||
*/
|
||||
public ActivityDTO addActivity(String name) {
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
@@ -3,20 +3,20 @@ package greenify.server.data.model;
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.accessibility.AccessibleValue;
|
||||
import javax.persistence.*;
|
||||
import java.util.Objects;
|
||||
import java.util.Scanner;
|
||||
|
||||
@Entity
|
||||
@Data
|
||||
@Table(name = "activities")
|
||||
//@AllArgsConstructor
|
||||
public abstract class Activity {
|
||||
public class Activity {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
Long id;
|
||||
long id;
|
||||
String name;
|
||||
String description;
|
||||
int score;
|
||||
@@ -28,10 +28,10 @@ public abstract class Activity {
|
||||
* @param description the description of the feature.
|
||||
* @param score the amount of points a user gets for doing this activity.
|
||||
*/
|
||||
public void User(Long id, String name, String description, int score) {
|
||||
public Activity(long id, String name, String description, int score) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.description = setDes(description);
|
||||
this.description = description;
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
@@ -39,7 +39,7 @@ public abstract class Activity {
|
||||
* gets the id.
|
||||
* @return the id
|
||||
*/
|
||||
public Long getId() {
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
@@ -52,14 +52,27 @@ public abstract class Activity {
|
||||
}
|
||||
|
||||
/**
|
||||
* gets the password.
|
||||
* @return the password
|
||||
* gets the description.
|
||||
* @return the description
|
||||
*/
|
||||
public String getDescription() {
|
||||
return description;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
/**
|
||||
* gets the score.
|
||||
* @return the score
|
||||
*/
|
||||
public int getScore() {
|
||||
return score;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* sets the id.
|
||||
* @param id the you want to assign to this.id.
|
||||
*/
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
@@ -75,12 +88,8 @@ public abstract class Activity {
|
||||
* sets the description.
|
||||
* @param description the description to be set.
|
||||
*/
|
||||
public String setDes(String description){
|
||||
Scanner scan = new Scanner(System.in);
|
||||
System.out.println("Please enter the description");
|
||||
description = scan.nextLine();
|
||||
scan.close();
|
||||
return description;
|
||||
public void setDescription(String description){
|
||||
this.description = description;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -91,6 +100,7 @@ public abstract class Activity {
|
||||
this.score = score;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* Returns a human readable object. It's in JSON.
|
||||
* @return the JSON form of the object.
|
||||
@@ -99,4 +109,26 @@ public abstract class Activity {
|
||||
public String toString() {
|
||||
return "Activity(id=" + this.id + ", name=" + this.name + ", description=" + this.description + ", score=" + this.score + ")";
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
if(other instanceof Activity){
|
||||
Activity that = (Activity)other;
|
||||
if(that.id != this.id)
|
||||
return false;
|
||||
if(!that.name.equals(this.name))
|
||||
return false;
|
||||
if(!that.description.equals(this.description))
|
||||
return false;
|
||||
if(that.score != this.score)
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, name, description, score);
|
||||
}
|
||||
}
|
||||
@@ -1,35 +1,44 @@
|
||||
package greenify.server.data.model;
|
||||
|
||||
import lombok.Data;
|
||||
import lombok.NoArgsConstructor;
|
||||
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
|
||||
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
import javax.persistence.*;
|
||||
import javax.validation.constraints.NotNull;
|
||||
import java.util.Objects;
|
||||
|
||||
@EnableAutoConfiguration
|
||||
@Entity
|
||||
@Data
|
||||
//@AllArgsConstructor
|
||||
@NoArgsConstructor
|
||||
@Table(name = "users")
|
||||
public class User {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
Long id;
|
||||
String name;
|
||||
String password;
|
||||
private Long id;
|
||||
|
||||
@NotNull
|
||||
private String name;
|
||||
|
||||
@NotNull
|
||||
private String password;
|
||||
|
||||
private int veganMeal;
|
||||
|
||||
public User() {}
|
||||
|
||||
/**
|
||||
* makes a user object.
|
||||
* @param id the id of the user.
|
||||
* @param name the supplied username
|
||||
* @param password the supplied password
|
||||
* @param veganMeal the supplied number of vegan meal
|
||||
*/
|
||||
public User(Long id, String name, String password) {
|
||||
public User(Long id, String name, String password, int veganMeal) {
|
||||
this.id = id;
|
||||
this.name = name;
|
||||
this.password = password;
|
||||
this.veganMeal = veganMeal;
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -40,6 +49,8 @@ public class User {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(Long id) { this.id = id; }
|
||||
|
||||
/**
|
||||
* gets the name.
|
||||
* @return the name
|
||||
@@ -48,6 +59,8 @@ public class User {
|
||||
return name;
|
||||
}
|
||||
|
||||
public void setName(String name) { this.name = name; }
|
||||
|
||||
/**
|
||||
* gets the password.
|
||||
* @return the password
|
||||
@@ -56,21 +69,68 @@ public class User {
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setId(Long id) {
|
||||
this.id = id;
|
||||
public void setPassword(String password) { this.password = password; }
|
||||
|
||||
/**
|
||||
* gets the number of vegan meal.
|
||||
* @return the veganMeal
|
||||
*/
|
||||
public int getVeganMeal() {
|
||||
return veganMeal;
|
||||
}
|
||||
|
||||
public void setName(String name) {
|
||||
this.name = name;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
public void setVeganMeal(int veganMeal) { this.veganMeal = veganMeal; }
|
||||
|
||||
/**
|
||||
* checks if two users are equal.
|
||||
* @param other the object to compare the user with
|
||||
* @return a boolean value of true if the user is equal to the object
|
||||
*/
|
||||
@Override
|
||||
public boolean equals(Object other) {
|
||||
|
||||
if (this == other) {
|
||||
return true;
|
||||
}
|
||||
if (other == null) {
|
||||
return false;
|
||||
}
|
||||
if (getClass() != other.getClass()) {
|
||||
return false;
|
||||
}
|
||||
if (other instanceof User) {
|
||||
User that = (User) other;
|
||||
return this.getName().equals(that.getName())
|
||||
&& this.getId().equals(that.getId())
|
||||
&& this.getPassword().equals(that.getPassword())
|
||||
&& this.getVeganMeal() == that.getVeganMeal();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* creates a string of the user object.
|
||||
* in the form of: User(id=, name=, password=, veganMeal=)
|
||||
* @return a string of the user object
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return "User(id="
|
||||
+ this.id
|
||||
+ ", name="
|
||||
+ this.name
|
||||
+ ", password="
|
||||
+ this.password
|
||||
+ ", veganMeal="
|
||||
+ this.veganMeal + ")";
|
||||
}
|
||||
|
||||
return "User(id=" + this.id + ", name=" + this.name + ", password=" + this.password + ")";
|
||||
/**
|
||||
* hashes the User object.
|
||||
* @return a hashcode for the user object
|
||||
*/
|
||||
@Override
|
||||
public int hashCode() {
|
||||
return Objects.hash(id, name, password, veganMeal);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +0,0 @@
|
||||
package greenify.server.service;
|
||||
|
||||
public class ActivityService {
|
||||
|
||||
|
||||
}
|
||||
@@ -26,7 +26,7 @@ public class UserService {
|
||||
if (user != null) {
|
||||
throw new ApplicationException("User already exists");
|
||||
} else {
|
||||
user = userRepository.save(new User(null, name, password));
|
||||
user = userRepository.save(new User(null, name, password, 0));
|
||||
}
|
||||
logger.info("Created user id=" + user.getId() + ", name=" + user.getName());
|
||||
return new UserDTO(user.getId(), user.getName());
|
||||
|
||||
48
src/Server/src/test/java/ActivityTest.java
Normal file
48
src/Server/src/test/java/ActivityTest.java
Normal file
@@ -0,0 +1,48 @@
|
||||
import greenify.server.data.model.Activity;
|
||||
import org.junit.Test;
|
||||
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
|
||||
public class ActivityTest {
|
||||
@Test
|
||||
public void setAndGetTest() {
|
||||
Activity activity = new Activity(1, "Vegetarian", "Eating", 100);
|
||||
Activity testActivity = new Activity(0, null, null, 0);
|
||||
testActivity.setId(1);
|
||||
testActivity.setName("Vegetarian");
|
||||
testActivity.setDescription("Eating");
|
||||
testActivity.setScore(100);
|
||||
assertEquals(activity.getId(), 1);
|
||||
assertEquals(activity.getName(), "Vegetarian");
|
||||
assertEquals(activity.getDescription(), "Eating");
|
||||
assertEquals(activity.getScore(), 100);
|
||||
assertEquals(activity, testActivity);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toStringTest() {
|
||||
Activity activity = new Activity(1, "Solar panels", "Installed", 10000);
|
||||
assertEquals("Activity(id=1, name=Solar panels, description=Installed, score=10000)", activity.toString());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsTest() {
|
||||
Activity first = new Activity(1, "Solar panels", "Installed", 10000);
|
||||
Activity second = new Activity(1, "Solar panels", "Installed", 10000);
|
||||
assertEquals(first.getId(), second.getId());
|
||||
assertEquals(first.getName(), second.getName());
|
||||
assertEquals(first.getDescription(), second.getDescription());
|
||||
assertEquals(first.getScore(), second.getScore());
|
||||
assertTrue(first.equals(second));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hashCodeTest() {
|
||||
Activity first = new Activity(1, "Solar panels", "Installed", 10000);
|
||||
Activity second = new Activity(1, "Solar panels", "Installed", 10000);
|
||||
assertEquals(first, second);
|
||||
assertEquals(first.hashCode(), second.hashCode());
|
||||
// assertTrue(first.hashCode() == second.hashCode());
|
||||
}
|
||||
}
|
||||
@@ -6,36 +6,51 @@ import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||
public class UserTest {
|
||||
@Test
|
||||
public void setAndGetTest() {
|
||||
User user = new User(1L, "greenify", "password");
|
||||
User testUser = new User(null, null, null);
|
||||
User user = new User(1L, "greenify", "password", 3);
|
||||
User testUser = new User();
|
||||
testUser.setId(1L);
|
||||
testUser.setName("greenify");
|
||||
testUser.setPassword("password");
|
||||
testUser.setVeganMeal(3);
|
||||
assertTrue(user.getId().equals(1L));
|
||||
assertEquals(user.getName(), "greenify");
|
||||
assertEquals(user.getPassword(), "password");
|
||||
assertEquals(user, testUser);
|
||||
assertEquals(user.getVeganMeal(), 3);
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsMethodTest() {
|
||||
User user = new User(1L, "greenify", "password", 3);
|
||||
User testUser = new User();
|
||||
testUser.setId(1L);
|
||||
testUser.setName("greenify");
|
||||
testUser.setPassword("password");
|
||||
testUser.setVeganMeal(3);
|
||||
assertTrue(user.equals(testUser));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void toStringTest() {
|
||||
User user = new User(1L, "greenify", "password");
|
||||
assertEquals("User(id=1, name=greenify, password=password)", user.toString());
|
||||
User user = new User(1L, "greenify", "password", 3);
|
||||
System.out.println("String is " + user.toString());
|
||||
assertTrue("User(id=1, name=greenify, password=password, veganMeal=3)".equals(user.toString()));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void equalsTest() {
|
||||
User first = new User(1L, "greenify", "password");
|
||||
User second = new User(1L, "greenify", "password");
|
||||
User first = new User(1L, "greenify", "password", 3);
|
||||
User second = new User(1L, "greenify", "password", 3);
|
||||
assertEquals(first.getId(), second.getId());
|
||||
assertEquals(first.getName(), second.getName());
|
||||
assertEquals(first.getPassword(), second.getPassword());
|
||||
assertEquals(first.getVeganMeal(), second.getVeganMeal());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void hashCodeTest() {
|
||||
User first = new User(1L, "greenify", "password");
|
||||
User second = new User(1L, "greenify", "password");
|
||||
User first = new User(1L, "greenify", "password", 3);
|
||||
User second = new User(1L, "greenify", "password", 3);
|
||||
assertTrue(first.equals(second) && second.equals(first));
|
||||
assertTrue(first.hashCode() == second.hashCode());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user