pvillard31 commented on code in PR #9816: URL: https://github.com/apache/nifi/pull/9816#discussion_r2025269315
########## nifi-extension-bundles/nifi-standard-services/nifi-oauth2-provider-bundle/nifi-oauth2-provider-service/src/test/java/org/apache/nifi/oauth2/JWTBearerOAuth2AccessTokenProviderTest.java: ########## @@ -0,0 +1,300 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.apache.nifi.oauth2; + +import com.nimbusds.jose.JOSEException; +import com.nimbusds.jose.JWSAlgorithm; +import com.nimbusds.jose.JWSHeader; +import com.nimbusds.jwt.JWTClaimsSet; +import org.apache.nifi.components.PropertyDescriptor; +import org.apache.nifi.components.PropertyValue; +import org.apache.nifi.components.ValidationContext; +import org.apache.nifi.components.ValidationResult; +import org.apache.nifi.controller.ConfigurationContext; +import org.apache.nifi.key.service.api.PrivateKeyService; +import org.apache.nifi.logging.ComponentLog; +import org.apache.nifi.web.client.api.HttpResponseEntity; +import org.apache.nifi.web.client.api.WebClientService; +import org.apache.nifi.web.client.provider.api.WebClientServiceProvider; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Answers; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.net.URISyntaxException; +import java.security.PrivateKey; +import java.security.interfaces.ECPrivateKey; +import java.security.interfaces.RSAPrivateKey; +import java.time.Duration; +import java.util.Collection; +import java.util.Map; +import java.util.Set; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +class JWTBearerOAuth2AccessTokenProviderTest { + + @Mock(answer = Answers.RETURNS_DEEP_STUBS) + private ConfigurationContext mockContext; + + @Mock(answer = Answers.RETURNS_DEEP_STUBS) + private ValidationContext mockValidationContext; + + @Mock + private PrivateKeyService mockKeyService; + + @Mock + private WebClientServiceProvider mockWebClientServiceProvider; + + @Mock + private WebClientService mockWebClientService; + + @Mock + private HttpResponseEntity mockResponseEntity; + + @Mock + private ComponentLog mockLogger; + + private JWTBearerOAuth2AccessTokenProviderForTests provider; + + @BeforeEach + void setUp() { + // Initialize mocks + MockitoAnnotations.openMocks(this); + provider = new JWTBearerOAuth2AccessTokenProviderForTests(); + + // Mock PropertyValue for WEB_CLIENT_SERVICE + PropertyValue webClientServicePropertyValue = mock(PropertyValue.class); + when(mockContext.getProperty(JWTBearerOAuth2AccessTokenProvider.WEB_CLIENT_SERVICE)) + .thenReturn(webClientServicePropertyValue); + + // Mock WebClientService + when(webClientServicePropertyValue.asControllerService(WebClientServiceProvider.class)) + .thenReturn(mockWebClientServiceProvider); + + when(mockWebClientServiceProvider.getWebClientService()).thenReturn(mockWebClientService); + } + + @Test + void testClaimsAreSetProperly() throws Exception { + // Setup context + setContext(); + + setPrivateKeyMock(RSAPrivateKey.class); + when(mockContext.getProperty(JWTBearerOAuth2AccessTokenProvider.SIGNING_ALGORITHM).getValue()).thenReturn(JWSAlgorithm.RS256.getName()); Review Comment: I felt like using the TestRunner would make things more complicated in the end as I would need to simulate a processor that references the controller service to actually test the access token retrieval. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
