Метод не издевается, используя powermockito и mockito - PullRequest
0 голосов
/ 20 марта 2019

Метод для тестирования:

public Map decryptRecord (String Test) {

    AmazonDynamoDB client = AmazonDynamoDBClientBuilder.standard().build();
    DynamoDB dynamoDB = new DynamoDB(client);

    Table table = dynamoDB.getTable("SampleTable");
    System.out.println(table);

    final AWSKMS kms = AWSKMSClientBuilder.standard().withRegion(new DefaultAwsRegionProviderChain().getRegion()).build();
    final DirectKmsMaterialProvider cmp = new DirectKmsMaterialProvider(kms, System.getenv("cmkArn"));
    final DynamoDBEncryptor encryptor = DynamoDBEncryptor.getInstance(cmp);
    final EncryptionContext encryptionContext = new EncryptionContext.Builder()
            .withTableName("SampleTable")
            .withHashKeyName("Test")
            .build();

    PrimaryKey TestKey = new PrimaryKey("Test", Test);
    Item item = table.getItem(TestKey);
    System.out.println("Item"+item);

    final Map<String, AttributeValue> record = new HashMap<>();
    record.put("Test", new AttributeValue().withS(item.getString("Test")));
    record.put("status", new AttributeValue().withS(item.getString("status")));
    record.put("connectionType", new AttributeValue().withS(item.getString("connectionType")));
    record.put("type", new AttributeValue().withS(item.getString("type")));
    record.put("*amzn-ddb-map-desc*", new AttributeValue().withB(item.getByteBuffer("*amzn-ddb-map-desc*")));
    record.put("*amzn-ddb-map-sig*", new AttributeValue().withB(item.getByteBuffer("*amzn-ddb-map-sig*")));
    record.put("connectionConfiguration", new AttributeValue().withB(item.getByteBuffer("connectionConfiguration")));

    final EnumSet<EncryptionFlags> signOnly = EnumSet.of(EncryptionFlags.SIGN);
    final EnumSet<EncryptionFlags> encryptAndSign = EnumSet.of(EncryptionFlags.ENCRYPT, EncryptionFlags.SIGN);
    final Map<String, Set<EncryptionFlags>> actions = new HashMap<>();

    for (String attributeName : record.keySet()) {
        switch (attributeName) {
            case "Test": // fall through
                //case sortKeyName: // fall through
                actions.put(attributeName, signOnly);
                break;
            case "connectionConfiguration":
                actions.put(attributeName, encryptAndSign);
                break;
            default:
                // We want to leave everything as is
                break;
        }
    }
    Map<String, AttributeValue> decrypted_record=null;

    try {
        System.out.println("Record before decryption"+ record);
        decrypted_record = encryptor.decryptRecord(record, actions, encryptionContext);
        System.out.println("Decrypted Record: " + decrypted_record);

    } catch (GeneralSecurityException e) {
        e.printStackTrace();
    }
    return decrypted_record;
}

и тестовый случай:

@ RunWith (PowerMockRunner.class) @PrepareForTest ({AmazonDynamoDBClientBuilder.class, AWSKMSClientBuilder.class, DynamoDBEncryptor.class}) открытый класс DBUtilsImplTest {

@Test
public void testDecryptMethod() throws Exception {

    PowerMockito.mockStatic(AmazonDynamoDBClientBuilder.class);
    AmazonDynamoDBClientBuilder mockamazonDynamoDBClientBuilder =mock(AmazonDynamoDBClientBuilder.class);
    AmazonDynamoDB mockclient=mock(AmazonDynamoDB.class);
    PowerMockito.when(AmazonDynamoDBClientBuilder.standard()).thenReturn(mockamazonDynamoDBClientBuilder);
    PowerMockito.when(mockamazonDynamoDBClientBuilder.build()).thenReturn(mockclient);
    DynamoDB mockdynamoDB=mock(DynamoDB.class);
    PowerMockito.whenNew(DynamoDB.class).withArguments(mockclient).thenReturn(mockdynamoDB);

    Table mocktable=mock(Table.class);

    PowerMockito.when(mockdynamoDB.getTable("SampleTable")).thenReturn(mocktable);
    PowerMockito.mockStatic(AWSKMSClientBuilder.class);

    AWSKMSClientBuilder mockawskmsclientbuilder=mock(AWSKMSClientBuilder.class);
    AWSKMS mockkms= mock(AWSKMS.class);

    String region="us-west-1";
    DefaultAwsRegionProviderChain mockchainprovider=mock(DefaultAwsRegionProviderChain.class);
    PowerMockito.when(mockchainprovider.getRegion()).thenReturn(region);

    PowerMockito.when(AWSKMSClientBuilder.standard()).thenReturn(mockawskmsclientbuilder);
    PowerMockito.when(mockawskmsclientbuilder.withRegion(mockchainprovider.getRegion())).thenReturn(mockawskmsclientbuilder);

    PowerMockito.when(mockawskmsclientbuilder.build()).thenReturn(mockkms);
    DirectKmsMaterialProvider mockcmp=mock(DirectKmsMaterialProvider.class);
    PowerMockito.whenNew(DirectKmsMaterialProvider.class).withArguments(mockkms, System.getenv("cmkArn")).thenReturn(mockcmp);

    PowerMockito.mockStatic(DynamoDBEncryptor.class);

    DynamoDBEncryptor mockencrypter=mock(DynamoDBEncryptor.class);

    PowerMockito.when(DynamoDBEncryptor.getInstance(mockcmp)).thenReturn(mockencrypter);

    PrimaryKey tenantIdKey = new PrimaryKey("Test", 12);
    //PowerMockito.when(mocktable.getItem(tenantIdKey)).thenReturn(new Item());
    Mockito.when(mocktable.getItem(tenantIdKey)).thenReturn(new Item());

    final EncryptionContext encryptionContext = new EncryptionContext.Builder()
            .withTableName("SampleTable")
            .withHashKeyName("Test")
            .build();
    final Map<String, AttributeValue> record = new HashMap<>();
    final Map<String, Set<EncryptionFlags>> actions = new HashMap<>();


    PowerMockito.when(mockencrypter.decryptRecord(record,actions,encryptionContext)).thenReturn(record);
    Map<String, AttributeValue> val=new DynamoDBUtilsImpl().decryptRecord("Test");
    Assert.assertNotNull(val);
}

Я получаю сообщение об ошибке по адресу:

1012 * java..services.inner.GetItemImpl.getItemOutcome (GetItemImpl.java:46) в com.amazonaws.services.dynamodbv2.document.internal.GetItemImpl.getItem (GetItemImpl.java:88) в com.amazonaws.services.dynamodbv2..getItem (Table.java:597) в com.nice.tfs.utils.DynamoDBUtilsImpl.decryptRecord (DynamoDBUtilsImpl.java:44) в com.example.demo.DBUtilsImplTest.testDecryptMethod (DBUtilsImplTest.java:92) в sun.reflect.NativeMethodAccessorImpl.invoke0 (нативный метод) в sun.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62) в sun.reflect.Dejava.lang.reflect.Method.invoke (Method.java:498) в org.junit.internal.runners.TestMethod.invoke (TestMethod.java:68)

Справка по причине и решению.

...