Java Code Examples for org.powermock.api.mockito.PowerMockito#verifyZeroInteractions()

The following examples show how to use org.powermock.api.mockito.PowerMockito#verifyZeroInteractions() . 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 File: SecureLoginTest.java    From pxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testLoginNoKerberosNoServiceUser() throws IOException {
    PowerMockito.mockStatic(PxfUserGroupInformation.class);
    expectedLoginSession = new LoginSession("config");

    UserGroupInformation loginUGI = secureLogin.getLoginUser("server", "config", configuration);

    LoginSession loginSession = SecureLogin.getCache().get("server");
    assertEquals(1, SecureLogin.getCache().size());
    assertEquals(expectedLoginSession, loginSession);
    assertSame(loginUGI, loginSession.getLoginUser());
    assertEquals(System.getProperty("user.name"), loginUGI.getUserName());
    assertNull(loginSession.getSubject());
    assertNull(loginSession.getUser());

    // Make sure that the cached entry is the same after the second call
    assertSame(loginUGI, secureLogin.getLoginUser("server", "config", configuration));

    PowerMockito.verifyZeroInteractions(PxfUserGroupInformation.class);
}
 
Example 2
Source File: SecureLoginTest.java    From pxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testLoginNoKerberosNoServiceUserWhenConfigurationValuesAreProvided() throws IOException {
    PowerMockito.mockStatic(PxfUserGroupInformation.class);
    expectedLoginSession = new LoginSession("config");
    // These values in the configuration should not be added to the LoginSession
    configuration.set("pxf.service.kerberos.principal", "foo");
    configuration.set("pxf.service.kerberos.keytab", "bar");
    configuration.set("hadoop.kerberos.min.seconds.before.relogin", "100");

    UserGroupInformation loginUGI = secureLogin.getLoginUser("server", "config", configuration);

    LoginSession loginSession = SecureLogin.getCache().get("server");
    assertEquals(1, SecureLogin.getCache().size());
    assertEquals(expectedLoginSession, loginSession);
    assertSame(loginUGI, loginSession.getLoginUser());
    assertEquals(System.getProperty("user.name"), loginUGI.getUserName());
    assertNull(loginSession.getSubject());
    assertNull(loginSession.getUser());

    // Make sure that the cached entry is the same after the second call
    assertSame(loginUGI, secureLogin.getLoginUser("server", "config", configuration));

    PowerMockito.verifyZeroInteractions(PxfUserGroupInformation.class);
}
 
Example 3
Source File: SecureLoginTest.java    From pxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testLoginNoKerberosWithServiceUser() throws IOException {
    PowerMockito.mockStatic(PxfUserGroupInformation.class);
    expectedLoginSession = new LoginSession("config", null, null, null, null, 0);
    configuration.set("pxf.service.user.name", "foo");

    UserGroupInformation loginUGI = secureLogin.getLoginUser("server", "config", configuration);

    LoginSession loginSession = SecureLogin.getCache().get("server");
    assertEquals(1, SecureLogin.getCache().size());
    assertEquals(expectedLoginSession, loginSession);
    assertSame(loginUGI, loginSession.getLoginUser());
    assertEquals("foo", loginUGI.getUserName());
    assertNull(loginSession.getSubject());
    assertNull(loginSession.getUser());

    PowerMockito.verifyZeroInteractions(PxfUserGroupInformation.class);
}
 
Example 4
Source File: ResponseHandlingInputStreamTest.java    From weex with Apache License 2.0 5 votes vote down vote up
@Test
public void testReadFully() throws IOException {
  byte[] tempReadingBuffer = new byte[TEST_RESPONSE_BODY.length];
  int result = mResponseHandlingInputStream.read(tempReadingBuffer);
  assertEquals(TEST_RESPONSE_BODY.length, result);
  assertBufferMatchesResponseBody(tempReadingBuffer, TEST_RESPONSE_BODY.length);
  assertBufferMatchesResponseBody(mTestOutputStream.toByteArray(), TEST_RESPONSE_BODY.length);

  PowerMockito.mockStatic(CLog.class);
  PowerMockito.verifyZeroInteractions(CLog.class);
  mResponseHandlingInputStream.close();
  PowerMockito.verifyStatic();
}
 
Example 5
Source File: ResponseHandlingInputStreamTest.java    From weex with Apache License 2.0 5 votes vote down vote up
@Test
public void testSkipMany() throws IOException {
  long numBytesToSkip = TEST_RESPONSE_BODY.length * 2;
  long result = mResponseHandlingInputStream.skip(numBytesToSkip);
  assertEquals((long) TEST_RESPONSE_BODY.length, result);
  assertBufferMatchesResponseBody(mTestOutputStream.toByteArray(), TEST_RESPONSE_BODY.length);
  PowerMockito.verifyZeroInteractions(CLog.class);
  mResponseHandlingInputStream.close();
}
 
Example 6
Source File: ResponseHandlingInputStreamTest.java    From stetho with MIT License 5 votes vote down vote up
@Test
public void testReadFully() throws IOException {
  byte[] tempReadingBuffer = new byte[TEST_RESPONSE_BODY.length];
  int result = mResponseHandlingInputStream.read(tempReadingBuffer);
  assertEquals(TEST_RESPONSE_BODY.length, result);
  assertBufferMatchesResponseBody(tempReadingBuffer, TEST_RESPONSE_BODY.length);
  assertBufferMatchesResponseBody(mTestOutputStream.toByteArray(), TEST_RESPONSE_BODY.length);

  PowerMockito.mockStatic(CLog.class);
  PowerMockito.verifyZeroInteractions(CLog.class);
  mResponseHandlingInputStream.close();
  PowerMockito.verifyStatic();
}
 
Example 7
Source File: ResponseHandlingInputStreamTest.java    From stetho with MIT License 5 votes vote down vote up
@Test
public void testSkipMany() throws IOException {
  long numBytesToSkip = TEST_RESPONSE_BODY.length * 2;
  long result = mResponseHandlingInputStream.skip(numBytesToSkip);
  assertEquals((long) TEST_RESPONSE_BODY.length, result);
  assertBufferMatchesResponseBody(mTestOutputStream.toByteArray(), TEST_RESPONSE_BODY.length);
  PowerMockito.verifyZeroInteractions(CLog.class);
  mResponseHandlingInputStream.close();
}
 
Example 8
Source File: CryptoUtilTest.java    From Auth0.Android with MIT License 4 votes vote down vote up
@RequiresApi(api = Build.VERSION_CODES.P)
@Test
@Config(sdk = 28)
public void shouldCreateNewRSAKeyPairWhenExistingRSAKeyPairCannotBeRebuiltOnAPI28AndUp() throws Exception {
    ReflectionHelpers.setStaticField(Build.VERSION.class, "SDK_INT", 28);
    PrivateKey privateKey = PowerMockito.mock(PrivateKey.class);

    //This is required to trigger the fallback when alias is present but key is not
    PowerMockito.when(keyStore.containsAlias(KEY_ALIAS)).thenReturn(true);
    PowerMockito.when(keyStore.getKey(KEY_ALIAS, null)).thenReturn(privateKey).thenReturn(null);
    PowerMockito.when(keyStore.getCertificate(KEY_ALIAS)).thenReturn(null);
    //This is required to trigger finding the key after generating it
    KeyStore.PrivateKeyEntry expectedEntry = PowerMockito.mock(KeyStore.PrivateKeyEntry.class);
    PowerMockito.when(keyStore.getEntry(KEY_ALIAS, null)).thenReturn(expectedEntry);

    //Tests no instantiation of PrivateKeyEntry
    PowerMockito.verifyZeroInteractions(KeyStore.PrivateKeyEntry.class);

    //Creation assertion
    KeyGenParameterSpec spec = PowerMockito.mock(KeyGenParameterSpec.class);
    KeyGenParameterSpec.Builder builder = newKeyGenParameterSpecBuilder(spec);
    PowerMockito.whenNew(KeyGenParameterSpec.Builder.class).withArguments(KEY_ALIAS, KeyProperties.PURPOSE_DECRYPT | KeyProperties.PURPOSE_ENCRYPT).thenReturn(builder);

    ArgumentCaptor<X500Principal> principalCaptor = ArgumentCaptor.forClass(X500Principal.class);
    ArgumentCaptor<Date> startDateCaptor = ArgumentCaptor.forClass(Date.class);
    ArgumentCaptor<Date> endDateCaptor = ArgumentCaptor.forClass(Date.class);


    final KeyStore.PrivateKeyEntry entry = cryptoUtil.getRSAKeyEntry();

    Mockito.verify(builder).setKeySize(2048);
    Mockito.verify(builder).setCertificateSubject(principalCaptor.capture());
    Mockito.verify(builder).setCertificateSerialNumber(BigInteger.ONE);
    Mockito.verify(builder).setCertificateNotBefore(startDateCaptor.capture());
    Mockito.verify(builder).setCertificateNotAfter(endDateCaptor.capture());
    Mockito.verify(builder).setEncryptionPaddings(KeyProperties.ENCRYPTION_PADDING_RSA_PKCS1);
    Mockito.verify(builder).setBlockModes(KeyProperties.BLOCK_MODE_ECB);
    Mockito.verify(keyPairGenerator).initialize(spec);
    Mockito.verify(keyPairGenerator).generateKeyPair();

    assertThat(principalCaptor.getValue(), is(notNullValue()));
    assertThat(principalCaptor.getValue().getName(), is(CERTIFICATE_PRINCIPAL));

    assertThat(startDateCaptor.getValue(), is(notNullValue()));
    long diffMillis = startDateCaptor.getValue().getTime() - new Date().getTime();
    long days = TimeUnit.MILLISECONDS.toDays(diffMillis);
    assertThat(days, is(0L)); //Date is Today

    assertThat(endDateCaptor.getValue(), is(notNullValue()));
    diffMillis = endDateCaptor.getValue().getTime() - new Date().getTime();
    days = TimeUnit.MILLISECONDS.toDays(diffMillis);
    assertThat(days, is(greaterThan(25 * 365L))); //Date more than 25 Years in days

    assertThat(entry, is(expectedEntry));
}