Skip to content

Instantly share code, notes, and snippets.

@AlbertFX91
Created August 15, 2017 12:06
Show Gist options
  • Save AlbertFX91/1d10fa21c4308704cdc5bb921453c040 to your computer and use it in GitHub Desktop.
Save AlbertFX91/1d10fa21c4308704cdc5bb921453c040 to your computer and use it in GitHub Desktop.
package com.albertfx.games.web.rest;
import static org.assertj.core.api.Assertions.assertThat;
import static org.hamcrest.Matchers.hasItem;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.content;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import com.albertfx.games.GamesApp;
import com.albertfx.games.domain.Game;
import com.albertfx.games.repository.GameRepository;
import com.albertfx.games.service.GameService;
import com.albertfx.games.web.rest.errors.ExceptionTranslator;
/**
* Test class for the GameResource REST controller.
*
* @see GameResource
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = GamesApp.class)
public class GameResourceIntTest {
private static final String DEFAULT_NAME = "AAAAAAAAAA";
private static final String UPDATED_NAME = "BBBBBBBBBB";
private static final String DEFAULT_DESCRIPTION = "AAAAAAAAAA";
private static final String UPDATED_DESCRIPTION = "BBBBBBBBBB";
private static final String DEFAULT_GENRE = "AAAAAAAAAA";
private static final String UPDATED_GENRE = "BBBBBBBBBB";
private static final String DEFAULT_URL = "AAAAAAAAAA";
private static final String UPDATED_URL = "BBBBBBBBBB";
private static final Integer DEFAULT_RANK = 0;
private static final Integer UPDATED_RANK = 1;
private static final LocalDate DEFAULT_CREATION_DATE = LocalDate.ofEpochDay(0L);
private static final LocalDate UPDATED_CREATION_DATE = LocalDate.now(ZoneId.systemDefault());
@Autowired
private GameRepository gameRepository;
@Autowired
private GameService gameService;
@Autowired
private MappingJackson2HttpMessageConverter jacksonMessageConverter;
@Autowired
private PageableHandlerMethodArgumentResolver pageableArgumentResolver;
@Autowired
private ExceptionTranslator exceptionTranslator;
private MockMvc restGameMockMvc;
private Game game;
@Before
public void setup() {
MockitoAnnotations.initMocks(this);
GameResource gameResource = new GameResource(gameService);
this.restGameMockMvc = MockMvcBuilders.standaloneSetup(gameResource)
.setCustomArgumentResolvers(pageableArgumentResolver)
.setControllerAdvice(exceptionTranslator)
.setMessageConverters(jacksonMessageConverter).build();
}
/**
* Create an entity for this test.
*
* This is a static method, as tests for other entities might also need it,
* if they test an entity which requires the current entity.
*/
public static Game createEntity() {
Game game = new Game()
.creationDate(DEFAULT_CREATION_DATE)
.description(DEFAULT_DESCRIPTION)
.genre(DEFAULT_GENRE)
.name(DEFAULT_NAME)
.rank(DEFAULT_RANK)
.url(DEFAULT_URL);
return game;
}
@Before
public void initTest() {
gameRepository.deleteAll();
game = createEntity();
}
@Test
public void createGame() throws Exception {
int databaseSizeBeforeCreate = gameRepository.findAll().size();
// Create the Game
restGameMockMvc.perform(post("/api/games")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(game)))
.andExpect(status().isCreated());
// Validate the Game in the database
List<Game> gameList = gameRepository.findAll();
assertThat(gameList).hasSize(databaseSizeBeforeCreate + 1);
Game testGame = gameList.get(gameList.size() - 1);
assertThat(testGame.getCreationDate()).isEqualTo(DEFAULT_CREATION_DATE);
assertThat(testGame.getDescription()).isEqualTo(DEFAULT_DESCRIPTION);
assertThat(testGame.getGenre()).isEqualTo(DEFAULT_GENRE);
assertThat(testGame.getName()).isEqualTo(DEFAULT_NAME);
assertThat(testGame.getRank()).isEqualTo(DEFAULT_RANK);
assertThat(testGame.getUrl()).isEqualTo(DEFAULT_URL);
}
@Test
public void createGameWithExistingId() throws Exception {
int databaseSizeBeforeCreate = gameRepository.findAll().size();
// Create the Game with an existing ID
game.setId("existing_id");
// An entity with an existing ID cannot be created, so this API call must fail
restGameMockMvc.perform(post("/api/games")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(game)))
.andExpect(status().isBadRequest());
// Validate the Alice in the database
List<Game> gameList = gameRepository.findAll();
assertThat(gameList).hasSize(databaseSizeBeforeCreate);
}
@Test
public void checkNameIsRequired() throws Exception {
int databaseSizeBeforeTest = gameRepository.findAll().size();
// set the field null
game.setName(null);
// Create the Game, which fails.
restGameMockMvc.perform(post("/api/games")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(game)))
.andExpect(status().isBadRequest());
List<Game> gameList = gameRepository.findAll();
assertThat(gameList).hasSize(databaseSizeBeforeTest);
}
@Test
public void checkDescriptionIsRequired() throws Exception {
int databaseSizeBeforeTest = gameRepository.findAll().size();
// set the field null
game.setDescription(null);
// Create the Game, which fails.
restGameMockMvc.perform(post("/api/games")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(game)))
.andExpect(status().isBadRequest());
List<Game> gameList = gameRepository.findAll();
assertThat(gameList).hasSize(databaseSizeBeforeTest);
}
@Test
public void checkUrlIsRequired() throws Exception {
int databaseSizeBeforeTest = gameRepository.findAll().size();
// set the field null
game.setUrl(null);
// Create the Game, which fails.
restGameMockMvc.perform(post("/api/games")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(game)))
.andExpect(status().isBadRequest());
List<Game> gameList = gameRepository.findAll();
assertThat(gameList).hasSize(databaseSizeBeforeTest);
}
@Test
public void checkRankIsRequired() throws Exception {
int databaseSizeBeforeTest = gameRepository.findAll().size();
// set the field null
game.setUrl(null);
// Create the Game, which fails.
restGameMockMvc.perform(post("/api/games")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(game)))
.andExpect(status().isBadRequest());
List<Game> gameList = gameRepository.findAll();
assertThat(gameList).hasSize(databaseSizeBeforeTest);
}
@Test
public void checkCreationDateIsRequired() throws Exception {
int databaseSizeBeforeTest = gameRepository.findAll().size();
// set the field null
game.setCreationDate(null);
// Create the Game, which fails.
restGameMockMvc.perform(post("/api/games")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(game)))
.andExpect(status().isBadRequest());
List<Game> gameList = gameRepository.findAll();
assertThat(gameList).hasSize(databaseSizeBeforeTest);
}
@Test
public void getAllGames() throws Exception {
// Initialize the database
gameRepository.save(game);
// Get all the gameList
restGameMockMvc.perform(get("/api/games?sort=id,desc"))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$.[*].id").value(hasItem(game.getId())))
.andExpect(jsonPath("$.[*].name").value(hasItem(DEFAULT_NAME.toString())))
.andExpect(jsonPath("$.[*].description").value(hasItem(DEFAULT_DESCRIPTION.toString())))
.andExpect(jsonPath("$.[*].genre").value(hasItem(DEFAULT_GENRE.toString())))
.andExpect(jsonPath("$.[*].rank").value(hasItem(DEFAULT_RANK)))
.andExpect(jsonPath("$.[*].url").value(hasItem(DEFAULT_URL.toString())))
.andExpect(jsonPath("$.[*].creationDate").value(hasItem(DEFAULT_CREATION_DATE.toString())));
}
@Test
public void getGame() throws Exception {
// Initialize the database
gameRepository.save(game);
// Get the game
restGameMockMvc.perform(get("/api/games/{id}", game.getId()))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8_VALUE))
.andExpect(jsonPath("$.id").value(game.getId()))
.andExpect(jsonPath("$.name").value(DEFAULT_NAME.toString()))
.andExpect(jsonPath("$.description").value(DEFAULT_DESCRIPTION.toString()))
.andExpect(jsonPath("$.genre").value(DEFAULT_GENRE.toString()))
.andExpect(jsonPath("$.rank").value(DEFAULT_RANK))
.andExpect(jsonPath("$.url").value(DEFAULT_URL.toString()))
.andExpect(jsonPath("$.creationDate").value(DEFAULT_CREATION_DATE.toString()));
}
@Test
public void getNonExistingGame() throws Exception {
// Get the game
restGameMockMvc.perform(get("/api/games/{id}", Long.MAX_VALUE))
.andExpect(status().isNotFound());
}
@Test
public void updateGame() throws Exception {
// Initialize the database
gameService.save(game);
int databaseSizeBeforeUpdate = gameRepository.findAll().size();
// Update the game
Game updatedGame = gameRepository.findOne(game.getId());
updatedGame
.creationDate(UPDATED_CREATION_DATE)
.description(UPDATED_DESCRIPTION)
.genre(UPDATED_GENRE)
.name(UPDATED_NAME)
.rank(UPDATED_RANK)
.url(UPDATED_URL);
restGameMockMvc.perform(put("/api/games")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(updatedGame)))
.andExpect(status().isOk());
// Validate the Game in the database
List<Game> gameList = gameRepository.findAll();
assertThat(gameList).hasSize(databaseSizeBeforeUpdate);
Game testGame = gameList.get(gameList.size() - 1);
assertThat(testGame.getCreationDate()).isEqualTo(UPDATED_CREATION_DATE);
assertThat(testGame.getDescription()).isEqualTo(UPDATED_DESCRIPTION);
assertThat(testGame.getGenre()).isEqualTo(UPDATED_GENRE);
assertThat(testGame.getName()).isEqualTo(UPDATED_NAME);
assertThat(testGame.getRank()).isEqualTo(UPDATED_RANK);
assertThat(testGame.getUrl()).isEqualTo(UPDATED_URL);
}
@Test
public void updateNonExistingGame() throws Exception {
int databaseSizeBeforeUpdate = gameRepository.findAll().size();
// If the entity doesn't have an ID, it will be created instead of just being updated
restGameMockMvc.perform(put("/api/games")
.contentType(TestUtil.APPLICATION_JSON_UTF8)
.content(TestUtil.convertObjectToJsonBytes(game)))
.andExpect(status().isCreated());
// Validate the Game in the database
List<Game> gameList = gameRepository.findAll();
assertThat(gameList).hasSize(databaseSizeBeforeUpdate + 1);
}
@Test
public void deleteGame() throws Exception {
// Initialize the database
gameService.save(game);
int databaseSizeBeforeDelete = gameRepository.findAll().size();
// Get the acme
restGameMockMvc.perform(delete("/api/games/{id}", game.getId())
.accept(TestUtil.APPLICATION_JSON_UTF8))
.andExpect(status().isOk());
// Validate the database is empty
List<Game> gameList = gameRepository.findAll();
assertThat(gameList).hasSize(databaseSizeBeforeDelete - 1);
}
@Test
public void equalsVerifier() throws Exception {
TestUtil.equalsVerifier(Game.class);
Game game1 = new Game();
game1.setId("id1");
Game game2 = new Game();
game2.setId(game1.getId());
assertThat(game1).isEqualTo(game2);
game2.setId("id2");
assertThat(game1).isNotEqualTo(game2);
game1.setId(null);
assertThat(game1).isNotEqualTo(game2);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment