package com.plannaplan.services; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.boot.test.context.SpringBootTest; import org.springframework.test.context.ContextConfiguration; import org.springframework.test.context.junit4.SpringRunner; import static org.junit.Assert.assertTrue; import com.plannaplan.entities.Assignment; import com.plannaplan.entities.Commision; import com.plannaplan.entities.User; import com.plannaplan.types.UserRoles; import org.junit.Test; import org.junit.runner.RunWith; @RunWith(SpringRunner.class) @SpringBootTest @ContextConfiguration public class CommisionServiceTest { @Autowired private CommisionService service; @Autowired private UserService userService; @Autowired private AssignmentService assignmentService; @Test public void shouldSaveCommision() { User usr = new User(); this.userService.save(usr); long beginState = this.service.getCommisionsAmmount(); this.service.save(new Commision(usr)); assertTrue("Commision ammount should have changed", this.service.getCommisionsAmmount() > beginState); } @Test public void shouldGetUserCommisions() { User usr = new User(); this.userService.save(usr); this.service.save(new Commision(usr)); assertTrue("User should have one commision", this.service.getUsersCommisions(usr).size() == 1); } @Test public void shouldGetNewestCommision() throws NullPointerException, InterruptedException { User usr = new User(); this.userService.save(usr); this.service.save(new Commision(usr)); Thread.sleep(2000); final Commision newestCommision = new Commision(usr); this.service.save(newestCommision); final Commision result = this.service.getNewestCommision(usr) .orElseThrow(() -> new NullPointerException("There was no commision")); assertTrue("Wrong commision was returned", result.getId().equals(newestCommision.getId())); } @Test public void shouldMarkAssignmentsOfPreviousCommisionPast() { final User usr = new User("Trevor", "Hammolt", "shouldMarkAssignmentsOfPreviousCommisionPast@commisionservice.test", UserRoles.STUDENT); this.userService.save(usr); final Commision firstCommision = new Commision(usr); this.service.save(firstCommision); Assignment firstCommisionAssignment = this.assignmentService.save(new Assignment(null, firstCommision)); assertTrue("FirstCommisionAssignment should be flaged as present assignment", !firstCommisionAssignment.isPastAssignment()); final Commision secondCommision = new Commision(usr); this.service.save(secondCommision); Assignment secondCommisionAssignment = new Assignment(null, secondCommision); this.assignmentService.save(secondCommisionAssignment); assertTrue("SecondCommisionAssignment should be flaged as present assignment", !secondCommisionAssignment.isPastAssignment()); assertTrue("FirstCommisionAssignment should be flaged as past assignment", this.assignmentService.getById(firstCommisionAssignment.getId()).get().isPastAssignment()); } }