Finish activity class and add tests

This commit is contained in:
Merel Steenbergen
2019-03-17 16:57:09 +01:00
parent 922f8b2657
commit dc6682d7fa
4 changed files with 98 additions and 49 deletions

View File

@@ -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) {
}
}

View File

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

View File

@@ -1,6 +0,0 @@
package greenify.server.service;
public class ActivityService {
}

View 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());
}
}