Skip to content

Commit

Permalink
Updated BSM, SPaT, Map Unit Tests
Browse files Browse the repository at this point in the history
  • Loading branch information
John-Wiens committed Jul 15, 2024
1 parent 81331dd commit afec827
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public String getCurrentTime() {

@CrossOrigin(origins = "http://localhost:3000")
@RequestMapping(value = "/bsm/json", method = RequestMethod.GET, produces = "application/json")
@PreAuthorize("hasRole('USER') || hasRole('ADMIN')")
@PreAuthorize("@PermissionService.hasIntersection(#intersectionID) and (@PermissionService.hasRole('USER') || @PermissionService.hasRole('ADMIN'))")
public ResponseEntity<List<OdeBsmData>> findBSMs(
@RequestParam(name = "origin_ip", required = false) String originIp,
@RequestParam(name = "vehicle_id", required = false) String vehicleId,
Expand All @@ -61,7 +61,7 @@ public ResponseEntity<List<OdeBsmData>> findBSMs(

@CrossOrigin(origins = "http://localhost:3000")
@RequestMapping(value = "/bsm/count", method = RequestMethod.GET, produces = "application/json")
@PreAuthorize("hasRole('USER') || hasRole('ADMIN')")
@PreAuthorize("@PermissionService.hasIntersection(#intersectionID) and (@PermissionService.hasRole('USER') || @PermissionService.hasRole('ADMIN'))")
public ResponseEntity<Long> countBSMs(
@RequestParam(name = "origin_ip", required = false) String originIp,
@RequestParam(name = "vehicle_id", required = false) String vehicleId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ public String getCurrentTime() {

@CrossOrigin(origins = "http://localhost:3000")
@RequestMapping(value = "/map/json", method = RequestMethod.GET, produces = "application/json")
@PreAuthorize("hasRole('USER') || hasRole('ADMIN')")
@PreAuthorize("@PermissionService.hasIntersection(#intersectionID) and (@PermissionService.hasRole('USER') || @PermissionService.hasRole('ADMIN'))")
public ResponseEntity<List<ProcessedMap<LineString>>> findMaps(
@RequestParam(name = "intersection_id", required = false) Integer intersectionID,
@RequestParam(name = "start_time_utc_millis", required = false) Long startTime,
Expand All @@ -64,7 +64,7 @@ public ResponseEntity<List<ProcessedMap<LineString>>> findMaps(

@CrossOrigin(origins = "http://localhost:3000")
@RequestMapping(value = "/map/count", method = RequestMethod.GET, produces = "application/json")
@PreAuthorize("hasRole('USER') || hasRole('ADMIN')")
@PreAuthorize("@PermissionService.hasIntersection(#intersectionID) and (@PermissionService.hasRole('USER') || @PermissionService.hasRole('ADMIN'))")
public ResponseEntity<Long> countMaps(
@RequestParam(name = "intersection_id", required = false) Integer intersectionID,
@RequestParam(name = "start_time_utc_millis", required = false) Long startTime,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,6 @@ public String getCurrentTime() {

@CrossOrigin(origins = "http://localhost:3000")
@RequestMapping(value = "/spat/json", method = RequestMethod.GET, produces = "application/json")
// @PreAuthorize("hasRole('USER') || hasRole('ADMIN')")
// @PreAuthorize("hasPermission( 'CONFLICT_VISUALIZER', 'ADMIN')")
// @PreAuthorize("isMember(#intersectionID)")
@PreAuthorize("@PermissionService.hasIntersection(#intersectionID) and (@PermissionService.hasRole('USER') || @PermissionService.hasRole('ADMIN'))")
public ResponseEntity<List<ProcessedSpat>> findSpats(
@RequestParam(name = "intersection_id", required = false) Integer intersectionID,
Expand All @@ -66,7 +63,7 @@ public ResponseEntity<List<ProcessedSpat>> findSpats(

@CrossOrigin(origins = "http://localhost:3000")
@RequestMapping(value = "/spat/count", method = RequestMethod.GET, produces = "application/json")
@PreAuthorize("hasRole('USER') || hasRole('ADMIN')")
@PreAuthorize("@PermissionService.hasIntersection(#intersectionID) and (@PermissionService.hasRole('USER') || @PermissionService.hasRole('ADMIN'))")
public ResponseEntity<Long> countSpats(
@RequestParam(name = "intersection_id", required = false) Integer intersectionID,
@RequestParam(name = "start_time_utc_millis", required = false) Long startTime,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package us.dot.its.jpo.ode.api;

import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.List;
Expand All @@ -17,6 +18,8 @@

import us.dot.its.jpo.ode.api.accessors.bsm.OdeBsmJsonRepository;
import us.dot.its.jpo.ode.api.controllers.BsmController;
import us.dot.its.jpo.ode.api.models.postgres.derived.UserOrgRole;
import us.dot.its.jpo.ode.api.services.PostgresService;
import us.dot.its.jpo.ode.model.OdeBsmData;


Expand All @@ -29,13 +32,22 @@ public class BsmTest {

@MockBean
OdeBsmJsonRepository odeBsmJsonRepository;

@MockBean
PostgresService postgresService;



@Test
public void testBsmJson() {

MockKeyCloakAuth.setSecurityContextHolder("cm_user", Set.of("USER"));
MockKeyCloakAuth.setSecurityContextHolder("cm_user@cimms.com", Set.of("USER"));

List <UserOrgRole> roles = new ArrayList<>();
UserOrgRole userOrgRole = new UserOrgRole("cm_user@cimms.com", "test", "USER");

roles.add(userOrgRole);
when(postgresService.findUserOrgRoles("cm_user@cimms.com")).thenReturn(roles);

List<OdeBsmData> list = new ArrayList<>();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

Expand All @@ -20,6 +21,8 @@
import us.dot.its.jpo.geojsonconverter.pojos.geojson.map.ProcessedMap;
import us.dot.its.jpo.ode.api.accessors.map.ProcessedMapRepository;
import us.dot.its.jpo.ode.api.controllers.MapController;
import us.dot.its.jpo.ode.api.models.postgres.derived.UserOrgRole;
import us.dot.its.jpo.ode.api.services.PostgresService;
import us.dot.its.jpo.ode.mockdata.MockMapGenerator;


Expand All @@ -32,13 +35,21 @@ public class MapTest {

@MockBean
ProcessedMapRepository processedMapRepo;


@MockBean
PostgresService postgresService;


@Test
public void testProcessedMap() {

MockKeyCloakAuth.setSecurityContextHolder("cm_user", Set.of("USER"));
MockKeyCloakAuth.setSecurityContextHolder("cm_user@cimms.com", Set.of("USER"));

List <UserOrgRole> roles = new ArrayList<>();
UserOrgRole userOrgRole = new UserOrgRole("cm_user@cimms.com", "test", "USER");

roles.add(userOrgRole);
when(postgresService.findUserOrgRoles("cm_user@cimms.com")).thenReturn(roles);


List<ProcessedMap<LineString>> list = MockMapGenerator.getProcessedMaps();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
package us.dot.its.jpo.ode.api;

import java.security.Principal;
import java.time.Instant;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.oauth2.jwt.Jwt;
import org.springframework.security.oauth2.server.resource.authentication.JwtAuthenticationToken;

import static org.mockito.Mockito.*;

Expand All @@ -17,8 +22,21 @@ public static void setSecurityContextHolder(String user, Set<String> roles){
Principal principal = mock(Principal.class);
when(principal.getName()).thenReturn(user);

Authentication authentication = mock(Authentication.class);
List<String> allowedOrigins = new ArrayList<>();
allowedOrigins.add("http://localhost:3000");

Map<String, Object> headers = new HashMap<>();
headers.put("allowed-origins", allowedOrigins);

Map<String, Object> claims = new HashMap<>();
claims.put("preferred_username", user);


Jwt token = new Jwt(user, Instant.now(), Instant.now().plusSeconds(10), headers, claims);

JwtAuthenticationToken authentication = mock(JwtAuthenticationToken.class);
when(authentication.isAuthenticated()).thenReturn(true);
when(authentication.getToken()).thenReturn(token);
Collection<GrantedAuthority> authorities = new ArrayList<>();
for (String role : roles) {
authorities.add(new SimpleGrantedAuthority("ROLE_" + role));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

Expand All @@ -19,6 +20,8 @@
import us.dot.its.jpo.geojsonconverter.pojos.spat.ProcessedSpat;
import us.dot.its.jpo.ode.api.accessors.spat.ProcessedSpatRepository;
import us.dot.its.jpo.ode.api.controllers.SpatController;
import us.dot.its.jpo.ode.api.models.postgres.derived.UserOrgRole;
import us.dot.its.jpo.ode.api.services.PostgresService;
import us.dot.its.jpo.ode.mockdata.MockSpatGenerator;


Expand All @@ -32,12 +35,19 @@ public class SpatTest {
@MockBean
ProcessedSpatRepository processedSpatRepo;


@MockBean
PostgresService postgresService;

@Test
public void testProcessedSpat() {

MockKeyCloakAuth.setSecurityContextHolder("cm_user", Set.of("USER"));
MockKeyCloakAuth.setSecurityContextHolder("cm_user@cimms.com", Set.of("USER"));

List <UserOrgRole> roles = new ArrayList<>();
UserOrgRole userOrgRole = new UserOrgRole("cm_user@cimms.com", "test", "USER");

roles.add(userOrgRole);
when(postgresService.findUserOrgRoles("cm_user@cimms.com")).thenReturn(roles);

List<ProcessedSpat> list = MockSpatGenerator.getProcessedSpats();

Expand Down

0 comments on commit afec827

Please sign in to comment.