@Test void testToUserMissingUserMappingMissingUser() { String email = "e@mail.com"; String username = "username"; String givenName = "MOL"; String familyName = "GENIS"; OidcUser oidcUser = mock(OidcUser.class); when(oidcUser.getEmail()).thenReturn(email); when(oidcUser.getEmailVerified()).thenReturn(true); when(oidcUser.getSubject()).thenReturn(username); when(oidcUser.getGivenName()).thenReturn(givenName); when(oidcUser.getFamilyName()).thenReturn(familyName); String registrationId = "google"; ClientRegistration clientRegistration = CommonOAuth2Provider.GOOGLE .getBuilder(registrationId) .clientId("clientId") .clientSecret("clientSecret") .build(); OidcUserRequest oidcUserRequest = mock(OidcUserRequest.class); when(oidcUserRequest.getClientRegistration()).thenReturn(clientRegistration); OidcClient oidcClient = mock(OidcClient.class); when(dataService.findOneById(OidcClientMetadata.OIDC_CLIENT, registrationId, OidcClient.class)) .thenReturn(oidcClient); @SuppressWarnings("unchecked") Query<User> userQuery = mock(Query.class, RETURNS_SELF); doReturn(userQuery).when(dataService).query(UserMetadata.USER, User.class); when(userQuery.eq(UserMetadata.EMAIL, email).findOne()).thenReturn(null); OidcUserMapping oidcUserMapping = mock(OidcUserMapping.class); @SuppressWarnings("unchecked") Query<OidcUserMapping> oidcUserMappingQuery = mock(Query.class, RETURNS_SELF); doReturn(oidcUserMappingQuery) .when(dataService) .query(OIDC_USER_MAPPING, OidcUserMapping.class); when(oidcUserMappingQuery .eq(OIDC_CLIENT, registrationId) .and() .eq(OIDC_USERNAME, username) .findOne()) .thenReturn(null); when(oidcUserMappingFactory.create()).thenReturn(oidcUserMapping); User user = mock(User.class); when(userFactory.create()).thenReturn(user); assertEquals(user, oidcUserMapperImpl.toUser(oidcUser, oidcUserRequest)); verify(dataService).add(OidcUserMappingMetadata.OIDC_USER_MAPPING, oidcUserMapping); verify(oidcUserMapping).setLabel("google:username"); verify(oidcUserMapping).setOidcClient(oidcClient); verify(oidcUserMapping).setOidcUsername("username"); verify(oidcUserMapping).setUser(user); verify(dataService).add(UserMetadata.USER, user); verify(user).setUsername(email); verify(user).setEmail(email); verify(user).setFirstName(givenName); verify(user).setLastName(familyName); }
@Transactional @Override public User toUser(OidcUser oidcUser, OidcUserRequest userRequest) { verifyOidcUser(oidcUser); return runAsSystem( () -> getUser(oidcUser, userRequest) .orElseGet(() -> createUserMapping(oidcUser, userRequest))); }
OidcUserMapperImpl implements OidcUserMapper { @Transactional @Override public User toUser(OidcUser oidcUser, OidcUserRequest userRequest) { verifyOidcUser(oidcUser); return runAsSystem( () -> getUser(oidcUser, userRequest) .orElseGet(() -> createUserMapping(oidcUser, userRequest))); } }
OidcUserMapperImpl implements OidcUserMapper { @Transactional @Override public User toUser(OidcUser oidcUser, OidcUserRequest userRequest) { verifyOidcUser(oidcUser); return runAsSystem( () -> getUser(oidcUser, userRequest) .orElseGet(() -> createUserMapping(oidcUser, userRequest))); } OidcUserMapperImpl(
      DataService dataService,
      OidcUserMappingFactory oidcUserMappingFactory,
      UserFactory userFactory); }
OidcUserMapperImpl implements OidcUserMapper { @Transactional @Override public User toUser(OidcUser oidcUser, OidcUserRequest userRequest) { verifyOidcUser(oidcUser); return runAsSystem( () -> getUser(oidcUser, userRequest) .orElseGet(() -> createUserMapping(oidcUser, userRequest))); } OidcUserMapperImpl(
      DataService dataService,
      OidcUserMappingFactory oidcUserMappingFactory,
      UserFactory userFactory); @Transactional @Override User toUser(OidcUser oidcUser, OidcUserRequest userRequest); }
OidcUserMapperImpl implements OidcUserMapper { @Transactional @Override public User toUser(OidcUser oidcUser, OidcUserRequest userRequest) { verifyOidcUser(oidcUser); return runAsSystem( () -> getUser(oidcUser, userRequest) .orElseGet(() -> createUserMapping(oidcUser, userRequest))); } OidcUserMapperImpl(
      DataService dataService,
      OidcUserMappingFactory oidcUserMappingFactory,
      UserFactory userFactory); @Transactional @Override User toUser(OidcUser oidcUser, OidcUserRequest userRequest);  }