diff --git a/MySQL_conf_pbx/test1/mariadb_data/aria_log.00000001 b/MySQL_conf_pbx/test1/mariadb_data/aria_log.00000001 index 53eebe8..2bc4d79 100644 Binary files a/MySQL_conf_pbx/test1/mariadb_data/aria_log.00000001 and b/MySQL_conf_pbx/test1/mariadb_data/aria_log.00000001 differ diff --git a/MySQL_conf_pbx/test1/mariadb_data/aria_log_control b/MySQL_conf_pbx/test1/mariadb_data/aria_log_control index 046d59e..f221f0d 100644 Binary files a/MySQL_conf_pbx/test1/mariadb_data/aria_log_control and b/MySQL_conf_pbx/test1/mariadb_data/aria_log_control differ diff --git a/MySQL_conf_pbx/test1/mariadb_data/asterisk_db/extensions_table.ibd b/MySQL_conf_pbx/test1/mariadb_data/asterisk_db/extensions_table.ibd index 41fa505..0e2475c 100644 Binary files a/MySQL_conf_pbx/test1/mariadb_data/asterisk_db/extensions_table.ibd and b/MySQL_conf_pbx/test1/mariadb_data/asterisk_db/extensions_table.ibd differ diff --git a/MySQL_conf_pbx/test1/mariadb_data/asterisk_db/ps_aors.ibd b/MySQL_conf_pbx/test1/mariadb_data/asterisk_db/ps_aors.ibd index 9785d38..b2a159c 100644 Binary files a/MySQL_conf_pbx/test1/mariadb_data/asterisk_db/ps_aors.ibd and b/MySQL_conf_pbx/test1/mariadb_data/asterisk_db/ps_aors.ibd differ diff --git a/MySQL_conf_pbx/test1/mariadb_data/asterisk_db/ps_auths.ibd b/MySQL_conf_pbx/test1/mariadb_data/asterisk_db/ps_auths.ibd index 1934d2a..3965b13 100644 Binary files a/MySQL_conf_pbx/test1/mariadb_data/asterisk_db/ps_auths.ibd and b/MySQL_conf_pbx/test1/mariadb_data/asterisk_db/ps_auths.ibd differ diff --git a/MySQL_conf_pbx/test1/mariadb_data/asterisk_db/ps_endpoints.ibd b/MySQL_conf_pbx/test1/mariadb_data/asterisk_db/ps_endpoints.ibd index e04003e..2235245 100644 Binary files a/MySQL_conf_pbx/test1/mariadb_data/asterisk_db/ps_endpoints.ibd and b/MySQL_conf_pbx/test1/mariadb_data/asterisk_db/ps_endpoints.ibd differ diff --git a/MySQL_conf_pbx/test1/mariadb_data/ib_buffer_pool b/MySQL_conf_pbx/test1/mariadb_data/ib_buffer_pool index de03a0a..26aefdd 100644 --- a/MySQL_conf_pbx/test1/mariadb_data/ib_buffer_pool +++ b/MySQL_conf_pbx/test1/mariadb_data/ib_buffer_pool @@ -94,6 +94,7 @@ 3,24 2,24 1,24 +3,46 3,23 2,23 1,23 @@ -109,6 +110,7 @@ 3,19 2,19 1,19 +3,45 3,18 2,18 1,18 @@ -118,7 +120,6 @@ 3,16 2,16 1,16 -3,45 3,15 2,15 1,48 @@ -144,12 +145,10 @@ 1,9 3,8 2,8 -1,46 1,8 3,7 2,7 1,7 -3,46 3,6 2,6 1,6 diff --git a/MySQL_conf_pbx/test1/mariadb_data/ib_logfile0 b/MySQL_conf_pbx/test1/mariadb_data/ib_logfile0 index b31bbf5..ee82e54 100644 Binary files a/MySQL_conf_pbx/test1/mariadb_data/ib_logfile0 and b/MySQL_conf_pbx/test1/mariadb_data/ib_logfile0 differ diff --git a/MySQL_conf_pbx/test1/mariadb_data/ibdata1 b/MySQL_conf_pbx/test1/mariadb_data/ibdata1 index e088b51..c3cc124 100644 Binary files a/MySQL_conf_pbx/test1/mariadb_data/ibdata1 and b/MySQL_conf_pbx/test1/mariadb_data/ibdata1 differ diff --git a/MySQL_conf_pbx/test1/mariadb_data/mysql/innodb_index_stats.ibd b/MySQL_conf_pbx/test1/mariadb_data/mysql/innodb_index_stats.ibd index f2bbc7c..194297b 100644 Binary files a/MySQL_conf_pbx/test1/mariadb_data/mysql/innodb_index_stats.ibd and b/MySQL_conf_pbx/test1/mariadb_data/mysql/innodb_index_stats.ibd differ diff --git a/MySQL_conf_pbx/test1/mariadb_data/mysql/innodb_table_stats.ibd b/MySQL_conf_pbx/test1/mariadb_data/mysql/innodb_table_stats.ibd index 1c40a2c..f6504e8 100644 Binary files a/MySQL_conf_pbx/test1/mariadb_data/mysql/innodb_table_stats.ibd and b/MySQL_conf_pbx/test1/mariadb_data/mysql/innodb_table_stats.ibd differ diff --git a/MySQL_conf_pbx/test1/mariadb_data/undo001 b/MySQL_conf_pbx/test1/mariadb_data/undo001 index 4ee5ac6..0dbcd86 100644 Binary files a/MySQL_conf_pbx/test1/mariadb_data/undo001 and b/MySQL_conf_pbx/test1/mariadb_data/undo001 differ diff --git a/MySQL_conf_pbx/test1/mariadb_data/undo002 b/MySQL_conf_pbx/test1/mariadb_data/undo002 index 11fcff6..4c3e524 100644 Binary files a/MySQL_conf_pbx/test1/mariadb_data/undo002 and b/MySQL_conf_pbx/test1/mariadb_data/undo002 differ diff --git a/MySQL_conf_pbx/test1/mariadb_data/undo003 b/MySQL_conf_pbx/test1/mariadb_data/undo003 index ddcfc52..2ae60d6 100644 Binary files a/MySQL_conf_pbx/test1/mariadb_data/undo003 and b/MySQL_conf_pbx/test1/mariadb_data/undo003 differ diff --git a/MySQL_conf_pbx/test1/springCezenPBX/pom.xml b/MySQL_conf_pbx/test1/springCezenPBX/pom.xml index 0df826d..7489bb1 100644 --- a/MySQL_conf_pbx/test1/springCezenPBX/pom.xml +++ b/MySQL_conf_pbx/test1/springCezenPBX/pom.xml @@ -55,6 +55,21 @@ spring-boot-starter-test test + + + org.springframework.boot + spring-boot-starter-validation + + + org.modelmapper + modelmapper + 3.0.0 + + + + org.springframework.boot + spring-boot-starter-actuator + diff --git a/MySQL_conf_pbx/test1/springCezenPBX/src/main/java/com/example/cezenPBX/DTO/PsEndPointsDTO.java b/MySQL_conf_pbx/test1/springCezenPBX/src/main/java/com/example/cezenPBX/DTO/PsEndPointsDTO.java index 378d7d3..094cd64 100644 --- a/MySQL_conf_pbx/test1/springCezenPBX/src/main/java/com/example/cezenPBX/DTO/PsEndPointsDTO.java +++ b/MySQL_conf_pbx/test1/springCezenPBX/src/main/java/com/example/cezenPBX/DTO/PsEndPointsDTO.java @@ -1,7 +1,24 @@ package com.example.cezenPBX.DTO; -public record PsEndPointsDTO - (String id, String transport, String aors, String auth, String context, - String disallow, String allow, String directMedia, String connectedLineMethod, - String callerid, String dtmfMode, String mohsuggest, String mailboxes) { +import jakarta.validation.constraints.Email; +import jakarta.validation.constraints.NotBlank; +import jakarta.validation.constraints.Size; + +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) { } diff --git a/MySQL_conf_pbx/test1/springCezenPBX/src/main/java/com/example/cezenPBX/controller/CezenPbxController.java b/MySQL_conf_pbx/test1/springCezenPBX/src/main/java/com/example/cezenPBX/controller/CezenPbxController.java index c4f186e..2078369 100644 --- a/MySQL_conf_pbx/test1/springCezenPBX/src/main/java/com/example/cezenPBX/controller/CezenPbxController.java +++ b/MySQL_conf_pbx/test1/springCezenPBX/src/main/java/com/example/cezenPBX/controller/CezenPbxController.java @@ -1,6 +1,7 @@ package com.example.cezenPBX.controller; import com.example.cezenPBX.DTO.*; import com.example.cezenPBX.service.BasicPbxServiceInterface; +import jakarta.validation.Valid; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; @@ -17,7 +18,7 @@ public class CezenPbxController { @PostMapping("/add_user") // 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); @@ -26,8 +27,9 @@ public class CezenPbxController { return this.basicPbxServiceInterface.createANewEndpointService( psEndPointsRecordDTO.id(), psEndPointsRecordDTO.transport(), - psEndPointsRecordDTO.aors(), - psEndPointsRecordDTO.auth(), + // because the id and AORS and AUTHS are the same and have to be so + psEndPointsRecordDTO.id(), + psEndPointsRecordDTO.id(), psEndPointsRecordDTO.context(), psEndPointsRecordDTO.disallow(), psEndPointsRecordDTO.allow(), @@ -38,6 +40,7 @@ public class CezenPbxController { psEndPointsRecordDTO.mohsuggest(), psEndPointsRecordDTO.mailboxes() ); + // return false; } @PostMapping("/add_extension") diff --git a/MySQL_conf_pbx/test1/springCezenPBX/src/main/java/com/example/cezenPBX/entity/PsEndPoints.java b/MySQL_conf_pbx/test1/springCezenPBX/src/main/java/com/example/cezenPBX/entity/PsEndPoints.java index 384d6a7..7c1b228 100644 --- a/MySQL_conf_pbx/test1/springCezenPBX/src/main/java/com/example/cezenPBX/entity/PsEndPoints.java +++ b/MySQL_conf_pbx/test1/springCezenPBX/src/main/java/com/example/cezenPBX/entity/PsEndPoints.java @@ -1,9 +1,6 @@ package com.example.cezenPBX.entity; -import jakarta.persistence.Column; -import jakarta.persistence.Entity; -import jakarta.persistence.Id; -import jakarta.persistence.Table; +import jakarta.persistence.*; @Entity @Table(name = "ps_endpoints") @@ -70,6 +67,18 @@ public class PsEndPoints implements PBXentiry{ 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() { return extension; } diff --git a/MySQL_conf_pbx/test1/springCezenPBX/src/main/resources/application.properties b/MySQL_conf_pbx/test1/springCezenPBX/src/main/resources/application.properties index 48cb15e..fac65e4 100644 --- a/MySQL_conf_pbx/test1/springCezenPBX/src/main/resources/application.properties +++ b/MySQL_conf_pbx/test1/springCezenPBX/src/main/resources/application.properties @@ -12,7 +12,6 @@ logging.level.org.hibernate.type.descriptor.sql.BasicBinder=TRACE logging.level.org.springframework.web: DEBUG logging.level.org.hibernate: ERROR -logging.level.=DEBUG #using J-SESSION ID till i use JWT server.servlet.session.cookie.secure=true \ No newline at end of file diff --git a/MySQL_conf_pbx/test1/springCezenPBX/src/test/java/com/example/cezenPBX/controller/CezenPbxHTTPControllerTest.java b/MySQL_conf_pbx/test1/springCezenPBX/src/test/java/com/example/cezenPBX/controller/CezenPbxHTTPControllerTest.java new file mode 100644 index 0000000..6ae070f --- /dev/null +++ b/MySQL_conf_pbx/test1/springCezenPBX/src/test/java/com/example/cezenPBX/controller/CezenPbxHTTPControllerTest.java @@ -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); + } + +}