diff --git a/src/test/java/com/bootexample4/products/controller/ProductController_createProduct_5fee6d5a95_Test.java b/src/test/java/com/bootexample4/products/controller/ProductController_createProduct_5fee6d5a95_Test.java new file mode 100644 index 00000000..b882dd47 --- /dev/null +++ b/src/test/java/com/bootexample4/products/controller/ProductController_createProduct_5fee6d5a95_Test.java @@ -0,0 +1,75 @@ +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +import java.net.URI; +import java.util.ArrayList; +import java.util.List; + +import org.junit.Before; +import org.junit.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.http.MediaType; +import org.springframework.test.web.reactive.server.WebTestClient; +import org.springframework.web.reactive.function.bodyToMono; + +public class ProductControllerTest { + + private WebTestClient.Builder testClientBuilder; + + @Autowired + public ProductControllerTest(WebTestClient.Builder testClientBuilder) { + this.testClientBuilder = testClientBuilder; + } + + @Before + public void setup() { + testClientBuilder.apply(this::configureMockMvc); + } + + private void configureMockMvc(WebTestClient.Builder builder) { + builder.mockMvc(new MockMvc(new WebApplicationContext())); + } + + @Test + public void testCreateProductSuccessful() { + // Arrange + Product product = new Product(); + product.setName("Test Product"); + product.setDescription("This is a test product."); + product.setPrice(19.99); + + // Act + ResponseEntity response = testClientBuilder.post() + .contentType(MediaType.APPLICATION_JSON) + .body(bodyToMono(product)) + .uri("/api/products") + .exchange(); + + // Assert + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertNotNull(response.getBody()); + assertEquals(product.getName(), response.getBody().getName()); + } + + @Test + public void testCreateProductFailed() { + // Arrange + Product product = new Product(); + product.setName("Invalid Name"); + product.setDescription("This is a test product."); + product.setPrice(19.99); + + // Act + ResponseEntity response = testClientBuilder.post() + .contentType(MediaType.APPLICATION_JSON) + .body(bodyToMono(product)) + .uri("/api/products") + .exchange(); + + // Assert + assertEquals(HttpStatus.BAD_REQUEST, response.getStatusCode()); + assertNotNull(response.getBody()); + assertTrue(response.getBody().getMessage().contains("Invalid name")); + } +} diff --git a/src/test/java/com/bootexample4/products/controller/ProductController_deleteProduct_8aa83376b7_Test.java b/src/test/java/com/bootexample4/products/controller/ProductController_deleteProduct_8aa83376b7_Test.java new file mode 100644 index 00000000..6583ae85 --- /dev/null +++ b/src/test/java/com/bootexample4/products/controller/ProductController_deleteProduct_8aa83376b7_Test.java @@ -0,0 +1,13 @@ +@Test +public void testDeleteSuccessfulProduct() { + Long id = 1L; + ResponseEntity response = restTemplate.deleteProduct(id); + assertEquals(HttpStatus.OK, response.getStatusCode()); +} + +@Test +public void testDeleteNonExistentProduct() { + Long id = 9999L; + ResponseEntity response = restTemplate.deleteProduct(id); + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); +} diff --git a/src/test/java/com/bootexample4/products/controller/ProductController_getAllProducts_ce6c2f6265_Test.java b/src/test/java/com/bootexample4/products/controller/ProductController_getAllProducts_ce6c2f6265_Test.java new file mode 100644 index 00000000..7c726b7d --- /dev/null +++ b/src/test/java/com/bootexample4/products/controller/ProductController_getAllProducts_ce6c2f6265_Test.java @@ -0,0 +1,61 @@ +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.when; + +import java.util.ArrayList; +import java.util.List; +import org.junit.Before; +import org.junit.Test; +import org.junit.runner.RunWith; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebFlux; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.http.MediaType; +import org.springframework.test.context.junit4.SpringRunner; +import reactor.core.publisher.Mono; + +@RunWith(SpringRunner.class) +@SpringBootTest +@AutoConfigureWebFlux +public class ProductControllerTest { + + @Autowired + private ProductRepository productRepository; + + private RestTemplate restTemplate; + + @Before + public void setup() { + this.restTemplate = new MockRestTemplate(); + } + + @Test + public void testGetAllProductsSuccessful() { + // Arrange + List expectedResults = new ArrayList<>(); + expectedResults.add(new Product("Product 1", 10)); + expectedResults.add(new Product("Product 2", 20)); + when(productRepository.findAll()).thenReturn(expectedResults); + + // Act + Mono> response = restTemplate.exchange("/api/products", HttpMethod.GET, null, List.class); + + // Assert + assertEquals(HttpStatus.OK, response.statusCode()); + assertEquals(expectedResults, response.body()); + } + + @Test + public void testGetAllProductsFailed() { + // Arrange + when(productRepository.findAll()).thenThrow(new RuntimeException("Mock database exception")); + + // Act + Mono> response = restTemplate.exchange("/api/products", HttpMethod.GET, null, List.class); + + // Assert + assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, response.statusCode()); + assertNull(response.body()); + } +} diff --git a/src/test/java/com/bootexample4/products/controller/ProductController_getProductById_079f0e7d67_Test.java b/src/test/java/com/bootexample4/products/controller/ProductController_getProductById_079f0e7d67_Test.java new file mode 100644 index 00000000..d2e5915f --- /dev/null +++ b/src/test/java/com/bootexample4/products/controller/ProductController_getProductById_079f0e7d67_Test.java @@ -0,0 +1,25 @@ +@Test +public void testGetProductByIdSuccessful() { + Long id = 1L; + ResponseEntity response = restTemplate.exchange( + String.format("/products/%d", id), + HttpMethod.GET, + null, + Product.class); + assertThat(response).isNotNull(); + assertThat(response.getBody()).isNotNull(); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); +} + +@Test +public void testGetProductByIdNotFound() { + Long id = 99999L; + ResponseEntity response = restTemplate.exchange( + String.format("/products/%d", id), + HttpMethod.GET, + null, + Product.class); + assertThat(response).isNotNull(); + assertThat(response.getBody()).isNull(); + assertThat(response.getStatusCode()).isEqualTo(HttpStatus.NOT_FOUND); +} diff --git a/src/test/java/com/bootexample4/products/controller/ProductController_updateProduct_d88ecc4aa9_Test.java b/src/test/java/com/bootexample4/products/controller/ProductController_updateProduct_d88ecc4aa9_Test.java new file mode 100644 index 00000000..8513db64 --- /dev/null +++ b/src/test/java/com/bootexample4/products/controller/ProductController_updateProduct_d88ecc4aa9_Test.java @@ -0,0 +1,40 @@ +@Test +public void testUpdateProductSuccess() { + // Arrange + long id = 1L; + Product product = new Product(); + + // Act + ResponseEntity response = restTemplate.exchange( + "/api/products/" + id, + HttpMethod.PUT, + product, + Product.class + ); + + // Assert + assertEquals(HttpStatus.OK, response.getStatusCode()); + assertNotNull(response.getBody()); + assertEquals("Updated Name", response.getBody().getName()); + assertEquals("Updated Description", response.getBody().getDescription()); + assertEquals(10.99, response.getBody().getPrice(), 0.01); +} + +@Test +public void testUpdateProductNotFound() { + // Arrange + long id = 9999L; + Product product = new Product(); + + // Act + ResponseEntity response = restTemplate.exchange( + "/api/products/" + id, + HttpMethod.PUT, + product, + Product.class + ); + + // Assert + assertEquals(HttpStatus.NOT_FOUND, response.getStatusCode()); + assertNull(response.getBody()); +} diff --git a/src/test/java/com/bootexample4/products/model/Product_getDescription_ca387c4bd2_Test.java b/src/test/java/com/bootexample4/products/model/Product_getDescription_ca387c4bd2_Test.java new file mode 100644 index 00000000..97cfaf90 --- /dev/null +++ b/src/test/java/com/bootexample4/products/model/Product_getDescription_ca387c4bd2_Test.java @@ -0,0 +1,26 @@ +@Test +public void testGetDescription_Success() { + // Arrange + Employee employee = new Employee(1L, "John Doe", "johndoe@example.com"); + employee.setDescription("A software engineer with 5 years of experience."); + + // Act + String result = employee.getDescription(); + + // Assert + assertEquals("A software engineer with 5 years of experience.", result); +} + +@Test +public void testGetDescription_Failure() { + // Arrange + Employee employee = new Employee(1L, "John Doe", "johndoe@example.com"); + + // Act + try { + String result = employee.getDescription(); + } catch (IllegalStateException e) { + // Assert + assertEquals("Employee has no description.", e.getMessage()); + } +} diff --git a/src/test/java/com/bootexample4/products/model/Product_getId_eb19b6a6d6_Test.java b/src/test/java/com/bootexample4/products/model/Product_getId_eb19b6a6d6_Test.java new file mode 100644 index 00000000..f18459f1 --- /dev/null +++ b/src/test/java/com/bootexample4/products/model/Product_getId_eb19b6a6d6_Test.java @@ -0,0 +1,14 @@ +@Test +public void testGetId_Success() { + Long expectedId = 1L; + Entity entity = new Entity(1, "name", "description"); + when(entityRepository.findById(anyLong())).thenReturn(Optional.of(entity)); + assertEquals(expectedId, entityService.getId()); +} + +@Test +public void testGetId_Failure() { + Long expectedId = null; + when(entityRepository.findById(anyLong())).thenReturn(Optional.empty()); + assertNull(entityService.getId()); +} diff --git a/src/test/java/com/bootexample4/products/model/Product_getName_4ad76af4d7_Test.java b/src/test/java/com/bootexample4/products/model/Product_getName_4ad76af4d7_Test.java new file mode 100644 index 00000000..718acc45 --- /dev/null +++ b/src/test/java/com/bootexample4/products/model/Product_getName_4ad76af4d7_Test.java @@ -0,0 +1,11 @@ +@Test +public void testGetName_Success() { + Employee employee = new Employee(1L); + assertEquals("John Doe", employee.getName()); +} + +@Test +public void testGetName_Failure() { + Employee employee = new Employee(1L, null); + assertNull(employee.getName()); +} diff --git a/src/test/java/com/bootexample4/products/model/Product_getPrice_d97185b3e6_Test.java b/src/test/java/com/bootexample4/products/model/Product_getPrice_d97185b3e6_Test.java new file mode 100644 index 00000000..26f46b47 --- /dev/null +++ b/src/test/java/com/bootexample4/products/model/Product_getPrice_d97185b3e6_Test.java @@ -0,0 +1,21 @@ +@Test +public void testGetPriceSuccess() { + // Arrange + Product product = new Product(); + product.setPrice(10.99); + // Act + double result = product.getPrice(); + // Assert + assertEquals(10.99, result, 0.01); +} + +@Test +public void testGetPriceFailure() { + // Arrange + Product product = new Product(); + product.setPrice(-1.00); + // Act + double result = product.getPrice(); + // Assert + assertEquals(-1.00, result, 0.01); +} diff --git a/src/test/java/com/bootexample4/products/model/Product_setDescription_5d6a2c2267_Test.java b/src/test/java/com/bootexample4/products/model/Product_setDescription_5d6a2c2267_Test.java new file mode 100644 index 00000000..3b96cac8 --- /dev/null +++ b/src/test/java/com/bootexample4/products/model/Product_setDescription_5d6a2c2267_Test.java @@ -0,0 +1,13 @@ +@Test +public void testSetDescriptionSuccess() { + Employee employee = new Employee(); + employee.setDescription("A software developer"); + assertEquals("A software developer", employee.getDescription()); +} + +@Test +public void testSetDescriptionFailureNullInput() { + Employee employee = new Employee(); + assertThrows(IllegalArgumentException.class, () -> employee.setDescription(null)); + assertEquals("", employee.getDescription()); +} diff --git a/src/test/java/com/bootexample4/products/model/Product_setId_e7f341730a_Test.java b/src/test/java/com/bootexample4/products/model/Product_setId_e7f341730a_Test.java new file mode 100644 index 00000000..070ea3f2 --- /dev/null +++ b/src/test/java/com/bootexample4/products/model/Product_setId_e7f341730a_Test.java @@ -0,0 +1,18 @@ +@Test +public void testSetIdSuccessful() { + Employee employee = new Employee(); + Long id = 1L; + employee.setId(id); + assertEquals(id, employee.getId()); +} + +@Test +public void testSetIdFailure() { + Employee employee = new Employee(); + try { + employee.setId(null); + fail("Expected IllegalArgumentException"); + } catch (IllegalArgumentException e) { + assertEquals("Invalid id", e.getMessage()); + } +} diff --git a/src/test/java/com/bootexample4/products/model/Product_setName_f5bd015150_Test.java b/src/test/java/com/bootexample4/products/model/Product_setName_f5bd015150_Test.java new file mode 100644 index 00000000..932d5ad9 --- /dev/null +++ b/src/test/java/com/bootexample4/products/model/Product_setName_f5bd015150_Test.java @@ -0,0 +1,18 @@ +@Test +public void testSetNameSuccess() { + Employee employee = new Employee(); + employee.setName("John Doe"); + assertEquals("John Doe", employee.getName()); +} + +@Test +public void testSetNameFailure() { + Employee employee = new Employee(); + try { + employee.setName(null); + fail(); + } catch (IllegalArgumentException e) { + assertEquals(" Cannot set null value for 'name'", e.getMessage()); + } + assertNull(employee.getName()); +} diff --git a/src/test/java/com/bootexample4/products/model/Product_setPrice_fa182f5c65_Test.java b/src/test/java/com/bootexample4/products/model/Product_setPrice_fa182f5c65_Test.java new file mode 100644 index 00000000..1a739f35 --- /dev/null +++ b/src/test/java/com/bootexample4/products/model/Product_setPrice_fa182f5c65_Test.java @@ -0,0 +1,17 @@ +@Test +public void testSetPriceSuccessful() { + Product product = new Product(); + product.setPrice(10.99); + assertEquals(10.99, product.getPrice()); +} + +@Test +public void testSetPriceFailure() { + Product product = new Product(); + try { + product.setPrice(-10.99); + failBecauseNotReached(); + } catch (IllegalArgumentException e) { + assertEquals("Price cannot be negative", e.getMessage()); + } +} diff --git a/target/classes/application.properties b/target/classes/application.properties new file mode 100644 index 00000000..8b137891 --- /dev/null +++ b/target/classes/application.properties @@ -0,0 +1 @@ + diff --git a/target/classes/com/bootexample4/products/ProductsApplication.class b/target/classes/com/bootexample4/products/ProductsApplication.class new file mode 100644 index 00000000..6a52de87 Binary files /dev/null and b/target/classes/com/bootexample4/products/ProductsApplication.class differ diff --git a/target/classes/com/bootexample4/products/controller/ProductController.class b/target/classes/com/bootexample4/products/controller/ProductController.class new file mode 100644 index 00000000..0d818f3f Binary files /dev/null and b/target/classes/com/bootexample4/products/controller/ProductController.class differ diff --git a/target/classes/com/bootexample4/products/model/Product.class b/target/classes/com/bootexample4/products/model/Product.class new file mode 100644 index 00000000..0b02a5ee Binary files /dev/null and b/target/classes/com/bootexample4/products/model/Product.class differ diff --git a/target/classes/com/bootexample4/products/repository/ProductRepository.class b/target/classes/com/bootexample4/products/repository/ProductRepository.class new file mode 100644 index 00000000..359c7e3f Binary files /dev/null and b/target/classes/com/bootexample4/products/repository/ProductRepository.class differ diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst new file mode 100644 index 00000000..3fc2fa99 --- /dev/null +++ b/target/maven-status/maven-compiler-plugin/compile/default-compile/createdFiles.lst @@ -0,0 +1,4 @@ +com/bootexample4/products/model/Product.class +com/bootexample4/products/controller/ProductController.class +com/bootexample4/products/repository/ProductRepository.class +com/bootexample4/products/ProductsApplication.class diff --git a/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst new file mode 100644 index 00000000..03306a72 --- /dev/null +++ b/target/maven-status/maven-compiler-plugin/compile/default-compile/inputFiles.lst @@ -0,0 +1,4 @@ +/var/tmp/Roost/RoostGPT/dmopensourcetest/bd2a7256-4ac2-4639-9540-14990381b896/java-springbootunit/src/main/java/com/bootexample4/products/controller/ProductController.java +/var/tmp/Roost/RoostGPT/dmopensourcetest/bd2a7256-4ac2-4639-9540-14990381b896/java-springbootunit/src/main/java/com/bootexample4/products/model/Product.java +/var/tmp/Roost/RoostGPT/dmopensourcetest/bd2a7256-4ac2-4639-9540-14990381b896/java-springbootunit/src/main/java/com/bootexample4/products/ProductsApplication.java +/var/tmp/Roost/RoostGPT/dmopensourcetest/bd2a7256-4ac2-4639-9540-14990381b896/java-springbootunit/src/main/java/com/bootexample4/products/repository/ProductRepository.java diff --git a/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst b/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/createdFiles.lst new file mode 100644 index 00000000..e69de29b diff --git a/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst b/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst new file mode 100644 index 00000000..71ffeb97 --- /dev/null +++ b/target/maven-status/maven-compiler-plugin/testCompile/default-testCompile/inputFiles.lst @@ -0,0 +1,18 @@ +/var/tmp/Roost/RoostGPT/dmopensourcetest/bd2a7256-4ac2-4639-9540-14990381b896/java-springbootunit/src/test/java/com/bootexample4/products/model/Product_setId_e7f341730a_Test.java +/var/tmp/Roost/RoostGPT/dmopensourcetest/bd2a7256-4ac2-4639-9540-14990381b896/java-springbootunit/src/test/java/com/bootexample4/products/controller/ProductController_getAllProducts_ce6c2f6265_Test.java +/var/tmp/Roost/RoostGPT/dmopensourcetest/bd2a7256-4ac2-4639-9540-14990381b896/java-springbootunit/src/test/java/com/bootexample4/products/model/Product_setName_f5bd015150_Test.java +/var/tmp/Roost/RoostGPT/dmopensourcetest/bd2a7256-4ac2-4639-9540-14990381b896/java-springbootunit/src/test/java/com/bootexample4/products/model/Product_getName_4ad76af4d7_Test.java +/var/tmp/Roost/RoostGPT/dmopensourcetest/bd2a7256-4ac2-4639-9540-14990381b896/java-springbootunit/src/test/java/com/bootexample4/products/model/Product_setDescription_5d6a2c2267_Test.java +/var/tmp/Roost/RoostGPT/dmopensourcetest/bd2a7256-4ac2-4639-9540-14990381b896/java-springbootunit/src/test/java/com/bootexample4/products/TestMockServer.java +/var/tmp/Roost/RoostGPT/dmopensourcetest/bd2a7256-4ac2-4639-9540-14990381b896/java-springbootunit/src/test/java/com/bootexample4/products/controller/ProductController_getProductById_079f0e7d67_Test.java +/var/tmp/Roost/RoostGPT/dmopensourcetest/bd2a7256-4ac2-4639-9540-14990381b896/java-springbootunit/src/test/java/com/bootexample4/products/model/Product_getPrice_d97185b3e6_Test.java +/var/tmp/Roost/RoostGPT/dmopensourcetest/bd2a7256-4ac2-4639-9540-14990381b896/java-springbootunit/src/test/java/com/bootexample4/products/cucumber/SpringIntegrationTests.java +/var/tmp/Roost/RoostGPT/dmopensourcetest/bd2a7256-4ac2-4639-9540-14990381b896/java-springbootunit/src/test/java/com/bootexample4/products/controller/ProductController_updateProduct_d88ecc4aa9_Test.java +/var/tmp/Roost/RoostGPT/dmopensourcetest/bd2a7256-4ac2-4639-9540-14990381b896/java-springbootunit/src/test/java/com/bootexample4/products/model/Product_getDescription_ca387c4bd2_Test.java +/var/tmp/Roost/RoostGPT/dmopensourcetest/bd2a7256-4ac2-4639-9540-14990381b896/java-springbootunit/src/test/java/com/bootexample4/products/controller/ProductController_createProduct_5fee6d5a95_Test.java +/var/tmp/Roost/RoostGPT/dmopensourcetest/bd2a7256-4ac2-4639-9540-14990381b896/java-springbootunit/src/test/java/com/bootexample4/products/ProductsApplicationTests.java +/var/tmp/Roost/RoostGPT/dmopensourcetest/bd2a7256-4ac2-4639-9540-14990381b896/java-springbootunit/src/test/java/com/bootexample4/products/cucumber/CucumberTestRunner.java +/var/tmp/Roost/RoostGPT/dmopensourcetest/bd2a7256-4ac2-4639-9540-14990381b896/java-springbootunit/src/test/java/com/bootexample4/products/cucumber/ProductStepDefinitions.java +/var/tmp/Roost/RoostGPT/dmopensourcetest/bd2a7256-4ac2-4639-9540-14990381b896/java-springbootunit/src/test/java/com/bootexample4/products/controller/ProductController_deleteProduct_8aa83376b7_Test.java +/var/tmp/Roost/RoostGPT/dmopensourcetest/bd2a7256-4ac2-4639-9540-14990381b896/java-springbootunit/src/test/java/com/bootexample4/products/model/Product_getId_eb19b6a6d6_Test.java +/var/tmp/Roost/RoostGPT/dmopensourcetest/bd2a7256-4ac2-4639-9540-14990381b896/java-springbootunit/src/test/java/com/bootexample4/products/model/Product_setPrice_fa182f5c65_Test.java diff --git a/target/test-classes/features/sample.feature b/target/test-classes/features/sample.feature new file mode 100644 index 00000000..61fb017b --- /dev/null +++ b/target/test-classes/features/sample.feature @@ -0,0 +1,38 @@ +Feature: Product API + As a user of the product API + I want to be able to perform CRUD operations on products + So that I can manage my products effectively + + Background: + Given the base URL is "http://localhost:8080" + + Scenario: Get all products + When the client sends a GET request "/api/products" to get the list of all products + Then the list of products returned should be empty + + Scenario: Create a new product + Given the client provides the following product data: + | name | description | price | + | Test Product | This is a test product. | 10.0 | + When the client sends a POST request to "/api/products" + Then the saved product should not be null and its properties must correspond to those provided by client + + Scenario: Get a product by ID + Given there is an existing product with ID 1 + When the client sends a GET request "/api/products/1" to get a product by its id + Then the response status code should be 200 + And the response should contain the product with ID 1 + + Scenario: Update an existing product + Given there is an existing product with ID 1 + And the client provides the following product data: + | name | description | price | + | Updated Product | This is an updated test product. | 15.0 | + When the client sends a PUT request to "/api/products/1" + Then the product with ID 1 should be updated with the provided details + + Scenario: Delete an existing product + Given there is an existing product with ID 1 + When the client sends a DELETE request to "/api/products/1" + Then the response status code should be 200 + And the product with ID 1 should no longer exist