|
| 1 | +package org.apereo.openlrw.risk; |
| 2 | + |
| 3 | +import org.apereo.model.oneroster.Result; |
| 4 | +import org.apereo.openlrw.oneroster.TestData; |
| 5 | +import org.apereo.openlrw.oneroster.exception.LineItemNotFoundException; |
| 6 | +import org.apereo.openlrw.oneroster.exception.OrgNotFoundException; |
| 7 | +import org.apereo.openlrw.oneroster.exception.ResultNotFoundException; |
| 8 | +import org.apereo.openlrw.risk.endpoint.RiskController; |
| 9 | +import org.apereo.openlrw.risk.service.RiskService; |
| 10 | +import org.apereo.openlrw.security.auth.JwtAuthenticationToken; |
| 11 | +import org.apereo.openlrw.security.model.UserContext; |
| 12 | +import org.junit.Before; |
| 13 | +import org.junit.Test; |
| 14 | +import org.junit.runner.RunWith; |
| 15 | +import org.mockito.InjectMocks; |
| 16 | +import org.mockito.Mock; |
| 17 | +import org.mockito.MockitoAnnotations; |
| 18 | +import org.springframework.boot.test.context.SpringBootTest; |
| 19 | +import org.springframework.boot.test.mock.mockito.MockBean; |
| 20 | +import org.springframework.data.crossstore.ChangeSetPersister; |
| 21 | +import org.springframework.security.core.GrantedAuthority; |
| 22 | +import org.springframework.security.core.authority.SimpleGrantedAuthority; |
| 23 | +import org.springframework.test.context.junit4.SpringJUnit4ClassRunner; |
| 24 | + |
| 25 | +import java.util.ArrayList; |
| 26 | +import java.util.Collection; |
| 27 | +import java.util.List; |
| 28 | + |
| 29 | +import static org.junit.Assert.assertTrue; |
| 30 | +import static org.mockito.Mockito.when; |
| 31 | + |
| 32 | +/** |
| 33 | + * @author xchopin <xavier.chopin@univ-lorraine.fr> |
| 34 | + */ |
| 35 | +@RunWith(SpringJUnit4ClassRunner.class) |
| 36 | +@SpringBootTest |
| 37 | +public class RiskControllerTest { |
| 38 | + |
| 39 | + @Mock |
| 40 | + private RiskService riskService; |
| 41 | + |
| 42 | + @InjectMocks |
| 43 | + private RiskController riskController; |
| 44 | + |
| 45 | + @MockBean |
| 46 | + JwtAuthenticationToken jwtToken; |
| 47 | + |
| 48 | + MongoRisk risk = new MongoRisk.Builder().withSourcedId("risk-id") |
| 49 | + .withActive(true) |
| 50 | + .withClassSourcedId(TestData.CLASS_SOURCED_ID) |
| 51 | + .withUserSourcedId(TestData.USER_SOURCED_ID) |
| 52 | + .withVelocity("-1.0") |
| 53 | + .withName("Computer Science - Semester 1 Risk") |
| 54 | + .build(); |
| 55 | + |
| 56 | + |
| 57 | + @Before |
| 58 | + public void init() throws OrgNotFoundException, LineItemNotFoundException { |
| 59 | + MockitoAnnotations.initMocks(this); |
| 60 | + riskController = new RiskController(null, riskService); |
| 61 | + List<GrantedAuthority> authorities = new ArrayList<GrantedAuthority>(); |
| 62 | + authorities.add(new SimpleGrantedAuthority("ROLE_TENANT_ADMIN")); |
| 63 | + UserContext context = UserContext.create(TestData.TENANT_1, "01", authorities); |
| 64 | + when(jwtToken.getPrincipal()).thenReturn(context); |
| 65 | + riskController.post(jwtToken, risk, true); |
| 66 | + } |
| 67 | + |
| 68 | + |
| 69 | + @Test(expected = ChangeSetPersister.NotFoundException.class) |
| 70 | + public void testGetResultForUserResultNotFoundException() throws Exception { |
| 71 | + String userSourcedId = "jo zimmerman"; |
| 72 | + String classSourcedId = "dead mow cinco"; |
| 73 | + when(riskService.getRisksForUserAndClass(TestData.TENANT_1, "*", classSourcedId, userSourcedId, "")).thenThrow(ChangeSetPersister.NotFoundException.class); |
| 74 | + riskController.getClassUser(jwtToken, classSourcedId, userSourcedId, ""); |
| 75 | + } |
| 76 | + |
| 77 | + |
| 78 | +} |
0 commit comments