Skip to content

Commit

Permalink
Update sponsor controller (#457)
Browse files Browse the repository at this point in the history
* Update .gitignore to also exclude symlinks to build dir

Signed-off-by: Jurriaan Den Toonder <[email protected]>

* Add dataintegrityviolation handler for sponsor controller

Signed-off-by: Jurriaan Den Toonder <[email protected]>

* Add naming of foreing key constraint to get readable error

Signed-off-by: Jurriaan Den Toonder <[email protected]>

* Remove hashcode from sponsor

Signed-off-by: Jurriaan Den Toonder <[email protected]>

* Undo foreign key and work with custom exception

Signed-off-by: Jurriaan Den Toonder <[email protected]>

* Rename exception and replace find method with native implementation
  • Loading branch information
Fastjur authored and Sille Kamoen committed Feb 9, 2018
1 parent 0f79569 commit e043c20
Show file tree
Hide file tree
Showing 8 changed files with 168 additions and 7 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

### Gradle ###
.gradle
build/
build
.gradletasknamecache

# Ignore Gradle GUI config
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
/*
* Copyright (c) 2016 W.I.S.V. 'Christiaan Huygens'
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/

package ch.wisv.areafiftylan.exception;

import ch.wisv.areafiftylan.web.sponsor.model.Sponsor;
import ch.wisv.areafiftylan.web.tournament.model.Tournament;

import java.util.stream.Collectors;

public class SponsorConstraintViolationException extends AreaFiftyLANException {
public SponsorConstraintViolationException(Sponsor sponsor) {
super(String.format(
"Sponsor %s is still used by tournaments: %s",
sponsor.getName(),
String.join(", ", sponsor.getTournaments()
.stream()
.map(Tournament::getHeaderTitle)
.collect(Collectors.toList()))
));
}

public SponsorConstraintViolationException() {
super("Sponsor is still being used by one or more tournaments");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.wisv.areafiftylan.web.sponsor.controller;

import ch.wisv.areafiftylan.exception.SponsorConstraintViolationException;
import ch.wisv.areafiftylan.web.sponsor.model.Sponsor;
import ch.wisv.areafiftylan.web.sponsor.model.SponsorType;
import ch.wisv.areafiftylan.web.sponsor.service.SponsorService;
Expand Down Expand Up @@ -78,4 +79,9 @@ public ResponseEntity<?> deleteAllSponsors() {
sponsorService.deleteAllSponsors();
return createResponseEntity(HttpStatus.OK, "Successfully deleted all sponsors.");
}

@ExceptionHandler(SponsorConstraintViolationException.class)
public ResponseEntity<?> handleSponsorStillUsedInTournamentException(SponsorConstraintViolationException ex) {
return createResponseEntity(HttpStatus.CONFLICT, ex.getMessage());
}
}
26 changes: 22 additions & 4 deletions src/main/java/ch/wisv/areafiftylan/web/sponsor/model/Sponsor.java
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
package ch.wisv.areafiftylan.web.sponsor.model;

import ch.wisv.areafiftylan.web.tournament.model.Tournament;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.*;
import java.util.Set;

@Entity
@Data
Expand All @@ -24,4 +24,22 @@ public class Sponsor {
private String website;

private SponsorType type;

@OneToMany(mappedBy = "sponsor", fetch = FetchType.EAGER)
@JsonIgnoreProperties("sponsor")
private Set<Tournament> tournaments;

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;

Sponsor sponsor = (Sponsor) o;

if (!id.equals(sponsor.id)) return false;
if (name != null ? !name.equals(sponsor.name) : sponsor.name != null) return false;
if (imageName != null ? !imageName.equals(sponsor.imageName) : sponsor.imageName != null) return false;
if (website != null ? !website.equals(sponsor.website) : sponsor.website != null) return false;
return type == sponsor.type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,5 @@

@Repository
public interface SponsorRepository extends JpaRepository<Sponsor, Long> {

Collection<Sponsor> findByType(SponsorType type);
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.wisv.areafiftylan.web.sponsor.service;

import ch.wisv.areafiftylan.exception.SponsorConstraintViolationException;
import ch.wisv.areafiftylan.web.sponsor.model.Sponsor;
import ch.wisv.areafiftylan.web.sponsor.model.SponsorType;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -34,7 +35,12 @@ public Collection<Sponsor> getAllSponsorsOfType(SponsorType type) {

@Override
public void deleteSponsor(Long sponsorId) {
sponsorRepository.delete(sponsorId);
Sponsor sponsor = sponsorRepository.findOne(sponsorId);
if (sponsor.getTournaments().size() == 0) {
sponsorRepository.delete(sponsorId);
} else {
throw new SponsorConstraintViolationException(sponsor);
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package ch.wisv.areafiftylan.web.tournament.model;

import ch.wisv.areafiftylan.web.sponsor.model.Sponsor;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import lombok.Data;
import lombok.NoArgsConstructor;

Expand Down Expand Up @@ -31,5 +32,6 @@ public class Tournament {
private List<String> prizes = new ArrayList<>();

@ManyToOne(targetEntity = Sponsor.class)
@JsonIgnoreProperties("tournaments")
private Sponsor sponsor;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,28 @@
import ch.wisv.areafiftylan.web.sponsor.model.Sponsor;
import ch.wisv.areafiftylan.web.sponsor.model.SponsorType;
import ch.wisv.areafiftylan.web.sponsor.service.SponsorRepository;
import ch.wisv.areafiftylan.web.tournament.model.Tournament;
import ch.wisv.areafiftylan.web.tournament.service.TournamentRepository;
import io.restassured.http.ContentType;
import org.apache.http.HttpStatus;
import org.junit.After;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.HashSet;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertEquals;

public class WebSponsorIntegrationTest extends XAuthIntegrationTest {

@Autowired
private SponsorRepository sponsorRepository;

@Autowired
private TournamentRepository tournamentRepository;

private final String SPONSOR_ENDPOINT = "/web/sponsor/";
private final String TYPE = "PREMIUM";

Expand All @@ -27,11 +35,37 @@ private Sponsor createSponsor() {
sponsor.setImageName("redbulllogo.png");
sponsor.setWebsite("www.redbull.com");
sponsor.setType(SponsorType.PREMIUM);
sponsor.setTournaments(new HashSet<>());
return sponsorRepository.save(sponsor);
}

private Sponsor updateSponsor() {
Sponsor sponsor = new Sponsor();
sponsor.setName("CH");
sponsor.setImageName("epicowl.png");
sponsor.setWebsite("www.thuischbezorgd.nl");
sponsor.setType(SponsorType.PRESENTER);
sponsor.setTournaments(new HashSet<>());
return sponsor;
}

private Tournament createSponsorUnderTournament() {
Sponsor sponsor = new Sponsor();
sponsor.setName("TU Delft");
sponsor.setImageName("lameimage.png");
sponsor.setWebsite("http://tudelft.nl");
sponsor.setType(SponsorType.NORMAL);
sponsor = sponsorRepository.save(sponsor);

Tournament tournament = new Tournament();
tournament.setHeaderTitle("A tournament");
tournament.setSponsor(sponsor);
return tournamentRepository.save(tournament);
}

@After
public void cleanupSponsorTests() {
tournamentRepository.deleteAll();
sponsorRepository.deleteAll();
}

Expand Down Expand Up @@ -191,4 +225,60 @@ public void testDeleteAllSponsorsAsCommitteeMember() {
body("message", is("Successfully deleted all sponsors."));
//@formatter:on
}

@Test
public void testTryUpdateSponsorAsUser() {
User user = createUser();
Sponsor sponsor = createSponsor(),
updateSponsor = updateSponsor();

updateSponsor.setId(sponsor.getId());

given()
.header(getXAuthTokenHeaderForUser(user))
.body(updateSponsor)
.contentType(ContentType.JSON)
.when()
.post(SPONSOR_ENDPOINT)
.then()
.statusCode(HttpStatus.SC_FORBIDDEN);

assertEquals(1, sponsorRepository.findAll().size());
assertEquals(sponsor, sponsorRepository.findAll().get(0));
}

@Test
public void testTryUpdateSponsorAsCommittee() {
User committee = createCommitteeMember();
Sponsor sponsor = createSponsor(),
updateSponsor = updateSponsor();

updateSponsor.setId(sponsor.getId());

given()
.header(getXAuthTokenHeaderForUser(committee))
.body(updateSponsor)
.contentType(ContentType.JSON)
.when()
.post(SPONSOR_ENDPOINT)
.then()
.statusCode(HttpStatus.SC_CREATED);

assertEquals(1, sponsorRepository.findAll().size());
assertEquals(updateSponsor, sponsorRepository.findOne(updateSponsor.getId()));
}

@Test
public void testTryDeleteUsedSponsor() {
User admin = createAdmin();
Tournament tournamentWithSponsor = createSponsorUnderTournament();

given()
.header(getXAuthTokenHeaderForUser(admin))
.when()
.delete(SPONSOR_ENDPOINT + tournamentWithSponsor.getSponsor().getId())
.then()
.statusCode(HttpStatus.SC_CONFLICT)
.body("message", is("Sponsor TU Delft is still used by tournaments: A tournament"));
}
}

0 comments on commit e043c20

Please sign in to comment.