Skip to content

Commit

Permalink
finished retrieveCourses endpoint + unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pvd232 committed Sep 23, 2024
1 parent ccbe71c commit 2fb77d1
Show file tree
Hide file tree
Showing 7 changed files with 149 additions and 5 deletions.
13 changes: 13 additions & 0 deletions .idea/libraries/Maven__org_javatuples_javatuples_1_2.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

13 changes: 13 additions & 0 deletions .idea/libraries/Maven__org_json_json_20240303.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 10 additions & 0 deletions IndividualProject/citations.txt
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,13 @@

12. https://stackoverflow.com/questions/12190103/checkstyle-always-receive-file-contains-tab-characters-this-is-the-first-inst
Used to debug Maven style checker tab character error

13. https://www.baeldung.com/java-org-json
https://www.quora.com/Does-Java-have-a-built-in-JSON-library
JSON array deserialization

14. https://stackoverflow.com/questions/65171828/how-to-return-an-array-of-objects-in-json-format-in-spring-boot
Returning JSON array in Spring

15. https://www.baeldung.com/java-tuples
Using Java Tuples
11 changes: 11 additions & 0 deletions IndividualProject/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,17 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.javatuples</groupId>
<artifactId>javatuples</artifactId>
<version>1.2</version>
</dependency>
<!-- https://mvnrepository.com/artifact/org.json/json -->
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20240303</version>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents.client5</groupId>
<artifactId>httpclient5</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,17 @@ public int getNumberOfMajors() {
*
* @return The name of the department chair.
*/

public String getCode() {
return this.deptCode;
}

/**
* Gets the name of the department chair.
*
* @return The name of the department chair.
*/

public String getDepartmentChair() {
return this.departmentChair;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package dev.coms4156.project.individualproject;

import java.util.Locale;
import java.util.Map;
import java.util.*;

import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
Expand Down Expand Up @@ -98,6 +98,40 @@ public ResponseEntity<?> retrieveCourse(@RequestParam("deptCode") String deptCod
}
}


/**
* Displays the details of the requested course to the user or displays the proper error
* message in response to the request.
*
* @param courseCode A {@code int} representing the course the user wishes
* to retrieve.
*
* @return A {@code ResponseEntity} object containing either the details of the
* course and an HTTP 200 response or, an appropriate message indicating the
* proper response.
*/
@GetMapping(value = "/retrieveCourses", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> retrieveCourses(@RequestParam("courseCode") int courseCode) {
try {
Map<String, Department> departmentMapping;
departmentMapping = IndividualProjectApplication.myFileDatabase.getDepartmentMapping();
Department[] departments = departmentMapping.values().toArray(new Department[0]);
ArrayList<String> res = new ArrayList<>();
for (Department dept: departments)
if (dept.getCourseSelection().containsKey(Integer.toString(courseCode)))
res.add(dept.getCourseSelection().get(Integer.toString(courseCode)).toString()
+ "; Code: " + courseCode + "; Department: " + dept.getCode());

if (!res.isEmpty())
return new ResponseEntity<>(Arrays.asList(res.toArray()), HttpStatus.OK);
else
return new ResponseEntity<>("Course Not Found", HttpStatus.NOT_FOUND);
} catch (Exception e) {
return handleException(e);
}
}


/**
* Displays whether the course has at minimum reached its enrollmentCapacity.
*
Expand Down Expand Up @@ -440,7 +474,7 @@ public ResponseEntity<?> setEnrollmentCount(@RequestParam("deptCode") String dep
* successful, or an error message if the course is not found
*/
@PatchMapping(value = "/updateCourseTime", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> changeCourseTime(@RequestParam("deptCode") String deptCode,
public ResponseEntity<?> updateCourseTime(@RequestParam("deptCode") String deptCode,
@RequestParam("courseCode") int courseCode,
@RequestParam("time") String time) {
try {
Expand Down Expand Up @@ -478,7 +512,7 @@ public ResponseEntity<?> changeCourseTime(@RequestParam("deptCode") String deptC
* successful, or an error message if the course is not found
*/
@PatchMapping(value = "/updateCourseTeacher", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> changeCourseTeacher(@RequestParam("deptCode") String deptCode,
public ResponseEntity<?> updateCourseTeacher(@RequestParam("deptCode") String deptCode,
@RequestParam("courseCode") int courseCode,
@RequestParam("teacher") String teacher) {
try {
Expand Down Expand Up @@ -516,7 +550,7 @@ public ResponseEntity<?> changeCourseTeacher(@RequestParam("deptCode") String de
* @return a ResponseEntity with a success message if the operation is
*/
@PatchMapping(value = "/updateCourseLocation", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<?> changeCourseLocation(@RequestParam("deptCode") String deptCode,
public ResponseEntity<?> updateCourseLocation(@RequestParam("deptCode") String deptCode,
@RequestParam("courseCode") int courseCode,
@RequestParam("location") String location) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,14 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import org.javatuples.Pair;
import org.json.JSONArray;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -192,6 +198,52 @@ public void retrieveAllCoursesTest() {
}


/**
* Test for ("/retrieveCourse").
* This tests that each course exists, based on the data populated in the application file.
*/
@Test
public void retrieveCourseTest() {
Integer courseCode = 1004;
String url = "http://localhost:"
+ port + "/retrieveCourses"
+ "?courseCode=" + courseCode;
ResponseEntity<String> response = restTemplate.getForEntity(url, String.class);

assertEquals(HttpStatus.OK, response.getStatusCode());

List<Object> jArray = new JSONArray(response.getBody()).toList();
ArrayList<String> stringArray = new ArrayList<>();

for (Object item: jArray)
stringArray.add((String) item);

ArrayList<Pair<String, Integer>>correctResult = new ArrayList<>();

Map<String, Department> departmentMapping;
departmentMapping = IndividualProjectApplication.myFileDatabase.getDepartmentMapping();
Department[] departments = departmentMapping.values().toArray(new Department[0]);

assertTrue(departments.length > 0);

for (Department dept: departments)
if (dept.getCourseSelection().containsKey(courseCode.toString()))
correctResult.add(new Pair<>(dept.getCode(), courseCode) );

assertTrue(correctResult.size() > 0);

// For each item in response, ensure both the course code and dept code
// match an entry in the correctResult array
for (String resItem: stringArray) {
boolean found = false;
for (Pair<String, Integer> c: correctResult)
if (resItem.contains(c.getValue0()) && resItem.contains(c.getValue1().toString())) {
found = true;
break;
}
assertTrue(found);
}
}


/**
Expand Down

0 comments on commit 2fb77d1

Please sign in to comment.