Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Setup of test environment #34

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 0 additions & 5 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,6 @@ jobs:
key: ${{ runner.os }}-maven-${{ hashFiles('**/pom.xml') }}
restore-keys: |
${{ runner.os }}-maven-
- uses: harmon758/postgresql-action@v1
with:
postgresql db: 'murdergame'
postgresql user: 'murdergame'
postgresql password: 'murdergame'
- name: Compile, test and verify
working-directory: ./backend
run: ./mvnw -q -Dspring.datasource.url="jdbc:postgresql://localhost:5432/murdergame" -Dspring.datasource.username="murdergame" -Dspring.datasource.password="murdergame" verify
5 changes: 5 additions & 0 deletions backend/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@
<artifactId>expo-server-sdk</artifactId>
<version>0.6.0</version>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<version>4.0.4.RELEASE</version>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,15 @@
import de.marvinbrieger.toothbrushgame.push.messagebuilders.MurderAssignmentNotificationService;
import de.marvinbrieger.toothbrushgame.services.interfaces.CurrentUserService;
import de.marvinbrieger.toothbrushgame.services.interfaces.EnsureGameOwnerService;
import de.marvinbrieger.toothbrushgame.services.interfaces.GameService;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@AllArgsConstructor
public class GameServiceImpl implements de.marvinbrieger.toothbrushgame.services.interfaces.GameService {
public class GameServiceImpl implements GameService {
private final GameRepository gameRepository;
private final GameCodeService gameCodeService;
private final AssignmentGeneratorService assignmentHelperService;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,13 @@ public class PlayerServiceImpl implements PlayerService {

@Override
public Player joinGame(Long gameId, Player player) {
Game gameToJoin = gameRepository.findById(gameId)
return gameRepository.findById(gameId)
.map(game -> {
Player newPlayer = new Player(player, game, currentUserService.getCurrentUser());
game.addPlayer(newPlayer);
return playerRepository.save(newPlayer);
})
.orElseThrow(() -> new GameNotFoundException(gameId));

Player newPlayer = new Player(player, gameToJoin, currentUserService.getCurrentUser());
gameToJoin.addPlayer(newPlayer);

return playerRepository.save(player);
}

}
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
package de.marvinbrieger.toothbrushgame.webservice;

import java.io.File;
import de.marvinbrieger.toothbrushgame.domain.ApplicationUser;
import de.marvinbrieger.toothbrushgame.services.interfaces.UserService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Locale;

@RestController
Expand All @@ -14,7 +16,7 @@ public class UserController {
private final UserService userService;

@PostMapping("/sign-up")
public void signUp(@RequestBody ApplicationUser user) {
public void signUp(@RequestBody ApplicationUser user) throws IOException {
Copy link
Member

@elKei24 elKei24 Jun 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I thought we do not use checked exceptions? 😄

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as i remember there was a compilation problem and I made the quick fix. You are right we had this agreement, but then I ask myself why this compilation problem was on master?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As far as I remember Intelij suggested to add it because of compilation problems. But why then was it necessary? I pulled from master?

I check this out

userService.signUp(user);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package de.marvinbrieger.toothbrushgame.mocks;

import org.json.JSONException;
import org.json.JSONObject;

public class ApplicationUserMocks {

public static JSONObject MARVINS_DEVICE;

public static JSONObject ELIAS_DEVICE;

static {
try {
MARVINS_DEVICE = new JSONObject()
.put("deviceId", "marvins device")
.put("password", "marvins password");

ELIAS_DEVICE = new JSONObject()
.put("deviceId", "elias device")
.put("password", "elias password");
} catch (JSONException e) {
e.printStackTrace();
}
}

}
Original file line number Diff line number Diff line change
@@ -1,66 +1,26 @@
package de.marvinbrieger.toothbrushgame.mocks;

import de.marvinbrieger.toothbrushgame.domain.Game;
import de.marvinbrieger.toothbrushgame.domain.GameStatus;
import de.marvinbrieger.toothbrushgame.domain.MurderAssignment;
import de.marvinbrieger.toothbrushgame.domain.Player;
import de.marvinbrieger.toothbrushgame.services.AssignmentGeneratorService;

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

import static de.marvinbrieger.toothbrushgame.mocks.GamePreferencesMocks.STANDARD_PREFERENCES;
import static de.marvinbrieger.toothbrushgame.mocks.PlayerMocks.ELIAS;
import static de.marvinbrieger.toothbrushgame.mocks.PlayerMocks.STORED_ALEX;
import static de.marvinbrieger.toothbrushgame.mocks.PlayerMocks.STORED_ELIAS;
import static de.marvinbrieger.toothbrushgame.mocks.PlayerMocks.STORED_KIPF;
import static de.marvinbrieger.toothbrushgame.mocks.PlayerMocks.STORED_MARVIN;
import static de.marvinbrieger.toothbrushgame.mocks.PlayerMocks.STORED_NEUMANN;
import static de.marvinbrieger.toothbrushgame.mocks.PlayerMocks.STORED_WINTER;
import org.json.JSONException;
import org.json.JSONObject;

public class GameMocks {

public static final Game SOFTSKILL_GAME;

public static final Game STORED_SOFTSKILL_GAME;

public static final Game SOFTSKILL_GAME_MARVIN_JOINED;

public static final Game SOFTSKILL_GAME_STARTED;
public static JSONObject SOFTSKILL_GAME;

static {
SOFTSKILL_GAME = new Game(null, "Softskillkurs SE 14", STANDARD_PREFERENCES,
null, ELIAS, null, null, null);

List<Player> storedPlayers = new ArrayList();
storedPlayers.add(STORED_ELIAS);
STORED_SOFTSKILL_GAME = new Game(1L, "Softskillkurs SE 14", STANDARD_PREFERENCES,
"ANTZUF", STORED_ELIAS, GameStatus.PREPARATION, storedPlayers, null);

List<Player> playersMarvinJoined = new ArrayList();
playersMarvinJoined.add(STORED_ELIAS);
playersMarvinJoined.add(STORED_MARVIN);
SOFTSKILL_GAME_MARVIN_JOINED = new Game(1L, "Softskillkurs SE 14", STANDARD_PREFERENCES,
"ANTZUF", STORED_ELIAS, GameStatus.PREPARATION, playersMarvinJoined, null);

List<Player> playersStartedGame = getPlayersList();
Game startedGame = new Game(1L, "Softskillkurs SE 14", STANDARD_PREFERENCES,
"ANTZUF", STORED_ELIAS, GameStatus.RUNNING, playersStartedGame, null);
AssignmentGeneratorService assignmentHelperService = new AssignmentGeneratorService();
List<MurderAssignment> murderAssignments = assignmentHelperService.generateKillAssignments(startedGame);
startedGame.setMurderAssignments(murderAssignments);
SOFTSKILL_GAME_STARTED = startedGame;
try {
SOFTSKILL_GAME = new JSONObject()
.put("title", "SE 14")
.put("preferences", new JSONObject()
.put("dailyReassignment", false)
.put("noAttestors", true)
.put("furtherRules", "nicht beim Essen töten"))
.put("owner", new JSONObject()
.put("playerName", "Marvin"));
} catch (JSONException e) {
e.printStackTrace();
}
}

private static List<Player> getPlayersList() {
List<Player> players = new ArrayList();
players.add(STORED_ELIAS);
players.add(STORED_MARVIN);
players.add(STORED_ALEX);
players.add(STORED_KIPF);
players.add(STORED_WINTER);
players.add(STORED_NEUMANN);
return players;
}

}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -2,40 +2,22 @@

import de.marvinbrieger.toothbrushgame.domain.ApplicationUser;
import de.marvinbrieger.toothbrushgame.domain.Player;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.HashSet;

public class PlayerMocks {
public static final ApplicationUser USER_MARVIN = new ApplicationUser(1L, "marvins device", "marvins password", null, new HashSet<>(),
null);
public static final ApplicationUser USER_ELIAS = new ApplicationUser(2L, "elias device", "elias password", null, new HashSet<>(), null);

public static final Player ELIAS = new Player(null, null, "Elias", USER_ELIAS, null);
public static JSONObject ELIAS_PLAYER;

public static final Player STORED_ELIAS = new Player(1L, null, "Elias", USER_ELIAS, null);

public static final Player MARVIN = new Player(null, null, "Marvin", USER_MARVIN, null);

public static final Player STORED_MARVIN = new Player(2L, null, "Marvin", USER_MARVIN, null);

public static final Player ALEX = new Player(null, null, "Alex", null, null);

public static final Player STORED_ALEX = new Player(3L, null, "Alex", null, null);

public static final Player KIPF = new Player(null, null, "Kipf", null, null);

public static final Player STORED_KIPF = new Player(4L, null, "Kipf", null, null);

public static final Player WINTER = new Player(null, null, "Winter", null, null);

public static final Player STORED_WINTER = new Player(5L, null, "Winter", null, null);

public static final Player NEUMANN = new Player(null, null, "Neumann", null, null);

public static final Player STORED_NEUMANN = new Player(6L, null, "Neumann", null, null);

public static final Player SCHELLY = new Player(null, null, "Schelly", null, null);

public static final Player STORED_SCHELLY = new Player(7L, null, "Schelly", null, null);
static {
try {
ELIAS_PLAYER = new JSONObject()
.put("playerName", "Elias");
} catch (JSONException e) {
e.printStackTrace();
}
}

}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package de.marvinbrieger.toothbrushgame.utils;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DbCleaningHelper {

public static void truncateAllTables(Connection conn) throws SQLException {
conn.createStatement().executeUpdate("SET FOREIGN_KEY_CHECKS=0");
ResultSet rs = conn.createStatement().
executeQuery("SELECT table_name FROM information_schema.tables WHERE table_schema = SCHEMA()");

while (rs.next()) {
String tableName = rs.getString(1);
conn.createStatement().executeUpdate("TRUNCATE TABLE " + tableName);
}
conn.createStatement().executeUpdate("SET FOREIGN_KEY_CHECKS=1");
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package de.marvinbrieger.toothbrushgame.utils;

import org.json.JSONObject;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;

import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;

abstract public class MockMvcHelper {

public static MockHttpServletRequestBuilder withJsonHeaders(MockHttpServletRequestBuilder builder) {
return builder.contentType(MediaType.APPLICATION_JSON)
.characterEncoding("utf-8");
}

}
Comment on lines +9 to +16
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There must be a more Spring style approch for doing that, probably something like:

@Component
public class ContentTypeHeaderMockMvcBuilderCustomizer implements MockMvcBuilderCustomizer {
    @Override
    public void customize(ConfigurableMockMvcBuilder<?> builder) {
        RequestBuilder improvedBuilder = MockMvcRequestBuilders.get("any")
                .contentType(MediaType.APPLICATION_JSON)
                .characterEncoding(StandardCharsets.UTF_8.name());
        builder.defaultRequest(improvedBuilder);
    }
}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator Author

@briegema briegema Jun 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right and there are couples of other techniques. On the other side you wrote it to me yourself. Sometimes spring config stuff is complicated and especially it is not very domain specific. The whole utils stuff i wrote isn't in the spring style but it feasible to write down shortly the things we need.

However in this special case it might be worth to adapt your solution :)

Loading