Checkpoint: Added ExchangeServiceTest
Signed-off-by: Marcin Woźniak <y0rune@aol.com>
This commit is contained in:
		@@ -19,6 +19,11 @@ public class MatchData {
 | 
			
		||||
	return assignmentOne;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public int hashCode() {
 | 
			
		||||
	return this.assignmentOne.hashCode() + this.assignmentTwo.hashCode();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public boolean equals(Object o) {
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -1,7 +1,10 @@
 | 
			
		||||
package com.plannaplan.services;
 | 
			
		||||
 | 
			
		||||
import java.util.HashSet;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Optional;
 | 
			
		||||
import java.util.Set;
 | 
			
		||||
import java.util.stream.Collectors;
 | 
			
		||||
 | 
			
		||||
import com.plannaplan.entities.Assignment;
 | 
			
		||||
import com.plannaplan.entities.Exchange;
 | 
			
		||||
@@ -62,9 +65,14 @@ public class ExchangeService {
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
//    public void getMatches(){
 | 
			
		||||
//	final List<MatchData> matches = this.repo.getMatches().stream().map(m -> {
 | 
			
		||||
//		return (MatchData) m;
 | 
			
		||||
//	    });
 | 
			
		||||
//    }
 | 
			
		||||
   public Set<MatchData> getMatches(){
 | 
			
		||||
       final List<MatchData> matches = this.repo.getMatches().stream().map(m -> {
 | 
			
		||||
	       final Exchange exchangeOne = (Exchange) m[0];
 | 
			
		||||
	       final Exchange exchangeTwo = (Exchange) m[1];
 | 
			
		||||
	       return new MatchData(exchangeOne.getOwnedAssignment(), exchangeTwo.getOwnedAssignment());
 | 
			
		||||
	   }).collect(Collectors.toList());
 | 
			
		||||
 | 
			
		||||
       final Set<MatchData> filterMatches = new HashSet<>(matches);
 | 
			
		||||
       return filterMatches;
 | 
			
		||||
   }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -2,9 +2,7 @@ package com.plannaplan.repositories;
 | 
			
		||||
 | 
			
		||||
import static org.junit.Assert.assertTrue;
 | 
			
		||||
 | 
			
		||||
import java.util.HashSet;
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Set;
 | 
			
		||||
 | 
			
		||||
import com.plannaplan.entities.Assignment;
 | 
			
		||||
import com.plannaplan.entities.Commision;
 | 
			
		||||
 
 | 
			
		||||
@@ -0,0 +1,66 @@
 | 
			
		||||
package com.plannaplan.services;
 | 
			
		||||
 | 
			
		||||
import static org.junit.Assert.assertTrue;
 | 
			
		||||
 | 
			
		||||
import java.util.Set;
 | 
			
		||||
 | 
			
		||||
import com.plannaplan.entities.Assignment;
 | 
			
		||||
import com.plannaplan.entities.Commision;
 | 
			
		||||
import com.plannaplan.entities.Exchange;
 | 
			
		||||
import com.plannaplan.entities.Groups;
 | 
			
		||||
import com.plannaplan.entities.User;
 | 
			
		||||
import com.plannaplan.models.MatchData;
 | 
			
		||||
import com.plannaplan.repositories.CommisionRepository;
 | 
			
		||||
import com.plannaplan.types.UserRoles;
 | 
			
		||||
import com.plannaplan.types.WeekDay;
 | 
			
		||||
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import org.junit.runner.RunWith;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.boot.test.context.SpringBootTest;
 | 
			
		||||
import org.springframework.test.annotation.DirtiesContext;
 | 
			
		||||
import org.springframework.test.annotation.DirtiesContext.MethodMode;
 | 
			
		||||
import org.springframework.test.context.ContextConfiguration;
 | 
			
		||||
import org.springframework.test.context.junit4.SpringRunner;
 | 
			
		||||
 | 
			
		||||
@RunWith(SpringRunner.class)
 | 
			
		||||
@SpringBootTest
 | 
			
		||||
@ContextConfiguration
 | 
			
		||||
public class ExchangeServiceTest {
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private AssignmentService assignmentService;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private GroupService groupService;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private UserService userService;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private CommisionRepository commisionRepository;
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private ExchangeService exchangeService;
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    @DirtiesContext(methodMode = MethodMode.BEFORE_METHOD)
 | 
			
		||||
    public void shouldReturnUniqMatches() {
 | 
			
		||||
	final User user1 = this.userService.save(new User(null, null, "shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT , 451));
 | 
			
		||||
	final Groups group1 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.MONDAY, null));
 | 
			
		||||
	final Commision commision1 = this.commisionRepository.save(new Commision(user1));
 | 
			
		||||
	final User user2 = this.userService.save(new User(null, null, "shouldReturnMatches@ExchangeRepository.test", "123454", UserRoles.STUDENT , 451));
 | 
			
		||||
	final Groups group2 = this.groupService.save(new Groups(123, "A2-3", null, 430, WeekDay.MONDAY, null));
 | 
			
		||||
	final Commision commision2 = this.commisionRepository.save(new Commision(user2));
 | 
			
		||||
	final Assignment assignmentUser1 = this.assignmentService.save(new Assignment(group1, commision1));
 | 
			
		||||
	final Assignment assignmentUser2 = this.assignmentService.save(new Assignment(group2, commision2));
 | 
			
		||||
 | 
			
		||||
	this.assignmentService.callAcceptAlgorythm();
 | 
			
		||||
 | 
			
		||||
	this.exchangeService.save(new Exchange(assignmentUser1, group2));
 | 
			
		||||
	this.exchangeService.save(new Exchange(assignmentUser2, group1));
 | 
			
		||||
 | 
			
		||||
	final Set<MatchData> uniqList = this.exchangeService.getMatches();
 | 
			
		||||
	assertTrue(uniqList.size() == 1);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@@ -30,11 +30,8 @@ import org.springframework.web.bind.annotation.RequestMapping;
 | 
			
		||||
import org.springframework.web.bind.annotation.RestController;
 | 
			
		||||
 | 
			
		||||
import io.swagger.annotations.Api;
 | 
			
		||||
import io.swagger.annotations.ApiModelProperty;
 | 
			
		||||
import io.swagger.annotations.ApiOperation;
 | 
			
		||||
import io.swagger.annotations.ApiParam;
 | 
			
		||||
import io.swagger.annotations.Example;
 | 
			
		||||
import io.swagger.annotations.ExampleProperty;
 | 
			
		||||
 | 
			
		||||
@RestController
 | 
			
		||||
@CrossOrigin
 | 
			
		||||
@@ -47,13 +44,13 @@ public class ExchangeController extends TokenBasedController{
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private AssignmentService assignmentService;
 | 
			
		||||
    
 | 
			
		||||
 | 
			
		||||
    @Autowired
 | 
			
		||||
    private ExchangeService exchangeService;
 | 
			
		||||
 | 
			
		||||
    @PostMapping("/exchange")
 | 
			
		||||
    @ApiOperation(value = "Creates exchange offer.")
 | 
			
		||||
    public ResponseEntity<String> createExchange(   
 | 
			
		||||
    public ResponseEntity<String> createExchange(
 | 
			
		||||
        @ApiParam( value = "Json object that contains assignment to trade and desired group")
 | 
			
		||||
        @RequestBody
 | 
			
		||||
        Map<String, Long> exchangeRequest)
 | 
			
		||||
@@ -63,7 +60,7 @@ public class ExchangeController extends TokenBasedController{
 | 
			
		||||
                                        .orElseThrow(() -> new UserNotFoundException("Invalid token"));
 | 
			
		||||
        final Long assignmentId = exchangeRequest.get("assignment");
 | 
			
		||||
        final Long groupId = exchangeRequest.get("group");
 | 
			
		||||
        final List<Long> ownedGroups = asker.getStudentRegisteredGrups().stream().map(Groups::getId).collect(Collectors.toList()); 
 | 
			
		||||
        final List<Long> ownedGroups = asker.getStudentRegisteredGrups().stream().map(Groups::getId).collect(Collectors.toList());
 | 
			
		||||
 | 
			
		||||
        if(ownedGroups.contains(groupId)){
 | 
			
		||||
            return new ResponseEntity<>("User has already got this group.", HttpStatus.BAD_REQUEST);
 | 
			
		||||
@@ -75,7 +72,7 @@ public class ExchangeController extends TokenBasedController{
 | 
			
		||||
 | 
			
		||||
        final Optional<Assignment> assignment = this.assignmentService.getById(assignmentId);
 | 
			
		||||
        final Optional<Groups> group = this.groupService.getGroupById(groupId);
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        if(assignment.isEmpty() || group.isEmpty()){
 | 
			
		||||
            return new ResponseEntity<>("Some of provided value does not exist.", HttpStatus.BAD_REQUEST);
 | 
			
		||||
        }
 | 
			
		||||
@@ -101,7 +98,7 @@ public class ExchangeController extends TokenBasedController{
 | 
			
		||||
                                        .orElseThrow(() -> new UserNotFoundException("Invalid token"));
 | 
			
		||||
 | 
			
		||||
        final Optional<Exchange> exchange = this.exchangeService.getById(offerId);
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        if(exchange.isEmpty()){
 | 
			
		||||
            return new ResponseEntity<>("Given offer does not exist.",  HttpStatus.BAD_REQUEST);
 | 
			
		||||
        }
 | 
			
		||||
@@ -123,10 +120,10 @@ public class ExchangeController extends TokenBasedController{
 | 
			
		||||
 | 
			
		||||
        final User asker = this.getCurrentUser()
 | 
			
		||||
                                        .orElseThrow(() -> new UserNotFoundException("Invalid token"));
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        final List<Exchange> response = exchangeService.getByUserId(asker.getId());
 | 
			
		||||
        final List<ExchangeResponse> listOfResponses = ExchangeResponseMappers.mapToDefaultResponse(response);
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        return new ResponseEntity<>(listOfResponses, HttpStatus.OK);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@@ -137,7 +134,7 @@ public class ExchangeController extends TokenBasedController{
 | 
			
		||||
 | 
			
		||||
        final User asker = this.getCurrentUser()
 | 
			
		||||
                                        .orElseThrow(() -> new UserNotFoundException("Invalid token"));
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        final Optional<Exchange> exchange = this.exchangeService.getById(offerId);
 | 
			
		||||
 | 
			
		||||
        if(exchange.isEmpty()){
 | 
			
		||||
@@ -146,9 +143,9 @@ public class ExchangeController extends TokenBasedController{
 | 
			
		||||
 | 
			
		||||
        final Exchange exchangeInstance = exchange.get();
 | 
			
		||||
        if(!exchangeInstance.getOwnerId().equals(asker.getId())){
 | 
			
		||||
            return new ResponseEntity<>(null,  HttpStatus.BAD_REQUEST);    
 | 
			
		||||
            return new ResponseEntity<>(null,  HttpStatus.BAD_REQUEST);
 | 
			
		||||
        }
 | 
			
		||||
        
 | 
			
		||||
 | 
			
		||||
        return new ResponseEntity<>(new ExchangeResponse(exchangeInstance), HttpStatus.OK);
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user