@Test void testGetAuthenticationCommandByServiceId() { AuthenticationCommand ok = new AuthenticationCommandTest(false); Authentication a1 = new Authentication(AuthenticationScheme.HTTP_BASIC_PASSTICKET, "applid01"); Authentication a2 = new Authentication(AuthenticationScheme.ZOWE_JWT, null); Authentication a3 = new Authentication(AuthenticationScheme.ZOWE_JWT, "applid01"); Authentication a4 = new Authentication(AuthenticationScheme.HTTP_BASIC_PASSTICKET, "applid02"); Authentication a5 = new Authentication(null, null); InstanceInfo ii1 = createInstanceInfo("inst01", a1); InstanceInfo ii2 = createInstanceInfo("inst01", a2); InstanceInfo ii3 = createInstanceInfo("inst01", a3); InstanceInfo ii4 = createInstanceInfo("inst01", a4); InstanceInfo ii5 = createInstanceInfo("inst02", a5); Application application; ServiceAuthenticationService sas = spy(serviceAuthenticationServiceImpl); AbstractAuthenticationScheme scheme = mock(AbstractAuthenticationScheme.class); doAnswer(invocation -> { ((Supplier<?>) invocation.getArgument(1)).get(); return ok; }).when(scheme).createCommand(any(), any()); when(authenticationSchemeFactory.getSchema(any())).thenReturn(scheme); application = createApplication(ii1); when(discoveryClient.getApplication("svr01")).thenReturn(application); assertSame(ok, sas.getAuthenticationCommand("svr01", "jwt01")); application = createApplication(ii1, ii1, ii1); when(discoveryClient.getApplication("svr02")).thenReturn(application); assertSame(ok, sas.getAuthenticationCommand("svr02", "jwt02")); reset(discoveryClient); application = createApplication(ii1, ii2); when(discoveryClient.getApplication("svr03")).thenReturn(application); assertTrue(sas.getAuthenticationCommand("svr03", "jwt03") instanceof ServiceAuthenticationServiceImpl.LoadBalancerAuthenticationCommand); reset(discoveryClient); application = createApplication(ii1, ii3); when(discoveryClient.getApplication("svr03")).thenReturn(application); assertTrue(sas.getAuthenticationCommand("svr03", "jwt03") instanceof ServiceAuthenticationServiceImpl.LoadBalancerAuthenticationCommand); reset(discoveryClient); application = createApplication(ii1, ii4); when(discoveryClient.getApplication("svr03")).thenReturn(application); assertTrue(sas.getAuthenticationCommand("svr03", "jwt03") instanceof ServiceAuthenticationServiceImpl.LoadBalancerAuthenticationCommand); reset(discoveryClient); application = createApplication(ii1, ii2, ii3, ii4); when(discoveryClient.getApplication("svr03")).thenReturn(application); assertTrue(sas.getAuthenticationCommand("svr03", "jwt03") instanceof ServiceAuthenticationServiceImpl.LoadBalancerAuthenticationCommand); reset(discoveryClient); when(discoveryClient.getInstancesById("svr03")).thenReturn(Collections.singletonList(ii5)); assertSame(AuthenticationCommand.EMPTY, sas.getAuthenticationCommand("svr03", "jwt03")); when(discoveryClient.getInstancesById("svr04")).thenReturn(Collections.emptyList()); assertSame(AuthenticationCommand.EMPTY, sas.getAuthenticationCommand("svr04", "jwt03")); }
@Override @CacheEvict(value = CACHE_BY_AUTHENTICATION, condition = "#result != null && #result.isExpired()") @Cacheable(CACHE_BY_AUTHENTICATION) public AuthenticationCommand getAuthenticationCommand(Authentication authentication, String jwtToken) { final AbstractAuthenticationScheme scheme = authenticationSchemeFactory.getSchema(authentication.getScheme()); if (jwtToken == null) return scheme.createCommand(authentication, () -> null); return scheme.createCommand(authentication, () -> authenticationService.parseJwtToken(jwtToken)); }
ServiceAuthenticationServiceImpl implements ServiceAuthenticationService { @Override @CacheEvict(value = CACHE_BY_AUTHENTICATION, condition = "#result != null && #result.isExpired()") @Cacheable(CACHE_BY_AUTHENTICATION) public AuthenticationCommand getAuthenticationCommand(Authentication authentication, String jwtToken) { final AbstractAuthenticationScheme scheme = authenticationSchemeFactory.getSchema(authentication.getScheme()); if (jwtToken == null) return scheme.createCommand(authentication, () -> null); return scheme.createCommand(authentication, () -> authenticationService.parseJwtToken(jwtToken)); } }
ServiceAuthenticationServiceImpl implements ServiceAuthenticationService { @Override @CacheEvict(value = CACHE_BY_AUTHENTICATION, condition = "#result != null && #result.isExpired()") @Cacheable(CACHE_BY_AUTHENTICATION) public AuthenticationCommand getAuthenticationCommand(Authentication authentication, String jwtToken) { final AbstractAuthenticationScheme scheme = authenticationSchemeFactory.getSchema(authentication.getScheme()); if (jwtToken == null) return scheme.createCommand(authentication, () -> null); return scheme.createCommand(authentication, () -> authenticationService.parseJwtToken(jwtToken)); }  }
ServiceAuthenticationServiceImpl implements ServiceAuthenticationService { @Override @CacheEvict(value = CACHE_BY_AUTHENTICATION, condition = "#result != null && #result.isExpired()") @Cacheable(CACHE_BY_AUTHENTICATION) public AuthenticationCommand getAuthenticationCommand(Authentication authentication, String jwtToken) { final AbstractAuthenticationScheme scheme = authenticationSchemeFactory.getSchema(authentication.getScheme()); if (jwtToken == null) return scheme.createCommand(authentication, () -> null); return scheme.createCommand(authentication, () -> authenticationService.parseJwtToken(jwtToken)); }  Authentication getAuthentication(InstanceInfo instanceInfo); @Override @CacheEvict(value = CACHE_BY_AUTHENTICATION, condition = "#result != null && #result.isExpired()") @Cacheable(CACHE_BY_AUTHENTICATION) AuthenticationCommand getAuthenticationCommand(Authentication authentication, String jwtToken); @Override @RetryIfExpired @CacheEvict( value = CACHE_BY_SERVICE_ID, condition = "#result != null && #result.isExpired()", keyGenerator = CacheConfig.COMPOSITE_KEY_GENERATOR ) @Cacheable(value = CACHE_BY_SERVICE_ID, keyGenerator = CacheConfig.COMPOSITE_KEY_GENERATOR) AuthenticationCommand getAuthenticationCommand(String serviceId, String jwtToken); @Override @CacheEvict(value = CACHE_BY_SERVICE_ID, allEntries = true) void evictCacheAllService(); @Override void evictCacheService(String serviceId); }
ServiceAuthenticationServiceImpl implements ServiceAuthenticationService { @Override @CacheEvict(value = CACHE_BY_AUTHENTICATION, condition = "#result != null && #result.isExpired()") @Cacheable(CACHE_BY_AUTHENTICATION) public AuthenticationCommand getAuthenticationCommand(Authentication authentication, String jwtToken) { final AbstractAuthenticationScheme scheme = authenticationSchemeFactory.getSchema(authentication.getScheme()); if (jwtToken == null) return scheme.createCommand(authentication, () -> null); return scheme.createCommand(authentication, () -> authenticationService.parseJwtToken(jwtToken)); }  Authentication getAuthentication(InstanceInfo instanceInfo); @Override @CacheEvict(value = CACHE_BY_AUTHENTICATION, condition = "#result != null && #result.isExpired()") @Cacheable(CACHE_BY_AUTHENTICATION) AuthenticationCommand getAuthenticationCommand(Authentication authentication, String jwtToken); @Override @RetryIfExpired @CacheEvict( value = CACHE_BY_SERVICE_ID, condition = "#result != null && #result.isExpired()", keyGenerator = CacheConfig.COMPOSITE_KEY_GENERATOR ) @Cacheable(value = CACHE_BY_SERVICE_ID, keyGenerator = CacheConfig.COMPOSITE_KEY_GENERATOR) AuthenticationCommand getAuthenticationCommand(String serviceId, String jwtToken); @Override @CacheEvict(value = CACHE_BY_SERVICE_ID, allEntries = true) void evictCacheAllService(); @Override void evictCacheService(String serviceId); static final String AUTHENTICATION_COMMAND_KEY; }