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

Test Coverage #8

Open
wants to merge 9 commits into
base: main
Choose a base branch
from
Open
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
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>

<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.gamedoora.backend.studioservices.api;

import com.gamedoora.backend.studioservices.assembler.StudioServicesAssembler;
import com.gamedoora.backend.studioservices.exceptions.BadRequestException;
import com.gamedoora.backend.studioservices.exceptions.NotFoundException;
import com.gamedoora.model.dao.Studios;
import com.gamedoora.model.dao.Users;
Expand All @@ -10,15 +11,7 @@
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
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.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.*;

import java.text.MessageFormat;
import java.util.List;
Expand Down Expand Up @@ -112,4 +105,28 @@ public ResponseEntity<List<StudiosDTO>> getAllStudiosByUsers(@RequestParam(requi
public ResponseEntity<List<StudiosDTO>> getAllStudiosByUsersFirstName(@RequestParam(required = false) String user_name) {
return createResponse(studioServicesAssembler.getAllStudiosByUsersFirstName(user_name), HttpStatus.OK);
}

// Cre
@GetMapping(
value = "studio/users/{lastName}",
produces = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<List<StudiosDTO>> getAllStudiosByUsersLastName(@RequestParam(required = false) String user_lastName){
if (user_lastName == null || user_lastName.isEmpty()) {
throw new BadRequestException("User last name is required.");
}
return createResponse(studioServicesAssembler.getAllStudiosByUsersLastName(user_lastName), HttpStatus.OK);
}

@GetMapping(
value = "studio/users/{phoneNumber}",
produces = {MediaType.APPLICATION_JSON_VALUE})
public ResponseEntity<List<StudiosDTO>> getAllStudiosByUsersPhoneNumber(@RequestParam(required = false) String userPhoneNumber){
if (userPhoneNumber == null || userPhoneNumber.isEmpty())
{
throw new BadRequestException("User phone number is required");
}
return createResponse(studioServicesAssembler.getAllStudiosByPhoneNumber(userPhoneNumber), HttpStatus.OK);
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ public List<StudiosDTO> getAllStudiosByVisibility(boolean visible) {
public List<StudiosDTO> getAllStudiosByCommunity(int community) {
List<StudiosDTO> studiosDto = new ArrayList<>();
if (community == 1) {
studioRepository.findByCommunity(community).forEach(studio -> studiosDto.add(getStudioMapper().studiosToStudiosDto(studio)));
studioRepository.findByIsCommunity(community).forEach(studio -> studiosDto.add(getStudioMapper().studiosToStudiosDto(studio)));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Observational: Returning the list within the if block can remove the need for else block.

} else {
studioRepository.findAll().forEach(studio -> studiosDto.add(getStudioMapper().studiosToStudiosDto(studio)));
}
Expand Down Expand Up @@ -134,4 +134,22 @@ public List<StudiosDTO> getAllStudiosByUsersFirstName(String name){
}
return (studiosDto.isEmpty() ? null : studiosDto);
}

public List<StudiosDTO> getAllStudiosByUsersLastName(String lastName){
List<StudiosDTO> studiosDto = new ArrayList<>();
studioRepository.findByUsersSet_LastName(lastName).forEach(studio -> studiosDto.add(getStudioMapper().studiosToStudiosDto(studio)));
if(studiosDto.isEmpty()) {
studioRepository.findAll().forEach(studios -> studiosDto.add(getStudioMapper().studiosToStudiosDto(studios)));
}
return (studiosDto.isEmpty() ? null : studiosDto);
}

public List<StudiosDTO> getAllStudiosByPhoneNumber(String phoneNumber){
List<StudiosDTO> studiosDto = new ArrayList<>();
studioRepository.findByUsersSet_PhoneNumber(phoneNumber).forEach(studio -> studiosDto.add(getStudioMapper().studiosToStudiosDto(studio)));
if(studiosDto.isEmpty()) {
studioRepository.findAll().forEach(studios -> studiosDto.add(getStudioMapper().studiosToStudiosDto(studios)));
}
return (studiosDto.isEmpty() ? null : studiosDto);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,4 @@ public BadRequestException(String message) {
public BadRequestException(String message, Throwable cause) {
super(message, cause);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,18 @@ public interface StudioRepository extends JpaRepository<Studios , Long> {

List<Studios> findByVisibility(boolean visibility);

List<Studios> findByCommunity(int community);
List<Studios> findByIsCommunity(int community);

List<Studios> findByRegistration(boolean registration);

List<Studios> findByUsersSet_Id(long id);

List<Studios> findByUsersSet_FirstName(String firstName);

List<Studios> findByUsersSet_LastName(String lastName);

List<Studios> findByUsersSet_PhoneNumber(String phoneNumber);

//List User is registered by a particular Studio or not, param-Studio ID

//Hang-On till we reach aggregator part
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/resources.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this enum required?

public enum resources {

}
6 changes: 3 additions & 3 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
debug=false
configserver.port=8082
spring.application.name=studioservice
spring.config.import=configserver:${CONFIG_SERVER}
config-server.port=8082
spring.application.name=studioService
spring.config.import=optional:config-server:${CONFIG_SERVER}
spring.jpa.open-in-view=false
spring.flyway.baseline-on-migrate = true

125 changes: 125 additions & 0 deletions src/test/java/com/gamedoora/backend/studioservices/repositoryTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
package com.gamedoora.backend.studioservices;

import java.util.*;

import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.*;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import org.springframework.beans.factory.annotation.*;
import org.springframework.boot.autoconfigure.domain.EntityScan;
import org.springframework.boot.test.autoconfigure.orm.jpa.DataJpaTest;

import com.gamedoora.backend.studioservices.repository.StudioRepository;
import com.gamedoora.model.dao.*;
import org.springframework.boot.test.autoconfigure.orm.jpa.TestEntityManager;
import org.springframework.test.context.TestPropertySource;


@DataJpaTest(properties="spring.cloud.config.enabled=false")
@TestPropertySource("classpath:test.properties")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use application.properties in the test/resources. The idea is to follow similar pattern across the projects.

@EntityScan("com.gamedoora.model.*") // Scanning the model(s)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's move this to the Application class.

class repositoryTest {

@Autowired
private StudioRepository repo;


@Autowired
private TestEntityManager entityManager;

// Can Use @BeforeEach annotation


@Test
public void test() {
// Checking object creation and its storing

Studios studio = Studios.builder().id(123).name("Yoshita").build();

Studios savedStudio = repo.save(studio);

Assertions.assertNotNull(savedStudio);

//fail("Not yet implemented");
}

@Test
public void findByIsCommunity(){
// single checking for IsCommunity

// To use for IsCommunity - 0 or 1 as True - False

Studios studio1 = Studios.builder().id(1).name("A").isCommunity(1).description("DA").build();
Studios studio2 = Studios.builder().id(2).name("b").isCommunity(0).description("DA").visibility(false).build();
Studios studio3 = Studios.builder().id(3).name("b").isCommunity(1).description("DA").visibility(false).build();

// check 1
repo.save(studio1);
List<Studios> commu = repo.findByIsCommunity(1);
Assertions.assertEquals(1,commu.size());

Assertions.assertEquals("A",commu.get(0).getName());

// check 1 Extended
repo.save(studio2);
repo.save(studio3);
//repo.save(studio2); // sequence changing affects below assertions

commu = repo.findByIsCommunity(1);
Assertions.assertEquals(2,commu.size());
Assertions.assertEquals("A",commu.get(0).getName());
Assertions.assertEquals("b",commu.get(1).getName());
}

@Test
public void testFindByIsCommunity() {
// Create some test studios with different is_community values

// To use for IsCommunity - 0 or 1 as True - False

Studios studio1 = Studios.builder().id(1).name("A").isCommunity(1).description("DA").build();
Studios studio2 = Studios.builder().id(2).name("A").isCommunity(0).description("DA").visibility(false).build();

List<Studios> list = List.of(studio1,studio2);
repo.saveAll(list);
// Perform the repository method call
List<Studios> communityStudios = repo.findByIsCommunity(1);

assertEquals(1,communityStudios.size());
for(int i=0; i<communityStudios.size();i++)
assertEquals("A",(communityStudios.get(i)).getName()); // works well with for loops

//assertThat(communityStudios).hasSize(1).contains(studio1); // Uses ArrayList

}

// To use TestEntityManager instead of EntityManager for testing environment

// Below Shows detached state error for TestEntityManager.persist(_object_)

// @Test
// public void findByUsersSet_Name()
// {
// Studios studio1 = Studios.builder().id(1).name("A").isCommunity(1).description("DA").build();
// Studios studio2 = Studios.builder().id(1).name("b").isCommunity(2).description("DA").visibility(false).build();
//
// List<Studios> list = List.of(studio1,studio2);
// List<Studios> lis = repo.saveAll(list);
//
// Assertions.assertNotNull(lis);
//
// Users user1 = Users.builder().id(1).providerToken("").email("yo").build();
// entityManager.persist(user1);
//
// List<Studios> com = repo.findByUsersSet_Name("A");
//
// Assertions.assertEquals(1,com.size());
//
// }



}
15 changes: 15 additions & 0 deletions src/test/resources/test.properties
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# H2 Database
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's add these properties to application properties in test/resources


db.primary.url=jdbc:h2:mem:testdb;MODE=PostgreSQL
db.primary.password=pass
db.primary.driver=org.h2.Driver
db.primary.hbm2ddl.auto=create-drop
db.primary.user=dh
db.primary.hibernate.dialect=org.hibernate.dialect.H2Dialect

spring.h2.console.enabled=true

spring.flyway.enabled=false
keycloak.server.url=http://127.0.0.1:8080/u/auth
keycloak.server.username=admin
keycloak.server.clientid=