From efa29723756fc18c6a8ac0cb7ca164d741552b33 Mon Sep 17 00:00:00 2001 From: Michael Hoennig Date: Sat, 27 Apr 2019 12:05:30 +0200 Subject: [PATCH] code coverage improved for MembershipResource and CustomerResource and Customer not explicitly deletable anymore --- .../hsadminng/web/rest/CustomerResource.java | 4 +-- .../web/rest/MembershipResource.java | 15 +++++----- src/main/webapp/i18n/de/custom-error.json | 1 + src/main/webapp/i18n/en/custom-error.json | 1 + .../web/rest/CustomerResourceIntTest.java | 30 +++++++++---------- .../web/rest/MembershipResourceIntTest.java | 26 ++++++++++++++-- 6 files changed, 50 insertions(+), 27 deletions(-) diff --git a/src/main/java/org/hostsharing/hsadminng/web/rest/CustomerResource.java b/src/main/java/org/hostsharing/hsadminng/web/rest/CustomerResource.java index b5a3b74e..6e7e3a81 100644 --- a/src/main/java/org/hostsharing/hsadminng/web/rest/CustomerResource.java +++ b/src/main/java/org/hostsharing/hsadminng/web/rest/CustomerResource.java @@ -131,7 +131,7 @@ public class CustomerResource { @DeleteMapping("/customers/{id}") public ResponseEntity deleteCustomer(@PathVariable Long id) { log.debug("REST request to delete Customer : {}", id); - customerService.delete(id); - return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build(); + // TODO mhoennig: Rather completely remove the endpoint? + throw new BadRequestAlertException("Customres can't be deleted", ENTITY_NAME, "customerNotDeletable"); } } diff --git a/src/main/java/org/hostsharing/hsadminng/web/rest/MembershipResource.java b/src/main/java/org/hostsharing/hsadminng/web/rest/MembershipResource.java index befa075f..795cd59e 100644 --- a/src/main/java/org/hostsharing/hsadminng/web/rest/MembershipResource.java +++ b/src/main/java/org/hostsharing/hsadminng/web/rest/MembershipResource.java @@ -1,25 +1,24 @@ package org.hostsharing.hsadminng.web.rest; + +import io.github.jhipster.web.util.ResponseUtil; +import org.hostsharing.hsadminng.service.MembershipQueryService; import org.hostsharing.hsadminng.service.MembershipService; +import org.hostsharing.hsadminng.service.dto.MembershipCriteria; +import org.hostsharing.hsadminng.service.dto.MembershipDTO; import org.hostsharing.hsadminng.web.rest.errors.BadRequestAlertException; import org.hostsharing.hsadminng.web.rest.util.HeaderUtil; import org.hostsharing.hsadminng.web.rest.util.PaginationUtil; -import org.hostsharing.hsadminng.service.dto.MembershipDTO; -import org.hostsharing.hsadminng.service.dto.MembershipCriteria; -import org.hostsharing.hsadminng.service.MembershipQueryService; -import io.github.jhipster.web.util.ResponseUtil; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.data.domain.Page; import org.springframework.data.domain.Pageable; import org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; import org.springframework.web.bind.annotation.*; import javax.validation.Valid; import java.net.URI; import java.net.URISyntaxException; - import java.util.List; import java.util.Optional; @@ -132,7 +131,7 @@ public class MembershipResource { @DeleteMapping("/memberships/{id}") public ResponseEntity deleteMembership(@PathVariable Long id) { log.debug("REST request to delete Membership : {}", id); - membershipService.delete(id); - return ResponseEntity.ok().headers(HeaderUtil.createEntityDeletionAlert(ENTITY_NAME, id.toString())).build(); + // TODO mhoennig: Rather completely remove the endpoint? + throw new BadRequestAlertException("Memberships can't be deleted", ENTITY_NAME, "membershipNotDeletable"); } } diff --git a/src/main/webapp/i18n/de/custom-error.json b/src/main/webapp/i18n/de/custom-error.json index b82d687c..1199f245 100644 --- a/src/main/webapp/i18n/de/custom-error.json +++ b/src/main/webapp/i18n/de/custom-error.json @@ -5,6 +5,7 @@ "shareCancellationNegativeQuantity": "Kündigungen von Geschäftsanteilen erfordern eine negative Stückzahl", "shareTransactionImmutable": "Transaktionen mit Geschäftsanteilen sind unveränderlich", "membershipNotDeletable": "Mitgliedschaft kann nicht gelöscht werden, setze stattdessen das 'untilDate'", + "customerNotDeletable": "Kunden können nicht explizit gelöscht werden'", "untilDateMustBeAfterSinceDate": "Mitgliedshafts-Austrittsdatum muss nach dem Beitrittsdatum liegen", "anotherUncancelledMembershipExists": "Nur eine einzige ungekündigte Mitgliedschaft pro Kunde ist zulässig", "initializationProhibited": "Initialisierung des Feldes unzulässig", diff --git a/src/main/webapp/i18n/en/custom-error.json b/src/main/webapp/i18n/en/custom-error.json index 657e0361..8373449d 100644 --- a/src/main/webapp/i18n/en/custom-error.json +++ b/src/main/webapp/i18n/en/custom-error.json @@ -5,6 +5,7 @@ "shareCancellationNegativeQuantity": "Share cancellations require a negative quantity", "shareTransactionImmutable": "Share transactions are immutable", "membershipNotDeletable": "Membership cannot be deleted, instead set 'untilDate'", + "customerNotDeletable": "Customer cannot be deleted explicitly'", "untilDateMustBeAfterSinceDate": "Membership until date must be after since date", "anotherUncancelledMembershipExists": "Only a single uncancelled membership allowed per customer", "initializationProhibited": "Initialization of the field prohibited", diff --git a/src/test/java/org/hostsharing/hsadminng/web/rest/CustomerResourceIntTest.java b/src/test/java/org/hostsharing/hsadminng/web/rest/CustomerResourceIntTest.java index 51121f0a..c0e5a6e1 100644 --- a/src/test/java/org/hostsharing/hsadminng/web/rest/CustomerResourceIntTest.java +++ b/src/test/java/org/hostsharing/hsadminng/web/rest/CustomerResourceIntTest.java @@ -265,7 +265,7 @@ public class CustomerResourceIntTest { public void createCustomerWithNonExistingIdIsRejected() throws Exception { int databaseSizeBeforeCreate = customerRepository.findAll().size(); - // Create the Customer with an existing ID + // Create the Customer with an ID for which no entity exists customer.setId(1L); CustomerDTO customerDTO = customerMapper.toDto(customer); @@ -434,20 +434,20 @@ public class CustomerResourceIntTest { .andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE)) .andExpect(jsonPath("$.id").value(customer.getId().intValue())) .andExpect(jsonPath("$.reference").value(DEFAULT_REFERENCE)) - .andExpect(jsonPath("$.prefix").value(DEFAULT_PREFIX.toString())) - .andExpect(jsonPath("$.name").value(DEFAULT_NAME.toString())) + .andExpect(jsonPath("$.prefix").value(DEFAULT_PREFIX)) + .andExpect(jsonPath("$.name").value(DEFAULT_NAME)) .andExpect(jsonPath("$.kind").value(DEFAULT_KIND.toString())) .andExpect(jsonPath("$.birthDate").value(DEFAULT_BIRTH_DATE.toString())) - .andExpect(jsonPath("$.birthPlace").value(DEFAULT_BIRTH_PLACE.toString())) - .andExpect(jsonPath("$.registrationCourt").value(DEFAULT_REGISTRATION_COURT.toString())) - .andExpect(jsonPath("$.registrationNumber").value(DEFAULT_REGISTRATION_NUMBER.toString())) + .andExpect(jsonPath("$.birthPlace").value(DEFAULT_BIRTH_PLACE)) + .andExpect(jsonPath("$.registrationCourt").value(DEFAULT_REGISTRATION_COURT)) + .andExpect(jsonPath("$.registrationNumber").value(DEFAULT_REGISTRATION_NUMBER)) .andExpect(jsonPath("$.vatRegion").value(DEFAULT_VAT_REGION.toString())) - .andExpect(jsonPath("$.vatNumber").value(DEFAULT_VAT_NUMBER.toString())) - .andExpect(jsonPath("$.contractualSalutation").value(DEFAULT_CONTRACTUAL_SALUTATION.toString())) - .andExpect(jsonPath("$.contractualAddress").value(DEFAULT_CONTRACTUAL_ADDRESS.toString())) - .andExpect(jsonPath("$.billingSalutation").value(DEFAULT_BILLING_SALUTATION.toString())) - .andExpect(jsonPath("$.billingAddress").value(DEFAULT_BILLING_ADDRESS.toString())) - .andExpect(jsonPath("$.remark").value(DEFAULT_REMARK.toString())); + .andExpect(jsonPath("$.vatNumber").value(DEFAULT_VAT_NUMBER)) + .andExpect(jsonPath("$.contractualSalutation").value(DEFAULT_CONTRACTUAL_SALUTATION)) + .andExpect(jsonPath("$.contractualAddress").value(DEFAULT_CONTRACTUAL_ADDRESS)) + .andExpect(jsonPath("$.billingSalutation").value(DEFAULT_BILLING_SALUTATION)) + .andExpect(jsonPath("$.billingAddress").value(DEFAULT_BILLING_ADDRESS)) + .andExpect(jsonPath("$.remark").value(DEFAULT_REMARK)); } @Test @@ -1269,11 +1269,11 @@ public class CustomerResourceIntTest { // Delete the customer restCustomerMockMvc.perform(delete("/api/customers/{id}", customer.getId()) .accept(TestUtil.APPLICATION_JSON_UTF8)) - .andExpect(status().isOk()); + .andExpect(status().isBadRequest()); - // Validate the database is empty + // Validate the database is unchanged List customerList = customerRepository.findAll(); - assertThat(customerList).hasSize(databaseSizeBeforeDelete - 1); + assertThat(customerList).hasSize(databaseSizeBeforeDelete); } @Test diff --git a/src/test/java/org/hostsharing/hsadminng/web/rest/MembershipResourceIntTest.java b/src/test/java/org/hostsharing/hsadminng/web/rest/MembershipResourceIntTest.java index ce53d732..f0513341 100644 --- a/src/test/java/org/hostsharing/hsadminng/web/rest/MembershipResourceIntTest.java +++ b/src/test/java/org/hostsharing/hsadminng/web/rest/MembershipResourceIntTest.java @@ -182,10 +182,32 @@ public class MembershipResourceIntTest { @Test @Transactional - public void createMembershipWithExistingId() throws Exception { + public void createCustomerWithExistingIdIsRejected() throws Exception { + // Initialize the database + final long existingCustomerId = membershipRepository.saveAndFlush(membership).getId(); int databaseSizeBeforeCreate = membershipRepository.findAll().size(); - // Create the Membership with an existing ID + // Create the Customer with an existing ID + membership.setId(existingCustomerId); + MembershipDTO membershipDTO = membershipMapper.toDto(membership); + + // An entity with an existing ID cannot be created, so this API call must fail + restMembershipMockMvc.perform(post("/api/memberships") + .contentType(TestUtil.APPLICATION_JSON_UTF8) + .content(TestUtil.convertObjectToJsonBytes(membershipDTO))) + .andExpect(status().isBadRequest()); + + // Validate the Customer in the database + List membershipList = membershipRepository.findAll(); + assertThat(membershipList).hasSize(databaseSizeBeforeCreate); + } + + @Test + @Transactional + public void createCustomerWithNonExistingIdIsRejected() throws Exception { + int databaseSizeBeforeCreate = membershipRepository.findAll().size(); + + // Create the Membership with an ID for which no entity exists membership.setId(1L); MembershipDTO membershipDTO = membershipMapper.toDto(membership);