Aplication spring backend test cases

This commit is contained in:
MathewFrancis 2025-04-12 15:59:55 +05:30
parent f2e73de440
commit b5340b118e
20 changed files with 286 additions and 15 deletions

View File

@ -94,6 +94,7 @@
3,24 3,24
2,24 2,24
1,24 1,24
3,46
3,23 3,23
2,23 2,23
1,23 1,23
@ -109,6 +110,7 @@
3,19 3,19
2,19 2,19
1,19 1,19
3,45
3,18 3,18
2,18 2,18
1,18 1,18
@ -118,7 +120,6 @@
3,16 3,16
2,16 2,16
1,16 1,16
3,45
3,15 3,15
2,15 2,15
1,48 1,48
@ -144,12 +145,10 @@
1,9 1,9
3,8 3,8
2,8 2,8
1,46
1,8 1,8
3,7 3,7
2,7 2,7
1,7 1,7
3,46
3,6 3,6
2,6 2,6
1,6 1,6

View File

@ -55,6 +55,21 @@
<artifactId>spring-boot-starter-test</artifactId> <artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope> <scope>test</scope>
</dependency> </dependency>
<!-- for testing -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.modelmapper</groupId>
<artifactId>modelmapper</artifactId>
<version>3.0.0</version>
<!-- End of testing -->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
</dependencies> </dependencies>
<build> <build>

View File

@ -1,7 +1,24 @@
package com.example.cezenPBX.DTO; package com.example.cezenPBX.DTO;
public record PsEndPointsDTO import jakarta.validation.constraints.Email;
(String id, String transport, String aors, String auth, String context, import jakarta.validation.constraints.NotBlank;
String disallow, String allow, String directMedia, String connectedLineMethod, import jakarta.validation.constraints.Size;
String callerid, String dtmfMode, String mohsuggest, String mailboxes) {
public record PsEndPointsDTO(
@NotBlank(message = "ID is required")
@Size(min=1, message="Extension point length must be 1 or longer")
String id,
String transport,
// String aors,
// String auth,
String context,
String disallow,
String allow,
String directMedia,
String connectedLineMethod,
String callerid,
String dtmfMode,
String mohsuggest,
@Email
String mailboxes) {
} }

View File

