diff --git a/.github/.keep b/.github/.keep deleted file mode 100644 index e69de29..0000000 diff --git a/README.md b/README.md index 1a88a70..20c9d7c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,4 @@ +[![Review Assignment Due Date](https://classroom.github.com/assets/deadline-readme-button-24ddc0f5d75046c5622901739e7c5dd533143b0c8e959d652212380cedb1ea36.svg)](https://classroom.github.com/a/iDPpP-d0) # **Java API Assessment** ## **Introduction** diff --git a/README2.md b/README2.md new file mode 100644 index 0000000..7966db2 --- /dev/null +++ b/README2.md @@ -0,0 +1,61 @@ +# Activities of Daily Living Self Assessment + +A Spring Boot API for managing self assessment user details and returning useful information based on the input. + +## Description + +The SelfAssessmentAPI is an API that provides a platform for users to perform self-assessments based on a set of questions. The project includes classes for managing user details, a service for program functions, and controllers for handling HTTP requests. The API allows users to perform CRUD operations on self-assessment data. It includes features like user creation, updating, and retrieving useful information based on results. + +## Class structure + +### Getters and Setters + +SelfAssessmentUserDetails.java + +This class represents the details of a user performing a self-assessment. It includes attributes such as userID, name, yearOfBirth, contactNumber, and answers to specific assessment questions. The class also provides methods for retrieving and updating user details. + +Question.java + +This class represents a set of predefined questions for a self-assessment. It serves as a container for questions related to various aspects of an individual's physical care and accessibility. Each question is encapsulated as a private field with corresponding getter and setter methods. + +### Functions + +SelfAssessmentUserProgrammeFunctions.java + +This class contains methods for program functions related to the Self Assessment Programme. It handles the storage and retrieval of self-assessment questions and user details using JSON files. The class includes methods for adding, updating, and deleting user details. + +### Controllers + +SelfAssessmentController.java + +This class is a REST controller that handles HTTP requests related to self-assessment questions and user details. It includes endpoints for retrieving questions, retrieving user details, adding a new user assessment, updating user details, and deleting user details. + +SelfAssessmentResultsController.java + +The SelfAssessmentResultsController class is another REST controller responsible for handling requests related to self-assessment results. It includes an endpoint for retrieving results based on a user's ID and a specialized endpoint for obtaining the count of users over 65 who answered a specific question. + +### Service + +SelfAssessmentServiceForAPI.java + +This class serves as the service layer for the SelfAssessmentAPI, containing methods utilized by the controller class. It orchestrates the communication between the controller, program functions, and the searching algorithm. + +### Algorithm + +SearchingAlgorithm.java + +This class provides a method to count the number of users over the age of 65 who answered a specific question negatively. This class is designed to work with a list of SelfAssessmentUserDetails objects. The countOver65sReturningFalseToAnswer1 takes a list of user details as input, uses Java streams to filter users born in or before 1957 who answered a specific question (answer1) with false. The method then returns the count of matching users. + +### Programme + +SelfAssessmentUserProgramme.java + +This class serves as the entry point for the SelfAssessmentAPI application. It uses the Spring Boot @SpringBootApplication annotation to bootstrap the application, making it executable. The class also includes the main method (public static void main(String[] args)) responsible for launching the Spring Boot application. + +### Link to published PostMan Documentation + +[PostMan Documentation](https://documenter.getpostman.com/view/31214081/2s9Ye8fuov#97f450c0-bbdb-4c7e-b719-cf71bc4944f3) + +### Acknowledgments + +Thank you Coding Black Females for giving me the knowledge and inspiration to complete this project diff --git a/pom.xml b/pom.xml index d36be90..ed2f79e 100644 --- a/pom.xml +++ b/pom.xml @@ -5,7 +5,7 @@ org.springframework.boot spring-boot-starter-parent - 3.1.4 + 3.1.5 com.cbfacademy @@ -33,7 +33,13 @@ spring-boot-starter-test test - + + + com.google.code.gson + gson + 2.8.5 + + diff --git a/src/main/java/com/cbfacademy/apiassessment/App.java b/src/main/java/com/cbfacademy/apiassessment/App.java deleted file mode 100644 index 83b50f4..0000000 --- a/src/main/java/com/cbfacademy/apiassessment/App.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.cbfacademy.apiassessment; - -import org.springframework.boot.SpringApplication; -import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.web.bind.annotation.GetMapping; -import org.springframework.web.bind.annotation.RequestParam; -import org.springframework.web.bind.annotation.RestController; - -@SpringBootApplication -@RestController -public class App { - - public static void main(String[] args) { - SpringApplication.run(App.class, args); - } - - @GetMapping("/greeting") - public String greeting(@RequestParam(value = "name", defaultValue = "World") String name) { - return String.format("Hello %s", name); - } - -} diff --git a/src/main/java/com/cbfacademy/apiassessment/Question.java b/src/main/java/com/cbfacademy/apiassessment/Question.java new file mode 100644 index 0000000..a18131d --- /dev/null +++ b/src/main/java/com/cbfacademy/apiassessment/Question.java @@ -0,0 +1,65 @@ +package com.cbfacademy.apiassessment; + +public class Question { + + private String question1 = "Do you need physical care and support due to difficulties managing with any of the following; personal care, accessing the community, getting in and out of bed, drink and meal prep?"; + private String question2 = "Do you have any difficulties ascending and / or descending the stairs?"; + private String question3 = "Do you have any difficulties accessing your bathing facilities?"; + private String question4 = "Do you have difficulties accessing your property?"; + private String question5 = "Do you have difficulties transferring on and off the bed, chair, toilet, sofa?"; + + + public Question() { + this.question1 = "Do you need physical care and support due to difficulties managing with any of the following; personal care, accessing the community, getting in and out of bed, drink and meal prep?"; + this.question2 = "Do you have any difficulties ascending and / or descending the stairs? "; + this.question3 = "Do you have any difficulties accessing your bathing facilities?"; + this.question4 = "Do you have difficulties accessing your property? "; + this.question5 = "Do you have difficulties transferring on and off the bed, chair, toilet, sofa?"; + } + + public String getQuestion1() { + return this.question1; + } + + public void setQuestion1(String question1) { + this.question1 = question1; + } + + public String getQuestion2() { + return this.question2; + } + + public void setQuestion2(String question2) { + this.question2 = question2; + } + + public String getQuestion3() { + return this.question3; + } + + public void setQuestion3(String question3) { + this.question3 = question3; + } + + public String getQuestion4() { + return this.question4; + } + + public void setQuestion4(String question4) { + this.question4 = question4; + } + + public String getQuestion5() { + return this.question5; + } + + public void setQuestion5(String question5) { + this.question5 = question5; + } + + + + + + +} diff --git a/src/main/java/com/cbfacademy/apiassessment/SearchingAlgorithm.java b/src/main/java/com/cbfacademy/apiassessment/SearchingAlgorithm.java new file mode 100644 index 0000000..84e6a70 --- /dev/null +++ b/src/main/java/com/cbfacademy/apiassessment/SearchingAlgorithm.java @@ -0,0 +1,20 @@ +package com.cbfacademy.apiassessment; + +import java.util.List; + +import org.springframework.stereotype.Component; + +// method is taking in a list of SelfAssessmentUserDetails as input and using Java streams to filter users born <= 1957 and return false +// count function used to count matching users + +@Component +public class SearchingAlgorithm { + + public long countOver65sReturningFalseToAnswer1 (List userDetailsList) { + return userDetailsList.stream() + .filter(user -> user.getYearOfBirth() <= 1957). + filter(user -> !user.getAnswer1()) + .count(); + } + +} diff --git a/src/main/java/com/cbfacademy/apiassessment/SelfAssessmentController.java b/src/main/java/com/cbfacademy/apiassessment/SelfAssessmentController.java new file mode 100644 index 0000000..ab461d6 --- /dev/null +++ b/src/main/java/com/cbfacademy/apiassessment/SelfAssessmentController.java @@ -0,0 +1,90 @@ +package com.cbfacademy.apiassessment; + +import java.util.LinkedList; +// import java.util.UUID; + +// import javax.swing.RepaintManager; + +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.http.HttpStatus; +// import org.springframework.http.HttpStatus; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.DeleteMapping; +// import org.springframework.web.bind.annotation.DeleteMapping; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.PostMapping; +import org.springframework.web.bind.annotation.PutMapping; +import org.springframework.web.bind.annotation.RequestBody; +// import org.springframework.web.bind.annotation.PathVariable; +// import org.springframework.web.bind.annotation.PostMapping; +// import org.springframework.web.bind.annotation.PutMapping; +// import org.springframework.web.bind.annotation.RequestBody; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + + +@SpringBootApplication +@RestController +@RequestMapping("/api") + +public class SelfAssessmentController { + +// end points @GetMapping + SelfAssessmentServiceForAPI selfAssessmentService; + public SelfAssessmentController(SelfAssessmentServiceForAPI selfAssessmentService) { + this.selfAssessmentService = selfAssessmentService; + } + +// + +// sends a request to retreieve the json file with the self assessment questions + @GetMapping("/selfAssessmentQuestions") + public ResponseEntity > retrieveSelfAssessmentQuestions(){ + LinkedList selfAssessmentQuestions = selfAssessmentService.retrieveQuestions(); + return ResponseEntity.ok(selfAssessmentQuestions); + } + +// sends request to retreive the json file with self assessment data + @GetMapping("/selfAssessmentData") + public ResponseEntity > retrieveSelfAssessmentData(){ + LinkedList selfAssessment = selfAssessmentService.retrieveSelfAssessment(); + return ResponseEntity.ok(selfAssessment); + } + +// sends request to create a user self assessment following input into body + @PostMapping("/selfAssessmentData/addUser") + public ResponseEntity createUserDetails(@RequestBody SelfAssessmentUserDetails userDetails) { + selfAssessmentService.createUserSelfAssessment(userDetails); + String successMessage = "Self Assessment added successfully"; + return ResponseEntity.status(HttpStatus.CREATED).body(successMessage); + } + +// request to update user, takes in param userID , update json file in body + @PutMapping("/selfAssessmentData/userDetails/{userID}") + public ResponseEntity updateUserDetails(@PathVariable String userID, @RequestBody SelfAssessmentUserDetails userDetails) { + boolean updated = selfAssessmentService.updateUserSelfAssessment(userID, userDetails); + if (updated) { + String successMessage = "Self Assessment updated successfully"; + return ResponseEntity.ok(successMessage); + } else { + String errorMessage = "Self Assessment not found for userID: " + userID; + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorMessage); + } + } + +// request to delete user, takes in param userID , updates json file in body + @DeleteMapping("/selfAssessmentData/userDetails/{userID}") + public ResponseEntity deleteUserDetails(@PathVariable String userID) { + boolean deleted = selfAssessmentService.deleteUserSelfAssessment(userID); + if (deleted) { + String successMessage = "Self Assessment deleted successfully"; + return ResponseEntity.ok(successMessage); + } else { + String errorMessage = "Self Assessment not found for userID: " + userID; + return ResponseEntity.status(HttpStatus.NOT_FOUND).body(errorMessage); + } + } + + +} diff --git a/src/main/java/com/cbfacademy/apiassessment/SelfAssessmentResults.html b/src/main/java/com/cbfacademy/apiassessment/SelfAssessmentResults.html new file mode 100644 index 0000000..2e89302 --- /dev/null +++ b/src/main/java/com/cbfacademy/apiassessment/SelfAssessmentResults.html @@ -0,0 +1,47 @@ + + + + + + + SelfAssessment Results + + +

Useful Information based on your answers

+ + + + + diff --git a/src/main/java/com/cbfacademy/apiassessment/SelfAssessmentResultsController.java b/src/main/java/com/cbfacademy/apiassessment/SelfAssessmentResultsController.java new file mode 100644 index 0000000..d09cbe6 --- /dev/null +++ b/src/main/java/com/cbfacademy/apiassessment/SelfAssessmentResultsController.java @@ -0,0 +1,78 @@ +package com.cbfacademy.apiassessment; + +import java.util.Map; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.ResponseEntity; +import org.springframework.web.bind.annotation.GetMapping; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RestController; + +@RestController +@RequestMapping("api/selfAssessmentResults") +public class SelfAssessmentResultsController { + + @Autowired + private SelfAssessmentServiceForAPI selfAssessmentService; + + @GetMapping("/{userID}") + public ResponseEntity getResults(@PathVariable String userID) { + // Retrieve user's answers + Map userAnswers = selfAssessmentService.getUserAnswers(userID); + + // Generate content based on answers + String content = generateContent(userAnswers); + + return ResponseEntity.ok(content); + } + + + + private String generateContent(Map answers) { + // Logic to map answers to content + StringBuilder contentBuilder = new StringBuilder(); + if (!answers.get("answer1")) { + contentBuilder.append("Here is a useful link if you need physical care and support : [Find your local council to access Adult Social Care](https://www.gov.uk/apply-needs-assessment-social-services)\n"); + } + if (!answers.get("answer2")) { + contentBuilder.append("Here is a useful link if you are struggling with getting up and down the stairs (You can also try fitting a second stair rail): [Stair lift comparison website](https://www.mobilitycompare.co.uk/stairlifts/?show-form=true&msclkid=0cd3dda3797b131283eab8fa099456b0&utm_source=bing&utm_medium=cpc&utm_campaign=Stairlifts%20Generic&utm_term=stairlifts%20uk&utm_content=Core)\n"); + } + + if (!answers.get("answer3")) { + contentBuilder.append("Here is a useful link if you are struggling with accessing your bathing facilities: [Link to bathroom aids](https://www.careco.co.uk/bathroom/)\n"); + } + + if (!answers.get("answer4")) { + contentBuilder.append("Here is a useful link if you are struggling with accessing your property in any way: [How to ensure safe access link](https://livingmadeeasy.org.uk/dlf-factsheets/adapting-your-home-access-into-and-around-your-home)\n"); + } + + if (!answers.get("answer5")) { + contentBuilder.append("Here is a useful link if you are struggling with getting on and off your; bed, chair, toilet (Explore the bed rails , raisers and toileting equipment section): [Care Co link](https://www.careco.co.uk/furniture/furniture-accessories/furniture-raisers/)\n"); + } + + return contentBuilder.toString(); + } + + @GetMapping("/over65s") + public ResponseEntity algorithmGetOver65sAnswer1() { + // Call the service to get the count using the new algorithm + long count = selfAssessmentService.getOver65sAnswer1(); + // Return the count in the response + return ResponseEntity.ok(count); + + } + + + + + +} + + + + + + + + diff --git a/src/main/java/com/cbfacademy/apiassessment/SelfAssessmentServiceForAPI.java b/src/main/java/com/cbfacademy/apiassessment/SelfAssessmentServiceForAPI.java new file mode 100644 index 0000000..0e8df13 --- /dev/null +++ b/src/main/java/com/cbfacademy/apiassessment/SelfAssessmentServiceForAPI.java @@ -0,0 +1,95 @@ +package com.cbfacademy.apiassessment; + + +import java.util.LinkedList; +import java.util.List; +import java.util.Map; + +import org.springframework.stereotype.Service; + + +// Springboot service layer containing method the the controller class. + +@Service +public class SelfAssessmentServiceForAPI { + + + private final SelfAssessmentUserProgrammeFunctions programmeFunctions; + private final SearchingAlgorithm searchingAlgorithm; + + + public SelfAssessmentServiceForAPI(SelfAssessmentUserProgrammeFunctions programmeFunctions, SearchingAlgorithm searchingAlgorithm) { + this.programmeFunctions = programmeFunctions; + this.searchingAlgorithm = searchingAlgorithm; + } + + public LinkedList retrieveQuestions() { + return programmeFunctions.retrieveSelfAssessmentQuestions(); + } + + public LinkedList retrieveSelfAssessment() { + return programmeFunctions.retrieveSelfAssessments(); + } + + public void createUserSelfAssessment(SelfAssessmentUserDetails userDetails) { + programmeFunctions.add(userDetails); + } + + public boolean updateUserSelfAssessment(String userID, SelfAssessmentUserDetails userDetails) { + return programmeFunctions.updateUserDetails(userID, userDetails); + } + + public boolean deleteUserSelfAssessment(String userID) { + + return programmeFunctions.deleteUserDetails(userID); + } + + public Map getUserAnswers(String userID) { + SelfAssessmentUserDetails userDetails = programmeFunctions.getUserDetails(userID); + + return userDetails.getAnswers(); + } + + public long getOver65sAnswer1() { + List allUsers = programmeFunctions.retrieveSelfAssessments(); + return searchingAlgorithm.countOver65sReturningFalseToAnswer1(allUsers); + } + +} + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/main/java/com/cbfacademy/apiassessment/SelfAssessmentUserDetails.java b/src/main/java/com/cbfacademy/apiassessment/SelfAssessmentUserDetails.java new file mode 100644 index 0000000..81be3ca --- /dev/null +++ b/src/main/java/com/cbfacademy/apiassessment/SelfAssessmentUserDetails.java @@ -0,0 +1,178 @@ +package com.cbfacademy.apiassessment; + +import java.util.HashMap; +import java.util.Map; + +import com.fasterxml.jackson.annotation.JsonIgnore; + +// class containing getters and setters for to take input of values + +public class SelfAssessmentUserDetails { + + private String userID; + private String name; + private int yearOfBirth; + private String contactNumber; + private Boolean answer1; + private Boolean answer2; + private Boolean answer3; + private Boolean answer4; + private Boolean answer5; + + // default constructor + // non of my internal values are being processed + // need to set default values + + public SelfAssessmentUserDetails() {} + + + public SelfAssessmentUserDetails(String userID, String name, int yearOfBirth, String contactNumber, boolean answer1, boolean answer2, boolean answer3, boolean answer4, boolean answer5) { +// this.userID = userID set as UUID ? this.uuid = uuid.randomid this.userID = UUID.randomUUID(); +// randomUUID method. change + + this.userID = userID; + this.name = name; + this.yearOfBirth = yearOfBirth; + this.contactNumber = contactNumber; + this.answer1 = answer1; + this.answer2 = answer2; + this.answer3 = answer3; + this.answer4 = answer4; + this.answer5 = answer5; + + } + + + public String getUserID() { + return userID; + } + + // wouldnt want to have a setter as you wouldnt want it to be changed. + // public void setUserID(String userID) { + // this.userID = userID; + + + // get method + public String getName() { + return name; + } + // set method to allow value of string to be passed as firstName + // contructor for method to allow string to be inputted as value + public void setName(String name) { + this.name = name; + } + + public int getYearOfBirth() { + return yearOfBirth; + } + public void setYearOfBirth(int yearOfBirth) { + this.yearOfBirth = yearOfBirth; + } + + + public String getContactNumber() { + return contactNumber; + } + public void setContactNumber(String contactNumber) { + this.contactNumber = contactNumber; + } + + // change these to post questions + + // @GetMapping so the user can see the questions "get the questions" + + public boolean getAnswer1() { + return answer1; + } + + public void setAnswer1(boolean answer1) { + this.answer1 = answer1; + } + + + public boolean getAnswer2() { + return answer2; + } + public void setAnswer2(boolean answer2) { + this.answer2 = answer2; + } + + + public boolean getAnswer3() { + return answer3; + } + public void setAnswer3(boolean answer3) { + this.answer3 = answer3; + } + + + public boolean getAnswer4() { + return answer4; + } + public void setAnswer4(boolean answer4) { + this.answer4 = answer4; + } + + + public boolean getAnswer5() { + return answer5; + } + public void setAnswer5(boolean answer5) { + this.answer5 = answer5; + } + + + @JsonIgnore + public Map getAnswers() { + Map answers = new HashMap<>(); + answers.put("answer1", this.answer1); + answers.put("answer2", this.answer2); + answers.put("answer3", this.answer3); + answers.put("answer4", this.answer4); + answers.put("answer5", this.answer5); + + return answers; + } + + + // toString merthod to return list of values inputted when called + @Override public String toString() { + + return "Self assessment details - { " + userID + " , " + name + " , " + yearOfBirth + " , " + contactNumber + " , " + answer1 + " , " + answer2 + " , " + answer3 + " , " + answer4 + " , " + answer5 + " }"; + } + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +} + + diff --git a/src/main/java/com/cbfacademy/apiassessment/SelfAssessmentUserProgramme.java b/src/main/java/com/cbfacademy/apiassessment/SelfAssessmentUserProgramme.java new file mode 100644 index 0000000..af21136 --- /dev/null +++ b/src/main/java/com/cbfacademy/apiassessment/SelfAssessmentUserProgramme.java @@ -0,0 +1,24 @@ +package com.cbfacademy.apiassessment; + +// import java.util.Scanner; +// import java.util.UUID; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; +import org.springframework.web.bind.annotation.RestController; + + +@SpringBootApplication +@RestController +@ComponentScan("com.cbfacademy.apiassessment") + +public class SelfAssessmentUserProgramme { + + + public static void main(String[] args) { + SpringApplication.run(SelfAssessmentUserProgramme.class , args); + } + +} + diff --git a/src/main/java/com/cbfacademy/apiassessment/SelfAssessmentUserProgrammeFunctions.java b/src/main/java/com/cbfacademy/apiassessment/SelfAssessmentUserProgrammeFunctions.java new file mode 100644 index 0000000..e65cd6b --- /dev/null +++ b/src/main/java/com/cbfacademy/apiassessment/SelfAssessmentUserProgrammeFunctions.java @@ -0,0 +1,180 @@ +package com.cbfacademy.apiassessment; + +import com.google.gson.reflect.TypeToken; + +import java.io.FileNotFoundException; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.Reader; +import java.io.Writer; +import java.lang.reflect.Type; +import java.util.Collections; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.Map; + +import org.springframework.stereotype.Component; +import org.springframework.util.ResourceUtils; + +import com.google.gson.Gson; +import com.google.gson.GsonBuilder; + +@Component + +// this class contains methods for functions for the Self Assessment Programme + +public class SelfAssessmentUserProgrammeFunctions { + + private LinkedList questions; + private LinkedList userDetailsInput; + + private String jsonFilePathQuestions; + private String jsonFilePathUserDetails; + private final Gson gson; + + public SelfAssessmentUserProgrammeFunctions() { + try { + jsonFilePathQuestions = ResourceUtils.getFile("classpath:selfAssessmentQuestions.json").getAbsolutePath(); + jsonFilePathUserDetails = ResourceUtils.getFile("classpath:selfAssessmentData.json").getAbsolutePath(); + } + catch (FileNotFoundException e) { + jsonFilePathQuestions = "selfAssessmentQuestions.json"; + jsonFilePathUserDetails = "selfAssessmentData.json"; + } + + gson = new GsonBuilder().setPrettyPrinting().create(); + questions = readDataFromFile(); + userDetailsInput = readUserDetailsDataFromFile(); + } + + public LinkedList retrieveSelfAssessmentQuestions() { + return questions; + } + + public LinkedList retrieveSelfAssessments() { + return userDetailsInput; + } + + public void add(SelfAssessmentUserDetails recoUserDetails) { + + if (!find(recoUserDetails.getUserID())) { + userDetailsInput.add(recoUserDetails); + writeDataToSelfAssessmentFile(); + } + else { + System.out.println("Self Assessment Record already exists"); + } + } + + public boolean find(String userID) { + return userDetailsInput.stream().anyMatch(l -> l.getUserID().equals(userID)); + } + + + // code below is reading data from the JSON file and returning as a list + private LinkedList readDataFromFile() { + try(Reader reader = new FileReader(jsonFilePathQuestions)) { + Type listType = new TypeToken>() { + }.getType(); + return gson.fromJson(reader, listType); + // if file does not exist returns an empty list + } catch (FileNotFoundException e) { + return new LinkedList<>(); + } catch (IOException e) { + e.printStackTrace(); + return new LinkedList<>(); + } + } + + private LinkedList readUserDetailsDataFromFile() { + try(Reader reader2 = new FileReader(jsonFilePathUserDetails)) { + Type listType2 = new TypeToken>() { + }.getType(); + return gson.fromJson(reader2, listType2); + // if file does not exist returns an empty list + } catch (FileNotFoundException e) { + return new LinkedList<>(); + } catch (IOException e) { + e.printStackTrace(); + return new LinkedList<>(); + } + } + + private void writeDataToSelfAssessmentFile() { + try(Writer writer = new FileWriter(jsonFilePathUserDetails)){ + gson.toJson(userDetailsInput, writer); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public boolean updateUserDetails(String userID, SelfAssessmentUserDetails userDetails) { + for(SelfAssessmentUserDetails user : userDetailsInput) { + // if userID provided == userID within json file then update the user details + if (user.getUserID().equals(userID)) { + user.setName(userDetails.getName()); + user.setYearOfBirth(userDetails.getYearOfBirth()); + user.setContactNumber(userDetails.getContactNumber()); + user.setAnswer1(userDetails.getAnswer1()); + user.setAnswer2(userDetails.getAnswer2()); + user.setAnswer3(userDetails.getAnswer3()); + user.setAnswer4(userDetails.getAnswer4()); + user.setAnswer5(userDetails.getAnswer5()); + writeDataToSelfAssessmentFile(); + return true; + } + } return false; + // returns false if no match is found after the loop + } + + public boolean deleteUserDetails(String userID) { + // Iterator method allows to iterate over the elements in the userDetailsInput (list), contains there methods within the Iterator Class; hasNext() , next() , remove() to traverse LinkedList + Iterator iterator = userDetailsInput.iterator(); + while (iterator.hasNext()) { + SelfAssessmentUserDetails user = iterator.next(); + if (user.getUserID().equals(userID)) { + iterator.remove();; + writeDataToSelfAssessmentFile(); + return true; + } + } return false; + } + + public SelfAssessmentUserDetails getUserDetails(String userID) { + // Logic to retrieve user details based on userID + for (SelfAssessmentUserDetails user : userDetailsInput) { + if (user.getUserID().equals(userID)) { + return user; + } + } return null; + } + + public Map getUserAnswers(String userID) { + SelfAssessmentUserDetails userDetails = getUserDetails(userID); + + if (userDetails != null) { + // Assuming getAnswers returns a Map + return userDetails.getAnswers(); + } else { + // Handle the case when user details are not found + return Collections.emptyMap(); // Or throw an exception + } + } + +} + + + + + + + + + + + + + + + diff --git a/src/main/resources/selfAssessmentData.json b/src/main/resources/selfAssessmentData.json new file mode 100644 index 0000000..0523f63 --- /dev/null +++ b/src/main/resources/selfAssessmentData.json @@ -0,0 +1,27 @@ +[ + { + "userID": "Jane7" , + "name": "Jane Doe", + "yearOfBirth": 1934, + "contactNumber": "07497654321", + "answer1": false, + "answer2": false, + "answer3": false, + "answer4": false, + "answer5": false + }, + + { + "userID": "John7" , + "name": "John Doe", + "yearOfBirth": 1930, + "contactNumber": "07634608932", + "answer1": false, + "answer2": true, + "answer3": false, + "answer4": true, + "answer5": true + } +] + + diff --git a/src/main/resources/selfAssessmentQuestions.json b/src/main/resources/selfAssessmentQuestions.json new file mode 100644 index 0000000..a52b178 --- /dev/null +++ b/src/main/resources/selfAssessmentQuestions.json @@ -0,0 +1,10 @@ +[ + { + "question 1": "Do you need physical care and support due to difficulties managing with any of the following; personal care, accessing the community, getting in and out of bed, drink and meal prep?" , + "question 2": "Do you have any difficulties ascending and / or descending the stairs? ", + "question 3": "Do you have any difficulties accessing your bathing facilities?", + "question 4": "Do you have difficulties accessing your property? ", + "question 5": "Do you have difficulties transferring on and off the bed, chair, toilet, sofa?" + } + +] \ No newline at end of file diff --git a/src/test/java/com/cbfacademy/apiassessment/AppTests.java b/src/test/java/com/cbfacademy/apiassessment/AppTests.java deleted file mode 100644 index c551ee8..0000000 --- a/src/test/java/com/cbfacademy/apiassessment/AppTests.java +++ /dev/null @@ -1,49 +0,0 @@ -package com.cbfacademy.apiassessment; - -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.Test; -import org.springframework.beans.factory.annotation.Autowired; -import org.springframework.boot.test.context.SpringBootTest; -import org.springframework.boot.test.web.client.TestRestTemplate; -import org.springframework.boot.test.web.server.LocalServerPort; -import org.springframework.context.annotation.Description; -import org.springframework.http.ResponseEntity; - -import java.net.URL; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -@SpringBootTest(classes = App.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) -class AppTests { - - @LocalServerPort - private int port; - - private URL base; - - @Autowired - private TestRestTemplate restTemplate; - - @BeforeEach - public void setUp() throws Exception { - this.base = new URL("http://localhost:" + port + "/greeting"); - } - - @Test - @Description("/greeting endpoint returns expected response for default name") - public void greeting_ExpectedResponseWithDefaultName() { - ResponseEntity response = restTemplate.getForEntity(base.toString(), String.class); - - assertEquals(200, response.getStatusCode().value()); - assertEquals("Hello World", response.getBody()); - } - - @Test - @Description("/greeting endpoint returns expected response for specified name parameter") - public void greeting_ExpectedResponseWithNameParam() { - ResponseEntity response = restTemplate.getForEntity(base.toString() + "?name=John", String.class); - - assertEquals(200, response.getStatusCode().value()); - assertEquals("Hello John", response.getBody()); - } -} diff --git a/src/test/java/com/cbfacademy/apiassessment/SelfAssessmentUserDetailsTest.java b/src/test/java/com/cbfacademy/apiassessment/SelfAssessmentUserDetailsTest.java new file mode 100644 index 0000000..4099ccc --- /dev/null +++ b/src/test/java/com/cbfacademy/apiassessment/SelfAssessmentUserDetailsTest.java @@ -0,0 +1,187 @@ +package com.cbfacademy.apiassessment; + +import static org.junit.jupiter.api.Assertions.assertEquals; + + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +@DisplayName(value = "The Self Assessment User Details class should:") +public class SelfAssessmentUserDetailsTest { + + SelfAssessmentUserDetails userDetails = new SelfAssessmentUserDetails("123", "Jane Doe", 2000, "07590673402", false, false, true, true, true); +// Below I am testing the get() method for returning the values; firstName, lastName, yearOfBirth, answer1, answer2, answer3 , answer 4 , answer 5. + + + @Test + @DisplayName("return userID when getuserID is used") + public void testGetUserID() { + + String userID = userDetails.getUserID(); + assertEquals("123", userID); + } + + @Test + @DisplayName("returns firstName when get firstName is used") + public void testGetName() { + String name = userDetails.getName(); + assertEquals("Jane Doe", name); + } + + + @Test + @DisplayName("return yearOfBirth when getYearOfBirth is used") + public void testYearOfBirth() { + + int yearOfBirth = userDetails.getYearOfBirth(); + assertEquals(2000 , yearOfBirth); + } + + + @Test + @DisplayName("return contactNumber when getContactNumber is used") + public void testGetContactNumber() { + + String contactNumber = userDetails.getContactNumber(); + assertEquals("07590673402", contactNumber); + } + + + @Test + @DisplayName("return answer1 when getAnswer1 is used") + public void testGetAnswer1() { + + boolean answer1 = userDetails.getAnswer1(); + assertEquals(false, answer1); + } + + @Test + @DisplayName("return answer2 when getAnswer2 is used") + public void testGetAnswer2() { + + boolean answer2 = userDetails.getAnswer2(); + assertEquals(false, answer2); + } + + @Test + @DisplayName("return answer3 when getAnswer3 is used") + public void testGetAnswer3() { + + boolean answer3 = userDetails.getAnswer3(); + assertEquals(true, answer3); + } + + @Test + @DisplayName("return answer4 when getAnswer4 is used") + public void testGetAnswer4() { + + boolean answer4 = userDetails.getAnswer4(); + assertEquals(true, answer4); + } + + @Test + @DisplayName("return answer5 when getAnswer5 is used") + public void testGetAnswer5() { + + boolean answer5 = userDetails.getAnswer3(); + assertEquals(true, answer5); + } + + +// below i am running tests to test the set methods from the Self Assessment user details class + + + + + @Test + @DisplayName("sets value of first name when setFirstName is used") + public void testSetName() { + + userDetails.setName("Chella"); + + String name = userDetails.getName(); + assertEquals("Chella", name); + } + + @Test + @DisplayName("sets value of year of birth when setYearOfBirth is used") + public void testSetYearOfBirth() { + + userDetails.setYearOfBirth(1998); + int yearOfBirth = userDetails.getYearOfBirth(); + assertEquals(1998, yearOfBirth); + } + + @Test + @DisplayName("sets value of year of birth when setYearOfBirth is used") + public void testSetContactNumber() { + + userDetails.setContactNumber("07490571681"); + String contactNumber = userDetails.getContactNumber(); + assertEquals("07490571681", contactNumber); + } + + @Test + @DisplayName("set value of answer to question one when setAnswer1 is used") + public void testSetAnswer1() { + + userDetails.setAnswer1(false); + boolean answer1 = userDetails.getAnswer1(); + assertEquals(false, answer1); + } + + @Test + @DisplayName("set value of answer to question true when setAnswer1 is used") + public void testSetAnswer2() { + + userDetails.setAnswer2(true); + boolean answer2 = userDetails.getAnswer2(); + assertEquals(true, answer2); + } + + @Test + @DisplayName("set value of answer to question three when setAnswer1 is used") + public void testSetAnswer3() { + + userDetails.setAnswer3(false); + boolean answer3 = userDetails.getAnswer3(); + assertEquals(false, answer3); + } + + @Test + @DisplayName("set value of answer to question four when setAnswer1 is used") + public void testSetAnswer4() { + + userDetails.setAnswer4(true); + boolean answer4 = userDetails.getAnswer4(); + assertEquals(true, answer4); + } + + @Test + @DisplayName("set value of answer to question five when setAnswer1 is used") + public void testSetAnswer5() { + + userDetails.setAnswer5(true); + boolean answer5 = userDetails.getAnswer5(); + assertEquals(true, answer5); + + } + + +// test that the toString method returns the user inputted recorded. + @Test + @DisplayName("tests the toString method to return the values inputted when called") + public void testToString() { + String expected = "Self assessment details - {" + " 123 , Jane Doe , 2000 , 07590673402 , false , false , true , true , true }"; + String actual = userDetails.toString(); + + assertEquals(expected, actual); + } + +} + + + + + + diff --git a/src/test/java/com/cbfacademy/apiassessment/SelfAssessmentUserProgrammeFunctionsTest.java b/src/test/java/com/cbfacademy/apiassessment/SelfAssessmentUserProgrammeFunctionsTest.java new file mode 100644 index 0000000..0416860 --- /dev/null +++ b/src/test/java/com/cbfacademy/apiassessment/SelfAssessmentUserProgrammeFunctionsTest.java @@ -0,0 +1,169 @@ +package com.cbfacademy.apiassessment; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertTrue; + + +import java.util.LinkedList; +import java.util.Map; + +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + + +@DisplayName(value = "The Self Assessment Programmes function class should:") + +public class SelfAssessmentUserProgrammeFunctionsTest { + + private SelfAssessmentUserProgrammeFunctions functions; + SelfAssessmentUserDetails userDetails = new SelfAssessmentUserDetails("TestUser", "Test User", 1990, "123456", true, false, true, false, true); + +// with the @BeforeEach annotation line 13 will be initialised before each test + @BeforeEach + + public void setUp() { + functions = new SelfAssessmentUserProgrammeFunctions(); + } + + @Test + @DisplayName("Test the retrieveSelfAssessmentQuestions method does not return null value") + public void testRetrieveSelfAssessmentQuestions() { + LinkedList questions = functions.retrieveSelfAssessmentQuestions(); + assertNotNull(questions); + } + + @Test + @DisplayName("Test the retrieveSelfAssessment method does not return null value") + public void testRetrieveSelfAssessment() { + LinkedList userDetails = functions.retrieveSelfAssessments(); + assertNotNull(userDetails); + } + + @Test + @DisplayName("Test the add method adds user details") + public void testAdd() { + // create an instance of the user detail class + functions.add(userDetails); + LinkedList userDetailsList = functions.retrieveSelfAssessments(); + // + assertTrue(userDetailsList.stream().anyMatch(user -> user.getUserID().equals("TestUser"))); + } + + @Test + @DisplayName("Test the find method correctly identifies the added user") + public void testFind() { + functions.add(userDetails); + assertTrue(functions.find("TestUser")); + assertFalse(functions.find("FakeUserID")); + } + + @Test + @DisplayName("Test the delete method correctly delete user based on usertID") + public void testDeleteUserDetails() { + functions.add(userDetails); + boolean deleted = functions.deleteUserDetails("TestUser"); + assertTrue(deleted); + } + + @Test + @DisplayName("Test getUserDetails method returns list of selfAssesment input") + public void testGetUserDetails() { + // Arrange: Create a sample user + functions.retrieveSelfAssessments().add(userDetails); + + // Act: Call the method + SelfAssessmentUserDetails actualUser = functions.getUserDetails("TestUser"); + + // Assert: Check if the returned user matches the expected user + assertEquals(userDetails, actualUser); + } + + @Test + @DisplayName("Test getUserAnswers method returns list of users answers") + public void testGetUserAnswers() { + // Arrange: Create a sample user + functions.retrieveSelfAssessments().add(userDetails); + + // Act: Call the method + Map actualAnswers = functions.getUserAnswers("TestUser"); + + // Assert: Check if the returned answers match the expected answers + assertEquals(userDetails.getAnswers(), actualAnswers); + } + + @Test + @DisplayName("Test updateUserDetails method updates user if userID does not exist ") + public void testUpdateUserDetails() { + SelfAssessmentUserDetails existingUser = new SelfAssessmentUserDetails("UserID3", "Alice Johnson", 1985, "789", true, false, true, false, true); + functions.retrieveSelfAssessments().add(existingUser); + + // Arrange: Create updated details + SelfAssessmentUserDetails updatedDetails = new SelfAssessmentUserDetails("UserID3", "Alice Smith", 1985, "789", false, true, false, true, false); + + // Act: Call the method to update user details + boolean result = functions.updateUserDetails("UserID3", updatedDetails); + + // Assert: Check if the update was successful + assertTrue(result); + + // Assert: Check if the user details are updated + LinkedList userList = functions.retrieveSelfAssessments(); + SelfAssessmentUserDetails updatedUser = userList.stream().filter(user -> user.getUserID().equals("UserID3")).findFirst().orElse(null); + assertNotNull(updatedUser); + assertEquals("Alice Smith", updatedUser.getName()); + assertFalse(updatedUser.getAnswer1()); + } + + + +} + + // cant create tests from methods to readfilefromjson and writefiletojson + + // @Test + // @DisplayName("Test the readDataFromFile method correctly reads data from json file") + // public void testReadDataFromFile(){ + // } + + // @Test + // @DisplayName("Test readUserDetailsDataFromFile method with different scenarios") + // public void testReadUserDetailsDataFromFile(){ + // } + + // @Test + // @DisplayName("Test write data to self assessment file") + // public void testWriteDataToSelfAssessmentFile() throws IOException { + // } + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +