Java Code Examples for org.mockito.internal.util.reflection.FieldSetter#setField()

The following examples show how to use org.mockito.internal.util.reflection.FieldSetter#setField() . These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: cloudbreak   File: SdxControllerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
void getTest() throws NoSuchFieldException {
    SdxCluster sdxCluster = getValidSdxCluster();
    when(sdxService.getSdxByNameInAccount(anyString(), anyString())).thenReturn(sdxCluster);

    SdxStatusEntity sdxStatusEntity = new SdxStatusEntity();
    sdxStatusEntity.setStatus(DatalakeStatusEnum.REQUESTED);
    sdxStatusEntity.setStatusReason("statusreason");
    sdxStatusEntity.setCreated(1L);
    when(sdxStatusService.getActualStatusForSdx(sdxCluster)).thenReturn(sdxStatusEntity);
    FieldSetter.setField(sdxClusterConverter, SdxClusterConverter.class.getDeclaredField("sdxStatusService"), sdxStatusService);

    SdxClusterResponse sdxClusterResponse = ThreadBasedUserCrnProvider.doAs(USER_CRN, () -> sdxController.get("test-sdx-cluster"));
    assertEquals("test-sdx-cluster", sdxClusterResponse.getName());
    assertEquals("test-env", sdxClusterResponse.getEnvironmentName());
    assertEquals("crn:sdxcluster", sdxClusterResponse.getCrn());
    assertEquals(SdxClusterStatusResponse.REQUESTED, sdxClusterResponse.getStatus());
    assertEquals("statusreason", sdxClusterResponse.getStatusReason());
}
 
Example 2
Source Project: XS2A-Sandbox   File: AISControllerTest.java    License: Apache License 2.0 6 votes vote down vote up
@Test
void revokeConsent() throws NoSuchFieldException {
    // Given
    when(responseUtils.consentCookie(any())).thenReturn(COOKIE);
    when(redirectConsentService.identifyConsent(anyString(), anyString(), anyBoolean(), anyString(), any())).thenReturn(getConsentWorkflow(FINALISED, ConsentStatus.RECEIVED));

    FieldSetter.setField(controller, controller.getClass().getDeclaredField("middlewareAuth"), new ObaMiddlewareAuthentication(null, new BearerTokenTO(TOKEN, null, 999, null, getAccessTokenTO())));

    when(cmsPsuAisClient.updateAuthorisationStatus(anyString(), anyString(), anyString(), anyString(), ArgumentMatchers.nullable(String.class), ArgumentMatchers.nullable(String.class), ArgumentMatchers.nullable(String.class), anyString(), any())).thenReturn(ResponseEntity.ok(null));

    // When
    ResponseEntity<ConsentAuthorizeResponse> result = controller.revokeConsent(ENCRYPTED_ID, AUTH_ID, COOKIE);

    // Then
    assertEquals(ResponseEntity.ok(getConsentAuthorizeResponse(false, false, true, ScaStatusTO.EXEMPTED)), result);
}
 
Example 3
@Test
void fromRequest() throws NoSuchFieldException {
    // Given
    FieldSetter.setField(defaultConsentReferencePolicy, defaultConsentReferencePolicy.getClass().getDeclaredField("hmacSecret"), "6VFX8YFQG5DLFKZIMNLGH9P406XR1SY4");

    ConsentReference reference = defaultConsentReferencePolicy.fromURL(REDIRECT_ID, CONSENT_TYPE_AIS, ENCRYPTED_CONSENT_ID);

    // When
    ConsentReference consentReference = defaultConsentReferencePolicy.fromRequest(ENCRYPTED_CONSENT_ID, AUTHORIZATION_ID, reference.getCookieString(), false);

    // Then
    assertNotNull(consentReference);
    assertEquals(AUTHORIZATION_ID, consentReference.getAuthorizationId());
    assertEquals(REDIRECT_ID, consentReference.getRedirectId());
    assertEquals(CONSENT_TYPE_AIS, consentReference.getConsentType());
    assertEquals(ENCRYPTED_CONSENT_ID, consentReference.getEncryptedConsentId());
    assertNotNull(consentReference.getCookieString());
}
 
Example 4
@Test
void handlePaymentAuthorizeException() throws NoSuchFieldException {
    // Given
    FieldSetter.setField(service, service.getClass().getDeclaredField("objectMapper"), STATIC_MAPPER);
    PaymentAuthorizeResponse authorizeResponse = new PaymentAuthorizeResponse(new PaymentTO());
    PsuMessage message = new PsuMessage();
    message.setCode("400");
    message.setText("Msg");
    authorizeResponse.setPsuMessages(List.of(message));

    // When
    ResponseEntity<Map> result = service.handlePaymentAuthorizeException(new PaymentAuthorizeException(ResponseEntity.status(HttpStatus.NOT_FOUND).body(authorizeResponse)));

    // Then
    compareBodies(result, ResponseEntity.status(HttpStatus.BAD_REQUEST).body(getExpected(400, "Msg")));
}
 
Example 5
@Before
public void setUp() throws NoSuchFieldException {
    given(tcString.getVendorListVersion()).willReturn(10);
    given(purposeStrategyOne.getPurposeId()).willReturn(1);
    given(purposeStrategyTwo.getPurposeId()).willReturn(2);
    given(purposeStrategyFour.getPurposeId()).willReturn(4);
    given(purposeStrategySeven.getPurposeId()).willReturn(7);
    purposeStrategies = asList(purposeStrategyOne, purposeStrategyTwo, purposeStrategyFour, purposeStrategySeven);

    given(specialFeaturesStrategyOne.getSpecialFeatureId()).willReturn(1);
    specialFeaturesStrategies = singletonList(specialFeaturesStrategyOne);

    given(vendorListService.forVersion(anyInt())).willReturn(Future.succeededFuture(emptyMap()));

    initPurposes();
    initSpecialFeatures();
    initGdpr();
    target = new Tcf2Service(gdprConfig, vendorListService, bidderCatalog);

    FieldSetter.setField(target,
            target.getClass().getDeclaredField("supportedPurposeStrategies"), purposeStrategies);
    FieldSetter.setField(target,
            target.getClass().getDeclaredField("supportedSpecialFeatureStrategies"), specialFeaturesStrategies);
}
 
Example 6
@Test
void handleFeignException() throws NoSuchMethodException, JsonProcessingException, NoSuchFieldException {
    // Given
    FieldSetter.setField(service, service.getClass().getDeclaredField("objectMapper"), STATIC_MAPPER);

    // When
    ResponseEntity<Map> result = service.handleFeignException(FeignException.errorStatus("method", getResponse()), new HandlerMethod(service, "toString", null));
    ResponseEntity<Map<String, String>> expected = ResponseEntity.status(HttpStatus.UNAUTHORIZED).body(getExpected(401, "status 401 reading method"));

    // Then
    compareBodies(result, expected);
}
 
Example 7
Source Project: XS2A-Sandbox   File: ParseServiceTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void getDefaultData() throws NoSuchFieldException {
    // Given
    FieldSetter.setField(parseService, parseService.getClass().getDeclaredField("resourceLoader"), LOCAL_LOADER);

    // When
    Optional<DataPayload> data = parseService.getDefaultData();

    // Then
    assertTrue(data.isPresent());
}
 
Example 8
Source Project: XS2A-Sandbox   File: AISControllerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void startConsentAuth() throws NoSuchFieldException {
    // Given
    FieldSetter.setField(controller, controller.getClass().getDeclaredField("middlewareAuth"), new ObaMiddlewareAuthentication(null, new BearerTokenTO(TOKEN, null, 999, null, getAccessTokenTO())));

    when(responseUtils.consentCookie(any())).thenReturn(COOKIE);
    when(redirectConsentService.identifyConsent(anyString(), anyString(), anyBoolean(), anyString(), any())).thenReturn(getConsentWorkflow(PSUIDENTIFIED, ConsentStatus.RECEIVED));
    when(accountRestClient.getListOfAccounts()).thenReturn(ResponseEntity.ok(new ArrayList<>()));

    // When
    ResponseEntity<ConsentAuthorizeResponse> result = controller.startConsentAuth(ENCRYPTED_ID, AUTH_ID, COOKIE, getAisConsentTO(false));

    // Then
    assertEquals(ResponseEntity.ok(getConsentAuthorizeResponse(true, true, false, PSUIDENTIFIED)), result);
}
 
Example 9
@Test
void fromRequest_wrongEncryptedConsentId() throws NoSuchFieldException {
    // Given
    FieldSetter.setField(defaultConsentReferencePolicy, defaultConsentReferencePolicy.getClass().getDeclaredField("hmacSecret"), "6VFX8YFQG5DLFKZIMNLGH9P406XR1SY4");
    ConsentReference reference = defaultConsentReferencePolicy.fromURL(REDIRECT_ID, CONSENT_TYPE_AIS, ENCRYPTED_CONSENT_ID);

    // Then
    assertThrows(AuthorizationException.class, () -> defaultConsentReferencePolicy.fromRequest(null, AUTHORIZATION_ID, reference.getCookieString(), false));
}
 
Example 10
Source Project: XS2A-Sandbox   File: PISControllerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void failPaymentAuthorisation() throws NoSuchFieldException {
    // Given
    FieldSetter.setField(controller, controller.getClass().getDeclaredField("middlewareAuth"), new ObaMiddlewareAuthentication(null, new BearerTokenTO(TOKEN, null, 999, null, getAccessTokenTO())));
    when(responseUtils.consentCookie(any())).thenReturn(COOKIE);
    when(paymentService.identifyPayment(anyString(), anyString(), anyBoolean(), anyString(), anyString(), any())).thenReturn(getPaymentWorkflow(FAILED));

    // When
    ResponseEntity<PaymentAuthorizeResponse> result = controller.failPaymentAuthorisation(ENCRYPTED_ID, AUTH_ID, COOKIE);

    // Then
    assertEquals(ResponseEntity.ok(getPaymentAuthorizeResponse(true, true, FAILED)), result);
}
 
Example 11
@Test
@SneakyThrows
public void assertExecuteReturnErrorResponsePacket() {
    PostgreSQLComQueryExecutor postgreSQLComQueryExecutor = new PostgreSQLComQueryExecutor(mock(PostgreSQLComQueryPacket.class), null);
    FieldSetter.setField(postgreSQLComQueryExecutor, PostgreSQLComQueryExecutor.class.getDeclaredField("textProtocolBackendHandler"), textProtocolBackendHandler);
    ErrorResponse errorResponse = new ErrorResponse(new PSQLException(mock(ServerErrorMessage.class)));
    when(textProtocolBackendHandler.execute()).thenReturn(errorResponse);
    Assert.assertThat(postgreSQLComQueryExecutor.execute().iterator().next(), Matchers.instanceOf(PostgreSQLErrorResponsePacket.class));
    Assert.assertThat(postgreSQLComQueryExecutor.isErrorResponse(), Matchers.is(true));
}
 
Example 12
Source Project: XS2A-Sandbox   File: PISControllerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void authrizedPayment() throws NoSuchFieldException {
    // Given
    FieldSetter.setField(controller, controller.getClass().getDeclaredField("middlewareAuth"), new ObaMiddlewareAuthentication(null, new BearerTokenTO(TOKEN, null, 999, null, getAccessTokenTO())));
    when(responseUtils.consentCookie(any())).thenReturn(COOKIE);
    when(paymentService.identifyPayment(anyString(), anyString(), anyBoolean(), anyString(), anyString(), any())).thenReturn(getPaymentWorkflow(PSUIDENTIFIED));
    when(paymentService.authorizePayment(any(), anyString(), anyString())).thenReturn(getPaymentWorkflow(FINALISED));

    // When
    ResponseEntity<PaymentAuthorizeResponse> result = controller.authrizedPayment(ENCRYPTED_ID, AUTH_ID, METHOD_ID, COOKIE);

    // Then
    assertEquals(ResponseEntity.ok(getPaymentAuthorizeResponse(true, true, FINALISED)), result);
}
 
Example 13
Source Project: XS2A-Sandbox   File: AISControllerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void authrizedConsent_error() throws NoSuchFieldException {
    // Given
    FieldSetter.setField(controller, controller.getClass().getDeclaredField("middlewareAuth"), new ObaMiddlewareAuthentication(null, new BearerTokenTO(TOKEN, null, 999, null, getAccessTokenTO())));
    when(responseUtils.consentCookie(any())).thenReturn(COOKIE);
    when(redirectConsentService.identifyConsent(anyString(), anyString(), anyBoolean(), anyString(), any())).thenThrow(new ConsentAuthorizeException(ResponseEntity.badRequest().build()));

    // When
    ResponseEntity<ConsentAuthorizeResponse> result = controller.authrizedConsent(ENCRYPTED_ID, AUTH_ID, COOKIE, CODE);

    // Then
    assertEquals(ResponseEntity.badRequest().build(), result);
}
 
Example 14
@Test
void fromURL() throws NoSuchFieldException {
    // Given
    FieldSetter.setField(defaultConsentReferencePolicy, defaultConsentReferencePolicy.getClass().getDeclaredField("hmacSecret"), "6VFX8YFQG5DLFKZIMNLGH9P406XR1SY4");

    // When
    ConsentReference reference = defaultConsentReferencePolicy.fromURL(REDIRECT_ID, CONSENT_TYPE_AIS, ENCRYPTED_CONSENT_ID);

    // Then
    assertNotNull(reference);
    assertEquals(REDIRECT_ID, reference.getRedirectId());
    assertEquals(CONSENT_TYPE_AIS, reference.getConsentType());
    assertEquals(ENCRYPTED_CONSENT_ID, reference.getEncryptedConsentId());
    assertNotNull(reference.getCookieString());
}
 
Example 15
Source Project: XS2A-Sandbox   File: AISControllerTest.java    License: Apache License 2.0 5 votes vote down vote up
@Test
void getListOfAccounts() throws NoSuchFieldException {
    // Given
    FieldSetter.setField(controller, controller.getClass().getDeclaredField("middlewareAuth"), new ObaMiddlewareAuthentication(null, new BearerTokenTO(TOKEN, null, 999, null, getAccessTokenTO())));
    when(accountRestClient.getListOfAccounts()).thenReturn(ResponseEntity.ok(getAccounts()));

    // When
    ResponseEntity<List<AccountDetailsTO>> result = controller.getListOfAccounts(COOKIE);

    // Then
    assertEquals(ResponseEntity.ok(getAccounts()), result);
}
 
Example 16
@Test
void fromRequest_missingClaim() throws NoSuchFieldException {
    // Given
    FieldSetter.setField(defaultConsentReferencePolicy, defaultConsentReferencePolicy.getClass().getDeclaredField("hmacSecret"), "6VFX8YFQG5DLFKZIMNLGH9P406XR1SY4");
    ConsentReference reference = defaultConsentReferencePolicy.fromURL(REDIRECT_ID, CONSENT_TYPE_AIS, ENCRYPTED_CONSENT_ID);

    // Then
    assertThrows(AuthorizationException.class, () -> defaultConsentReferencePolicy.fromRequest(ENCRYPTED_CONSENT_ID, AUTHORIZATION_ID, reference.getCookieString(), true));
}
 
Example 17
@Test
@SneakyThrows
public void assertIsQuery() {
    BackendConnection backendConnection = mock(BackendConnection.class);
    SchemaContext schema = mock(SchemaContext.class);
    when(backendConnection.getSchema()).thenReturn(schema);
    MySQLComStmtExecuteExecutor mysqlComStmtExecuteExecutor = new MySQLComStmtExecuteExecutor(mock(MySQLComStmtExecutePacket.class), backendConnection);
    FieldSetter.setField(mysqlComStmtExecuteExecutor, MySQLComStmtExecuteExecutor.class.getDeclaredField("databaseCommunicationEngine"), databaseCommunicationEngine);
    when(databaseCommunicationEngine.execute()).thenReturn(new QueryResponse(Collections.singletonList(mock(QueryHeader.class))));
    mysqlComStmtExecuteExecutor.execute();
    assertThat(mysqlComStmtExecuteExecutor.isQuery(), Matchers.is(true));
}
 
Example 18
@Test
void performLoginForConsent() throws NoSuchFieldException {
    // Given
    FieldSetter.setField(service, service.getClass().getDeclaredField("request"), new MockHttpServletRequest());
    when(userMgmtRestClient.authoriseForConsent(anyString(), anyString(), anyString(), anyString(), any())).thenReturn(getLoginResponse());

    // When
    ResponseEntity<SCALoginResponseTO> result = service.performLoginForConsent(LOGIN, PIN, CONSENT_ID, AUTH_ID, OpTypeTO.CONSENT);

    // Then
    assertEquals(getLoginResponse(), result);
}
 
Example 19
@SneakyThrows({NoSuchFieldException.class, SecurityException.class})
@Before
public void setUp() {
    Properties props = new Properties();
    props.setProperty(ApolloPropertyKey.PORTAL_URL.getKey(), PORTAL_URL);
    props.setProperty(ApolloPropertyKey.TOKEN.getKey(), TOKEN);
    apolloOpenApiWrapper = new ApolloOpenApiWrapper(new CenterConfiguration("apollo", new Properties()), new ApolloProperties(props));
    FieldSetter.setField(apolloOpenApiWrapper, ApolloOpenApiWrapper.class.getDeclaredField("client"), client);
    FieldSetter.setField(apolloOpenApiWrapper, ApolloOpenApiWrapper.class.getDeclaredField("namespace"), NAME_SPACE);
    FieldSetter.setField(apolloOpenApiWrapper, ApolloOpenApiWrapper.class.getDeclaredField("appId"), ApolloPropertyKey.APP_ID.getDefaultValue());
    FieldSetter.setField(apolloOpenApiWrapper, ApolloOpenApiWrapper.class.getDeclaredField("env"), ApolloPropertyKey.ENV.getDefaultValue());
    FieldSetter.setField(apolloOpenApiWrapper, ApolloOpenApiWrapper.class.getDeclaredField("clusterName"), ApolloPropertyKey.CLUSTER_NAME.getDefaultValue());
    FieldSetter.setField(apolloOpenApiWrapper, ApolloOpenApiWrapper.class.getDeclaredField("administrator"), ApolloPropertyKey.ADMINISTRATOR.getDefaultValue());
}
 
Example 20
@Test
public void permissionsForShouldNotAllowAllWhenP1TIsFalseAndP1TIIsAccessAllowed() throws NoSuchFieldException {
    // given
    given(bidderCatalog.nameByVendorId(any())).willReturn("rubicon");

    given(tcString.getPurposeOneTreatment()).willReturn(false);

    target = new Tcf2Service(
            GdprConfig.builder()
                    .purposes(purposes)
                    .purposeOneTreatmentInterpretation(PurposeOneTreatmentInterpretation.accessAllowed)
                    .build(),
            vendorListService,
            bidderCatalog);
    FieldSetter.setField(target,
            target.getClass().getDeclaredField("supportedPurposeStrategies"), purposeStrategies);
    FieldSetter.setField(target,
            target.getClass().getDeclaredField("supportedSpecialFeatureStrategies"), specialFeaturesStrategies);

    // when
    target.permissionsFor(singleton(1), tcString);

    // then
    verify(purposeStrategyOne, never()).allow(any());
    verify(purposeStrategyOne).processTypePurposeStrategy(any(), any(), anyCollection());
    verify(purposeStrategyTwo).processTypePurposeStrategy(any(), any(), anyCollection());
    verify(purposeStrategySeven).processTypePurposeStrategy(any(), any(), anyCollection());
    verify(purposeStrategyFour).processTypePurposeStrategy(any(), any(), anyCollection());

    verify(specialFeaturesStrategyOne).processSpecialFeaturesStrategy(any(), any(), anyCollection());
}