@ -1,6 +1,7 @@
package com.example.cezenPBX.controller; package com.example.cezenPBX.controller;
import com.example.cezenPBX.DTO.*; import com.example.cezenPBX.DTO.*;
import com.example.cezenPBX.service.BasicPbxServiceInterface; import com.example.cezenPBX.service.BasicPbxServiceInterface;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RequestBody;
@ -17,7 +18,7 @@ public class CezenPbxController {
@PostMapping("/add_user") @PostMapping("/add_user")
// this method will accept json input fields, process them using jackson then pass them to the service // this method will accept json input fields, process them using jackson then pass them to the service
public boolean createNewEndPointController(@RequestBody PsEndPointsDTO psEndPointsRecordDTO){ public boolean createNewEndPointController(@RequestBody @Valid PsEndPointsDTO psEndPointsRecordDTO){
System.out.println(psEndPointsRecordDTO); System.out.println(psEndPointsRecordDTO);
@ -26,8 +27,9 @@ public class CezenPbxController {
return this.basicPbxServiceInterface.createANewEndpointService( return this.basicPbxServiceInterface.createANewEndpointService(
psEndPointsRecordDTO.id(), psEndPointsRecordDTO.id(),
psEndPointsRecordDTO.transport(), psEndPointsRecordDTO.transport(),
psEndPointsRecordDTO.aors(), // because the id and AORS and AUTHS are the same and have to be so
psEndPointsRecordDTO.auth(), psEndPointsRecordDTO.id(),
psEndPointsRecordDTO.id(),
psEndPointsRecordDTO.context(), psEndPointsRecordDTO.context(),
psEndPointsRecordDTO.disallow(), psEndPointsRecordDTO.disallow(),
psEndPointsRecordDTO.allow(), psEndPointsRecordDTO.allow(),
@ -38,6 +40,7 @@ public class CezenPbxController {
psEndPointsRecordDTO.mohsuggest(), psEndPointsRecordDTO.mohsuggest(),
psEndPointsRecordDTO.mailboxes() psEndPointsRecordDTO.mailboxes()
); );
// return false;
} }
@PostMapping("/add_extension") @PostMapping("/add_extension")

View File

@ -1,9 +1,6 @@
package com.example.cezenPBX.entity; package com.example.cezenPBX.entity;
import jakarta.persistence.Column; import jakarta.persistence.*;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Table;
@Entity @Entity
@Table(name = "ps_endpoints") @Table(name = "ps_endpoints")
@ -70,6 +67,18 @@ public class PsEndPoints implements PBXentiry{
public PsEndPoints() { public PsEndPoints() {
} }
//This ensures mohsuggest will never be null in the database it defaults to "default".
@PrePersist
@PreUpdate
public void applyDefaults() {
if (this.transport == null) {this.transport = "transport-udp";}
if (this.context == null) this.context = "default";
if (this.disallow == null) this.disallow = "all";
if (this.allow == null) this.allow = "ulaw,alaw";
if (this.directMedia == null) this.directMedia = "no";
if (this.mohsuggest == null) this.mohsuggest = "default";
}
public String getExtension() { public String getExtension() {
return extension; return extension;
} }

View File

@ -12,7 +12,6 @@ logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE
logging.level.org.springframework.web: DEBUG logging.level.org.springframework.web: DEBUG
logging.level.org.hibernate: ERROR logging.level.org.hibernate: ERROR
logging.level.=DEBUG
#using J-SESSION ID till i use JWT #using J-SESSION ID till i use JWT
server.servlet.session.cookie.secure=true server.servlet.session.cookie.secure=true

View File

@ -0,0 +1,229 @@
package com.example.cezenPBX.controller;
import com.example.cezenPBX.DTO.PsEndPointsDTO;
import com.example.cezenPBX.service.BasicPbxService;
import com.example.cezenPBX.service.BasicPbxServiceInterface;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.test.web.servlet.RequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import static org.mockito.ArgumentMatchers.*;
@WebMvcTest(CezenPbxController.class)
@MockBean({BasicPbxService.class})
public class CezenPbxHTTPControllerTest {
@Autowired
private MockMvc mockMvc;
@Autowired
private BasicPbxServiceInterface basicPbxServiceInterface;
// test for exception when user passes null value
@Test
@DisplayName("Null value input")
void testCreateEndpoint_whenUserEntersNullValues_thenAppMustReturn400() throws Exception {
//ModelMapper modelMapper = new ModelMapper();
// create a new object for an endpoint DTO
PsEndPointsDTO psEndPointsDTO = new PsEndPointsDTO(
null,
null,
null,
null,
null,
null,
null,
null,
null,
null,
null
);
httpTestSupportForEndpointCreation(psEndPointsDTO, 400);
}
@Test
@DisplayName("1001 value input")
void testCreateEndpoint_whenUserEntersProperValue_thenAppMustReturn200() throws Exception {
//ModelMapper modelMapper = new ModelMapper();
// create a new object for an endpoint DTO
PsEndPointsDTO psEndPointsDTO = new PsEndPointsDTO(
"1001",
null,
null,
null,
null,
null,
null,
null,
null,
null,
null
);
httpTestSupportForEndpointCreation(psEndPointsDTO, 200);
}
@Test
@DisplayName("Empty value input")
void testCreateEndpoint_whenUserEntersNullValues_thenAppMustThrowException() throws Exception {
//ModelMapper modelMapper = new ModelMapper();
// create a new object for an endpoint DTO
PsEndPointsDTO psEndPointsDTO = new PsEndPointsDTO(
"",
null,
null,
null,
null,
null,
null,
null,
null,
null,
null
);
httpTestSupportForEndpointCreation(psEndPointsDTO, 400);
}
@Test
@DisplayName("Wrong Email Format input")
void testCreateEndpoint_whenUserEntersWrongEmailFormat_thenAppMustThrowException() throws Exception {
//ModelMapper modelMapper = new ModelMapper();
// create a new object for an endpoint DTO
PsEndPointsDTO psEndPointsDTO = new PsEndPointsDTO(
"1001",
null,
null,
null,
null,
null,
null,
null,
null,
null,
"mathew"
);
httpTestSupportForEndpointCreation(psEndPointsDTO, 400);
}
@Test
@DisplayName("Correct email format")
void testCreateEndpoint_whenUserEntersCorrectEmailFormat_thenAppMustReturn200() throws Exception {
//ModelMapper modelMapper = new ModelMapper();
// create a new object for an endpoint DTO
PsEndPointsDTO psEndPointsDTO = new PsEndPointsDTO(
"1001",
null,
null,
null,
null,
null,
null,
null,
null,
null,
"mathew@gmail.com"
);
httpTestSupportForEndpointCreation(psEndPointsDTO, 200);
}
private void httpTestSupportForEndpointCreation(PsEndPointsDTO psEndPointsDTO, final int testHttpStatus) throws Exception {
RequestBuilder requestBuilder = MockMvcRequestBuilders.post("/cezen/add_user")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content(new ObjectMapper().writeValueAsBytes(psEndPointsDTO));
// Act
// get the result of the simulated post request
MvcResult mvcResult = mockMvc.perform(requestBuilder).andReturn();
// Assert
Assertions.assertEquals(testHttpStatus, mvcResult.getResponse().getStatus(), "Incorrect HTTP Status code");
}
// database error or duplicate value check
@Test
@DisplayName("JDBC returns true when value attempts to persist and values checks out")
void testCreateEndpoint_whenUserEntersDuplicateValue_thenAppReturnFalse() throws Exception {
//ModelMapper modelMapper = new ModelMapper();
// create a new object for an endpoint DTO
PsEndPointsDTO psEndPointsDTO = new PsEndPointsDTO(
"1005",
null,
null,
null,
null,
null,
null,
null,
null,
null,
null
);
// now mock psEndPoints and basicPbxServiceInterface
Mockito.when(basicPbxServiceInterface.createANewEndpointService(
eq("1005"), // Arg 1: psEndPointsRecordDTO.id()
isNull(String.class), // Arg 2: psEndPointsRecordDTO.transport() - is null
eq("1005"), // Arg 3: psEndPointsRecordDTO.id() again
eq("1005"), // Arg 4: psEndPointsRecordDTO.id() again
isNull(String.class), // Arg 5: psEndPointsRecordDTO.context() - is null
isNull(String.class), // Arg 6: psEndPointsRecordDTO.disallow() - is null
isNull(String.class), // Arg 7: psEndPointsRecordDTO.allow() - is null
isNull(String.class), // Arg 8: psEndPointsRecordDTO.directMedia() - is null
isNull(String.class), // Arg 9: psEndPointsRecordDTO.connectedLineMethod() - is null
isNull(String.class), // Arg 10: psEndPointsRecordDTO.callerid() - is null
isNull(String.class), // Arg 11: psEndPointsRecordDTO.dtmfMode() - is null
isNull(String.class), // Arg 12: psEndPointsRecordDTO.mohsuggest() - is null
isNull(String.class)
)).thenReturn(true);
RequestBuilder requestBuilder = MockMvcRequestBuilders.post("/cezen/add_user")
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content(new ObjectMapper().writeValueAsBytes(psEndPointsDTO));
// Act
// get the result of the simulated post request
MvcResult mvcResult = mockMvc.perform(requestBuilder).andReturn();
// --- Get the Response ---
MockHttpServletResponse response = mvcResult.getResponse();
String responseBody = response.getContentAsString();
// --- --- --- --- --- ---
Assertions.assertEquals("true", responseBody);
}
}