Skip to content

Commit

Permalink
Add update for tournament (#458)
Browse files Browse the repository at this point in the history
* Add update for tournament

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

* Add tests for tournament not found exception and updating of it

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

* Add equals method to tournament and set update to PUT binding

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

* Add exception handler for tournament not found

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

* Add update of tournament

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

* Remove failing equals check

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

* Replace stream with native method
  • Loading branch information
Fastjur authored and Sille Kamoen committed Feb 9, 2018
1 parent e043c20 commit 3f24228
Show file tree
Hide file tree
Showing 6 changed files with 141 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
/*
* 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;

public class TournamentNotFoundException extends AreaFiftyLANException {
public TournamentNotFoundException() {
super("Could not find tournament");
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.wisv.areafiftylan.web.tournament.controller;

import ch.wisv.areafiftylan.exception.TournamentNotFoundException;
import ch.wisv.areafiftylan.web.tournament.model.Tournament;
import ch.wisv.areafiftylan.web.tournament.model.TournamentType;
import ch.wisv.areafiftylan.web.tournament.service.TournamentService;
Expand Down Expand Up @@ -42,6 +43,13 @@ ResponseEntity<?> addTournament(@RequestBody Tournament tournament) {
return createResponseEntity(HttpStatus.CREATED, "Tournament added.", tournament);
}

@PutMapping("/{id}")
@PreAuthorize("hasRole('COMMITTEE')")
ResponseEntity<?> editTournament(@RequestBody Tournament tournament) {
tournament = tournamentService.replaceTournament(tournament);
return createResponseEntity(HttpStatus.CREATED, "Tournament updated.", tournament);
}

@DeleteMapping("/{id}")
@PreAuthorize("hasRole('COMMITTEE')")
ResponseEntity<?> deleteTournament(@PathVariable Long id) {
Expand All @@ -55,4 +63,9 @@ ResponseEntity<?> deleteAllTournaments() {
tournamentService.deleteAllTournaments();
return createResponseEntity(HttpStatus.OK, "All Tournaments deleted.");
}

@ExceptionHandler(TournamentNotFoundException.class)
public ResponseEntity<?> handleTournamentNotFoundException(TournamentNotFoundException ex) {
return createResponseEntity(HttpStatus.NOT_FOUND, ex.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,21 @@
package ch.wisv.areafiftylan.web.tournament.model;

import java.util.ArrayList;
import java.util.List;

import javax.persistence.CollectionTable;
import javax.persistence.ElementCollection;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;

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

import javax.persistence.*;
import java.util.ArrayList;
import java.util.List;

@Entity
@Data
@NoArgsConstructor
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ public interface TournamentService {

Tournament addTournament(Tournament tournament);

Tournament replaceTournament(Tournament tournament);

void deleteTournament(Long id);

void deleteAllTournaments();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ch.wisv.areafiftylan.web.tournament.service;

import ch.wisv.areafiftylan.exception.TournamentNotFoundException;
import ch.wisv.areafiftylan.web.tournament.model.Tournament;
import ch.wisv.areafiftylan.web.tournament.model.TournamentType;
import org.springframework.stereotype.Service;
Expand Down Expand Up @@ -30,6 +31,14 @@ public Tournament addTournament(Tournament tournament) {
return tournamentRepository.save(tournament);
}

@Override
public Tournament replaceTournament(Tournament tournament) {
if (tournamentRepository.findOne(tournament.getId()) == null) {
throw new TournamentNotFoundException();
}
return tournamentRepository.save(tournament);
}

@Override
public void deleteTournament(Long id) {
tournamentRepository.delete(id);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertEquals;

public class WebTournamentIntegrationTest extends XAuthIntegrationTest {

Expand Down Expand Up @@ -50,6 +51,27 @@ private Tournament createTournament() {
return tournamentRepository.save(tournament);
}

private Tournament updateTournament() {
Tournament tournament = new Tournament();
tournament.setType(TournamentType.UNOFFICIAL);
tournament.setButtonTitle("AB");
tournament.setButtonImagePath("imgpath");
tournament.setFormat("1 v 1");
tournament.setHeaderTitle("AlphaBetus");
tournament.setDescription("AlphaBetus lorum ipsum");
tournament.setPrizes(Arrays.asList("one prize", "two prize"));

Sponsor sponsor = new Sponsor();
sponsor.setName("Sponsorzzz");
sponsor.setImageName("sponzor.png");
sponsor.setWebsite("www.sponzor.com");
sponsor.setType(SponsorType.NORMAL);
sponsorRepository.save(sponsor);

tournament.setSponsor(sponsor);
return tournament;
}

@After
public void cleanupTournamentTests() {
tournamentRepository.deleteAll();
Expand Down Expand Up @@ -212,4 +234,64 @@ public void testDeleteAllTournamentsAsCommitteeMember() {
body("message", is("All Tournaments deleted."));
//@formatter:on
}

@Test
public void testUpdateTournamentAsUser() {
User user = createUser();
Tournament tournament = createTournament(),
updateTournament = updateTournament();
updateTournament.setId(tournament.getId());

given()
.header(getXAuthTokenHeaderForUser(user))
.body(updateTournament)
.contentType(ContentType.JSON)
.when()
.put(TOURNAMENT_ENDPOINT + tournament.getId())
.then()
.statusCode(HttpStatus.SC_FORBIDDEN);

assertEquals(1, tournamentRepository.findAll().size());
}

@Test
public void testUpdateTournamentAsCommittee() {
User committee = createCommitteeMember();
Tournament tournament = createTournament(),
updateTournament = updateTournament();
updateTournament.setId(tournament.getId());

given()
.header(getXAuthTokenHeaderForUser(committee))
.body(updateTournament)
.contentType(ContentType.JSON)
.when()
.put(TOURNAMENT_ENDPOINT + tournament.getId())
.then()
.statusCode(HttpStatus.SC_CREATED);

assertEquals(1, tournamentRepository.findAll().size());
assertEquals(updateTournament, tournamentRepository.findOne(tournament.getId()));
}

@Test
public void testUpdateUnknownTournament() {
User admin = createAdmin();
Tournament updateTournament = updateTournament();
updateTournament.setId(123L);

assertEquals(0, tournamentRepository.findAll().size());

given()
.header(getXAuthTokenHeaderForUser(admin))
.body(updateTournament)
.contentType(ContentType.JSON)
.when()
.put(TOURNAMENT_ENDPOINT + updateTournament.getId())
.then()
.statusCode(HttpStatus.SC_NOT_FOUND)
.body("message", equalTo("Could not find tournament"));

assertEquals(0, tournamentRepository.findAll().size());
}
}

0 comments on commit 3f24228

Please sign in to comment.