Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Development #20

Merged
merged 3 commits into from
Mar 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 13 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,25 @@
<div align="center">

## Bio Beacon
![SpringBoot](https://img.shields.io/badge/Spring_Boot-F2F4F9?style=for-the-badge&logo=spring-boot)

![Java](https://img.shields.io/badge/Java-ED8B00?style=for-the-badge&logo=openjdk&logoColor=white)
![AWS](https://img.shields.io/badge/Amazon_AWS-FF9900?style=for-the-badge&logo=amazonaws&logoColor=white)
![Cockroach](https://img.shields.io/badge/Cockroach%20Labs-6933FF?style=for-the-badge&logo=Cockroach%20Labs&logoColor=white)
![SpringBoot](https://img.shields.io/badge/Spring_Boot-F2F4F9?style=for-the-badge&logo=spring-boot)
![SpringSecurity](https://img.shields.io/badge/Spring_Security-6DB33F?style=for-the-badge&logo=Spring-Security&logoColor=white)
![SonarLint](https://img.shields.io/badge/SonarLint-CB2029?style=for-the-badge&logo=sonarlint&logoColor=white)
![Intellij](https://img.shields.io/badge/IntelliJ_IDEA-000000.svg?style=for-the-badge&logo=intellij-idea&logoColor=white)
![Cockroach](https://img.shields.io/badge/Cockroach%20Labs-6933FF?style=for-the-badge&logo=Cockroach%20Labs&logoColor=white)
![AWS](https://img.shields.io/badge/Amazon_AWS-FF9900?style=for-the-badge&logo=amazonaws&logoColor=white)
![GitHub Actions](https://img.shields.io/badge/github%20actions-%232671E5.svg?style=for-the-badge&logo=githubactions&logoColor=white)
![Intellij](https://img.shields.io/badge/IntelliJ_IDEA-000000.svg?style=for-the-badge&logo=intellij-idea&logoColor=white)
![SonarLint](https://img.shields.io/badge/SonarLint-CB2029?style=for-the-badge&logo=sonarlint&logoColor=white)

![Maven](https://badgen.net/maven/v/metadata-url/repo1.maven.org/maven2/com/google/code/gson/gson/maven-metadata.xml)
![WOrkFlow](https://github.com/NimsHub/BioBeacon-Api/actions/workflows/deployment.yml/badge.svg)
![WOrkFlow](https://github.com/NimsHub/BioBeacon-Api/actions/workflows/integration.yml/badge.svg)
![Maven Central](https://img.shields.io/maven-central/v/org.springframework.boot/spring-boot-starter-parent?label=Spring%20%20%20Boot&logo=spring%20boot&versionPrefix=3.0.2)
[![Java CI with Maven](https://github.com/NimsHub/BioBeacon-Api/actions/workflows/integration.yml/badge.svg)](https://github.com/NimsHub/BioBeacon-Api/actions/workflows/integration.yml)
[![Deploy to Beanstalk](https://github.com/NimsHub/BioBeacon-Api/actions/workflows/deployment.yml/badge.svg)](https://github.com/NimsHub/BioBeacon-Api/actions/workflows/deployment.yml)
<hr/>
<p>
Bio Beacon is a prooduct developed as an undergraduate project.
It is an Wireless Wearable Body Area Network which provides an IoT based

Bio Beacon is a product developed as an undergraduate project. It is a Wireless Wearable Body Area Network which provides an IoT based
Workload Management System.

</p>

</div>
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
import com.nimshub.biobeacon.athlete.dto.AthleteDetailsResponse;
import com.nimshub.biobeacon.athlete.dto.CreateAthleteRequest;
import com.nimshub.biobeacon.auth.AuthenticationResponse;
import com.nimshub.biobeacon.session.SessionController;
import lombok.RequiredArgsConstructor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -21,10 +20,11 @@
public class AthleteController {
private final AthleteService athleteService;
Logger logger = LoggerFactory.getLogger(AthleteController.class);

@PostMapping("/create-athlete")
public ResponseEntity<AuthenticationResponse> createAthlete(@RequestBody CreateAthleteRequest request) {
AuthenticationResponse authenticationResponse = athleteService.createAthlete(request);
logger.info("new athlete has been created from : {}",request);
logger.info("new athlete has been created from : {}", request);
return new ResponseEntity<>(authenticationResponse, HttpStatus.CREATED);
}

Expand All @@ -38,15 +38,15 @@ public ResponseEntity<List<AthleteDetailsResponse>> getAthletes() {
@GetMapping("/get-athlete")
public ResponseEntity<AthleteDetailsResponse> getAthlete(@RequestHeader("Authorization") String authHeader) {
AthleteDetailsResponse response = athleteService.getAthlete(authHeader);
logger.info("athlete details retrieved : {}",response);
logger.info("athlete details retrieved : {}", response);
return new ResponseEntity<>(response, HttpStatus.OK);
}

@GetMapping("/coach/{id}")
@PreAuthorize("hasAuthority('COACH')")
public ResponseEntity<List<AthleteDetailsResponse>> getAthletesByCoachId(@PathVariable UUID id) {
List<AthleteDetailsResponse> athletes = athleteService.getAthletesByCoachId(id);
logger.info("athletes of coach : {} retrieved",id);
logger.info("athletes of coach : {} retrieved", id);
return new ResponseEntity<>(athletes, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class AthleteService {

/**
* This method creates new Athlete
*
* @param request : CreateAthleteRequest
* @return : AuthenticationResponse
*/
Expand Down Expand Up @@ -62,6 +63,7 @@ public AuthenticationResponse createAthlete(CreateAthleteRequest request) {

/**
* This method retrieves all the Athletes
*
* @return : List<AthleteDetailsResponse>
*/
public List<AthleteDetailsResponse> getAthletes() {
Expand All @@ -86,6 +88,7 @@ public List<AthleteDetailsResponse> getAthletes() {

/**
* This method retrieves athlete details from the token
*
* @param authHeader : String
* @return : AthleteDetailsResponse
*/
Expand All @@ -112,6 +115,7 @@ public AthleteDetailsResponse getAthlete(String authHeader) {

/**
* This method retrieves athletes of specific Coach
*
* @param id : UUID
* @return : List<AthleteDetailsResponse>
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package com.nimshub.biobeacon.athlete.dto;

import com.nimshub.biobeacon.user.Gender;
import lombok.Builder;
import lombok.Data;

import java.time.LocalDate;
import java.util.UUID;

@Data
@Builder
public class CreateAthleteRequest {
private UUID coachId;
private String firstname;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class CoachController {
@PostMapping("/create-coach")
public ResponseEntity<AuthenticationResponse> createCoach(@RequestBody CreateCoachRequest request) {
AuthenticationResponse authenticationResponse = coachService.createCoach(request);
logger.info("new coach has been created from : {}",request);
logger.info("new coach has been created from : {}", request);
return new ResponseEntity<>(authenticationResponse, HttpStatus.CREATED);
}

Expand All @@ -36,7 +36,7 @@ public ResponseEntity<List<CoachDetailsResponse>> getCoaches() {
@GetMapping("/get-coach")
public ResponseEntity<CoachDetailsResponse> getCoach(@RequestHeader("Authorization") String authHeader) {
CoachDetailsResponse response = coachService.getCoach(authHeader);
logger.info("coach details retrieved : {}",response);
logger.info("coach details retrieved : {}", response);
return new ResponseEntity<>(response, HttpStatus.OK);
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
package com.nimshub.biobeacon.coach.dto;

import com.nimshub.biobeacon.user.Gender;
import lombok.Builder;
import lombok.Data;

import java.time.LocalDate;

@Data
@Builder
public class CreateCoachRequest {
private String firstname;
private String lastname;
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/com/nimshub/biobeacon/device/DeviceService.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ public class DeviceService {

/**
* This method creates new Device
*
* @return : Device
*/
public Device createDevice() {
Expand All @@ -24,6 +25,7 @@ public Device createDevice() {

/**
* This method retrieves all the devices
*
* @return : List<Device>
*/
public List<Device> getAllDevices() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ public ResponseEntity<Object> handleUsernameNotFoundException(UsernameNotFoundEx
public ResponseEntity<Object> handleAthleteNotFoundException(AthleteNotFoundException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
}

@ExceptionHandler(ExpiredJwtException.class)
public ResponseEntity<Object> handleExpiredJwtException(ExpiredJwtException e) {
return new ResponseEntity<>(e.getMessage(), HttpStatus.NOT_FOUND);
Expand Down
132 changes: 132 additions & 0 deletions src/test/java/com/nimshub/biobeacon/athlete/AthleteControllerTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
package com.nimshub.biobeacon.athlete;

import com.nimshub.biobeacon.athlete.dto.AthleteDetailsResponse;
import com.nimshub.biobeacon.athlete.dto.CreateAthleteRequest;
import com.nimshub.biobeacon.auth.AuthenticationResponse;
import com.nimshub.biobeacon.user.Gender;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;

import java.time.LocalDate;
import java.util.Collections;
import java.util.List;
import java.util.UUID;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

class AthleteControllerTest {
@Mock
private AthleteService athleteService;
@InjectMocks
private AthleteController athleteController;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);

}

private static AthleteDetailsResponse athleteDetailsResponse;

@BeforeAll
static void setUpData() {
athleteDetailsResponse = AthleteDetailsResponse.builder()
.id(UUID.randomUUID())
.coachId(UUID.randomUUID())
.firstname("John")
.lastname("Doe")
.email("john.doe@example.com")
.gender(Gender.MALE)
.dateOfBirth(LocalDate.of(1990, 1, 1))
.height(180.0)
.weight(80.0)
.mobile("1234567890")
.address("123.Main St")
.occupation(null)
.build();
}

@Test
void testCreateAthlete() {
// given
CreateAthleteRequest createAthleteRequest = CreateAthleteRequest.builder()
.email("test@example.com")
.password("password")
.firstname("John")
.lastname("Doe")
.gender(Gender.MALE)
.height(180.0)
.weight(70.0)
.dateOfBirth(LocalDate.of(1990, 1, 1))
.occupation("programmer")
.mobile("1234567890")
.address("123 Main St")
.coachId(UUID.randomUUID())
.build();

AuthenticationResponse authenticationResponse = new AuthenticationResponse("token");

when(athleteService.createAthlete(any())).thenReturn(authenticationResponse);

// when
ResponseEntity<AuthenticationResponse> response = athleteController.createAthlete(createAthleteRequest);

// then
verify(athleteService).createAthlete(createAthleteRequest);
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.CREATED);
assertThat(response.getBody()).isEqualTo(authenticationResponse);
}

@Test
void testGetAthletes() {
// given
when(athleteService.getAthletes()).thenReturn(Collections.singletonList(athleteDetailsResponse));

// when
ResponseEntity<List<AthleteDetailsResponse>> response = athleteController.getAthletes();

// then
verify(athleteService).getAthletes();
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.getBody()).isEqualTo(Collections.singletonList(athleteDetailsResponse));
}

@Test
void testGetAthlete() {
// given

when(athleteService.getAthlete(any())).thenReturn(athleteDetailsResponse);

// when
ResponseEntity<AthleteDetailsResponse> response = athleteController.getAthlete("Bearer token");

// then
verify(athleteService).getAthlete("Bearer token");
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.getBody()).isEqualTo(athleteDetailsResponse);
}

@Test
void testGetAthletesByCoachId() {
// given

when(athleteService.getAthletesByCoachId(any())).thenReturn(Collections.singletonList(athleteDetailsResponse));

// when
ResponseEntity<List<AthleteDetailsResponse>> response = athleteController.getAthletesByCoachId(UUID.randomUUID());

// then
verify(athleteService).getAthletesByCoachId(any());
assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK);
assertThat(response.getBody()).isEqualTo(Collections.singletonList(athleteDetailsResponse));
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,68 @@
package com.nimshub.biobeacon.athlete;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.UUID;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.when;

@DataJpaTest
class AthleteRepositoryTest {

@Mock
private AthleteRepository mockedAthleteRepository;

@BeforeEach
void setUp() {
MockitoAnnotations.openMocks(this);
}

@Test
void shouldFindByEmail() {
String email = "test@test.com";
Athlete athlete = Athlete.builder()
.id(UUID.randomUUID())
.email(email)
.build();

when(mockedAthleteRepository.findByEmail(email)).thenReturn(Optional.of(athlete));
Optional<Athlete> result = mockedAthleteRepository.findByEmail(email);
assertTrue(result.isPresent());
assertEquals(result.get().getEmail(), athlete.getEmail());
}

@Test
void shouldFindByCoachId() {
UUID coachId = UUID.randomUUID();
Athlete athlete1 = Athlete.builder()
.id(UUID.randomUUID())
.coachId(coachId)
.build();

Athlete athlete2 = Athlete.builder()
.id(UUID.randomUUID())
.coachId(coachId)
.build();

List<Athlete> athletes = new ArrayList<>();
athletes.add(athlete1);
athletes.add(athlete2);

when(mockedAthleteRepository.findByCoachId(coachId)).thenReturn(Optional.of(athletes));
Optional<List<Athlete>> result = mockedAthleteRepository.findByCoachId(coachId);

}
assertTrue(result.isPresent());
assertEquals(result.get().size(), athletes.size());
assertEquals(result.get().get(0).getCoachId(), athletes.get(0).getCoachId());
assertEquals(result.get().get(1).getCoachId(), athletes.get(1).getCoachId());
}
}
Loading