Skip to content

Commit

Permalink
added jUnit tests with Mockito and added more checks for lambda methods
Browse files Browse the repository at this point in the history
  • Loading branch information
vladutmargineanu committed Jan 23, 2024
1 parent afd6370 commit 400f990
Show file tree
Hide file tree
Showing 185 changed files with 34,320 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
Expand Down Expand Up @@ -85,9 +86,12 @@ public Optional<Player> getPlayerDetails(String idPlayer) {
* @return
*/
public static boolean checkNewCampaignForPlayer(Player player, CampaignDto campaignDto) {
return player.getCampaigns()
.stream()
.noneMatch(campaign -> campaign.getName().equals(campaignDto.getName()));
if (!CollectionUtils.isEmpty(player.getCampaigns())) {
return player.getCampaigns()
.stream()
.noneMatch(campaign -> campaign.getName().equals(campaignDto.getName()));
}
return true;
}

/**
Expand All @@ -111,40 +115,58 @@ public static boolean matchCurrentCampaign(Player player, CampaignDto campaign)
*
*/
private static final BiPredicate<Player, CampaignDto> matchLevel = (player, campaign) -> {
int playerLevel = player.getLevel();
int minLevel = campaign.getMatchers().getLevel().getMin();
int maxLevel = campaign.getMatchers().getLevel().getMax();

return playerLevel >= minLevel && playerLevel <= maxLevel;
if (null != player.getLevel()) {
int playerLevel = player.getLevel();
;
int minLevel = campaign.getMatchers() != null && campaign.getMatchers().getLevel() != null ?
campaign.getMatchers().getLevel().getMin() : Integer.MAX_VALUE;
int maxLevel = campaign.getMatchers() != null && campaign.getMatchers().getLevel() != null ?
campaign.getMatchers().getLevel().getMax() : Integer.MIN_VALUE;

return playerLevel >= minLevel && playerLevel <= maxLevel;
}
return false;
};

/**
*
*/
private static final BiPredicate<Player, CampaignDto> matchCountry = (player, campaign) -> {
String playerCountry = player.getCountry();
List<String> campaignCountries = campaign.getMatchers().getHas().getCountry();
if (null != player.getCountry()) {
String playerCountry = player.getCountry();
List<String> campaignCountries = null != campaign.getMatchers() && null != campaign.getMatchers().getHas() ?
campaign.getMatchers().getHas().getCountry() : Collections.emptyList();

return campaignCountries.contains(playerCountry);
return campaignCountries.contains(playerCountry);
}
return false;
};

/**
*
*/
private static final BiPredicate<Player, CampaignDto> matchItems = (player, campaign) -> {
List<Item> playerItems = player.getInventory().getItems();
List<String> campaignItems = campaign.getMatchers().getHas().getItems();
if (null != player.getInventory() && !CollectionUtils.isEmpty(player.getInventory().getItems())) {
List<Item> playerItems = player.getInventory().getItems();
List<String> campaignItems = null != campaign.getMatchers() && null != campaign.getMatchers().getHas() ?
campaign.getMatchers().getHas().getItems() : Collections.emptyList();

return playerItems.stream().map(Item::getName).anyMatch(campaignItems::contains);
return playerItems.stream().map(Item::getName).anyMatch(campaignItems::contains);
}
return false;
};

/**
*
*/
private static final BiPredicate<Player, CampaignDto> matchDoesNotHaveItems = (player, campaign) -> {
List<Item> playerItems = player.getInventory().getItems();
List<String> campaignDoesNotHaveItems = campaign.getMatchers().getDoesNotHave().getItems();
if (null != player.getInventory() && !CollectionUtils.isEmpty(player.getInventory().getItems())) {
List<Item> playerItems = player.getInventory().getItems();
List<String> campaignDoesNotHaveItems = null != campaign.getMatchers() && null != campaign.getMatchers().getDoesNotHave() ?
campaign.getMatchers().getDoesNotHave().getItems() : Collections.emptyList();

return playerItems.stream().map(Item::getName).noneMatch(campaignDoesNotHaveItems::contains);
return playerItems.stream().map(Item::getName).noneMatch(campaignDoesNotHaveItems::contains);
}
return false;
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.profile.matcher.assembler;

import com.profile.matcher.dto.player.ClanDto;
import com.profile.matcher.dto.player.DeviceDto;
import com.profile.matcher.entity.campaign.Campaign;
import com.profile.matcher.entity.player.Clan;
import com.profile.matcher.entity.player.Device;
import com.profile.matcher.entity.player.Inventory;
import com.profile.matcher.entity.player.Player;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.Collections;
import java.util.HashMap;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.when;

public class PlayerAssemblerTest {

@InjectMocks
private PlayerAssembler assembler;

@Mock
private DeviceAssembler deviceAssembler;
@Mock
private InventoryAssembler inventoryAssembler;
@Mock
private ClanAssembler clanAssembler;

@BeforeEach
public void setUp() {
assembler = new PlayerAssembler();
MockitoAnnotations.initMocks(this);
}

@Test
void toResourceTest() {
Player player = new Player();
player.setClan(new Clan());
player.setDevices(Collections.singletonList(new Device()));
player.setInventory(new Inventory());
Campaign campaign = new Campaign();
campaign.setName("mygame");
player.setCampaigns(Collections.singletonList(campaign));

when(deviceAssembler.toCollectionResource(player.getDevices())).thenReturn(Collections.singletonList(new DeviceDto()));
when(inventoryAssembler.toResource(player.getInventory())).thenReturn(new HashMap<>(Collections.singletonMap("item_22", "34")));
when(clanAssembler.toResource(player.getClan())).thenReturn(new ClanDto());

assertNotNull(assembler.toResource(player));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package com.profile.matcher.command;

import com.profile.matcher.entity.player.Player;
import com.profile.matcher.service.PlayerService;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.when;

public class PlayerCommandTest {
@InjectMocks
private PlayerCommand command;

@Mock
private PlayerService playerService;

private final String idPlayer = "test";

@BeforeEach
public void setUp() {
command = new PlayerCommand(idPlayer);
MockitoAnnotations.initMocks(this);
}

@Test
public void doExecuteTest() {
when(playerService.getPlayerDetails(idPlayer)).thenReturn(Optional.of(new Player()));
assertNotNull(command.doExecute());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
package com.profile.matcher.controller;

import com.profile.matcher.assembler.PlayerAssembler;
import com.profile.matcher.command.PlayerCommand;
import com.profile.matcher.dto.campaign.CampaignDto;
import com.profile.matcher.dto.player.ClanDto;
import com.profile.matcher.dto.player.DeviceDto;
import com.profile.matcher.dto.player.PlayerDto;
import com.profile.matcher.entity.campaign.Campaign;
import com.profile.matcher.entity.player.Clan;
import com.profile.matcher.entity.player.Device;
import com.profile.matcher.entity.player.Inventory;
import com.profile.matcher.entity.player.Player;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.http.ResponseEntity;

import java.util.Collections;
import java.util.Objects;
import java.util.Optional;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.when;

public class PlayerControllerTest {
@InjectMocks
private PlayerController controller;

@Mock
private PlayerAssembler playerAssembler;

@Mock
private PlayerCommand playerCommand;

@Mock
protected BeanFactory beanFactory;

@BeforeEach
public void setUp() {
controller = new PlayerController();
MockitoAnnotations.initMocks(this);
}

@Test
public void getClientConfigTest() {
Player player = new Player();
player.setClan(new Clan());
player.setDevices(Collections.singletonList(new Device()));
player.setInventory(new Inventory());
Campaign campaign = new Campaign();
campaign.setName("mygame");
player.setCampaigns(Collections.singletonList(campaign));

PlayerDto playerDto = new PlayerDto();
playerDto.setClan(new ClanDto());
playerDto.setDevices(Collections.singletonList(new DeviceDto()));
playerDto.setInventory(Collections.singletonMap("item_22", "45"));
CampaignDto campaignDto = new CampaignDto();
campaignDto.setName("mygame");
playerDto.setActive_campaigns(Collections.singletonList(campaignDto.getName()));

when(beanFactory.getBean(PlayerCommand.class, "test")).thenReturn(playerCommand);
when(playerCommand.execute()).thenReturn(Optional.of(player));
when(playerAssembler.toResource(player)).thenReturn(playerDto);

ResponseEntity<PlayerDto> playerDtoResponseEntity = controller.getClientConfig("test");
assertNotNull(playerDtoResponseEntity);
assertEquals(Objects.requireNonNull(playerDtoResponseEntity.getBody()).getActive_campaigns(), playerDto.getActive_campaigns());
}

@Test
public void getClientConfigTest_EmptyResponse() {
when(beanFactory.getBean(PlayerCommand.class, "test")).thenReturn(playerCommand);
when(playerCommand.execute()).thenReturn(Optional.empty());

ResponseEntity<PlayerDto> playerDtoResponseEntity = controller.getClientConfig("test");
assertNotNull(playerDtoResponseEntity);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package com.profile.matcher.service;

import com.profile.matcher.dto.campaign.CampaignDto;
import com.profile.matcher.repository.CampaignRepository;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.util.List;

import static org.junit.jupiter.api.Assertions.assertNotNull;

public class CampaignServiceTest {
@InjectMocks
private CampaignService service;

@Mock
private CampaignRepository campaignRepository;

@BeforeEach
public void setUp() {
service = new CampaignService();
MockitoAnnotations.initMocks(this);
}

@Test
public void getCurrentCampaignsMockedServiceTest() {
List<CampaignDto> response = service.getCurrentCampaignsMockedService();
assertNotNull(response);
}

@Test
public void getCurrentCampaignsRealServiceTest() {
List<CampaignDto> response = service.getCurrentCampaignsRealService();
assertNotNull(response);
}
}
Loading

0 comments on commit 400f990

Please sign in to comment.