Skip to content

Commit

Permalink
Initial test framework (#14660)
Browse files Browse the repository at this point in the history
  • Loading branch information
azabbasi authored Aug 31, 2020
1 parent 7b1e380 commit 4a74849
Show file tree
Hide file tree
Showing 10 changed files with 382 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ temp/
# Spring
**/*packageOutputDirectory*

# Sensitive files
*.json.env

#javadoc overview files generated from README.md
readme_overview.html
**/javadocTemp/**
Expand Down
5 changes: 5 additions & 0 deletions sdk/digitaltwins/Print-MyEnvData.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
param(
[Parameter()]
[string] $fileName = "test-resources.json.env"
)
([System.Text.Encoding]::UTF8).GetString([Security.Cryptography.ProtectedData]::Unprotect([IO.File]::ReadAllBytes((Resolve-path $fileName)), $null, [Security.Cryptography.DataProtectionScope]::CurrentUser))
49 changes: 43 additions & 6 deletions sdk/digitaltwins/azure-digitaltwins-core/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -15,28 +15,65 @@
<artifactId>azure-digitaltwins-core</artifactId>
<version>1.0.0-beta.1</version> <!-- {x-version-update;com.azure:azure-digitaltwins-core;current} -->

<properties>
<!-- These are the code coverage settings that should eventually be removed to inherit them from the parent pom file. for the time being we have a very low code coverage so we are dropping it to a very low setting -->
<jacoco.min.linecoverage>0.01</jacoco.min.linecoverage>
<jacoco.min.branchcoverage>0.01</jacoco.min.branchcoverage>
</properties>

<dependencies>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core</artifactId>
<version>1.7.0</version> <!-- {x-version-update;com.azure:azure-core;dependency} -->
</dependency>
<dependency>
<!--Azure identity library has useful classes for authenticating against ADT, but isn't required in the client library. It is used in samples/tests-->
<groupId>com.azure</groupId>
<artifactId>azure-core-http-netty</artifactId>
<version>1.5.4</version> <!-- {x-version-update;com.azure:azure-core-http-netty;dependency} -->
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.2</version> <!-- {x-version-update;com.fasterxml.jackson.core:jackson-annotations;external_dependency} -->
</dependency>

<!-- Test dependencies -->
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-test</artifactId>
<version>1.4.0</version> <!-- {x-version-update;com.azure:azure-core-test;dependency} -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-identity</artifactId>
<version>1.1.0</version> <!-- {x-version-update;com.azure:azure-identity;dependency} -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-core-http-netty</artifactId>
<version>1.5.4</version> <!-- {x-version-update;com.azure:azure-core-http-netty;dependency} -->
<artifactId>azure-core-http-okhttp</artifactId>
<version>1.2.5</version> <!-- {x-version-update;com.azure:azure-core-http-okhttp;dependency} -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.11.2</version> <!-- {x-version-update;com.fasterxml.jackson.core:jackson-annotations;external_dependency} -->
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-api</artifactId>
<version>5.6.2</version> <!-- {x-version-update;org.junit.jupiter:junit-jupiter-api;external_dependency} -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>5.6.2</version> <!-- {x-version-update;org.junit.jupiter:junit-jupiter-engine;external_dependency} -->
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-params</artifactId>
<version>5.6.2</version> <!-- {x-version-update;org.junit.jupiter:junit-jupiter-params;external_dependency} -->
<scope>test</scope>
</dependency>
</dependencies>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
package com.azure.digitaltwins.core;

import com.azure.core.credential.AccessToken;
import com.azure.core.credential.TokenCredential;
import com.azure.core.credential.TokenRequestContext;
import com.azure.core.http.policy.HttpLogDetailLevel;
import com.azure.core.http.policy.HttpLogOptions;
import com.azure.core.http.policy.HttpPipelinePolicy;
import com.azure.core.test.TestBase;
import com.azure.core.test.TestMode;
import com.azure.core.util.Configuration;
import com.azure.core.util.logging.ClientLogger;
import com.azure.identity.ClientSecretCredentialBuilder;
import reactor.core.publisher.Mono;

import java.util.Locale;

public class DigitalTwinsTestBase extends TestBase
{
protected static final String TENANT_ID = Configuration.getGlobalConfiguration()
.get("TENANT_ID", "tenantId");

protected static final String CLIENT_SECRET = Configuration.getGlobalConfiguration()
.get("CLIENT_SECRET", "clientSecret");

protected static final String CLIENT_ID = Configuration.getGlobalConfiguration()
.get("CLIENT_ID", "clientId");

protected static final String DIGITALTWINS_URL = Configuration.getGlobalConfiguration()
.get("DIGITALTWINS_URL", "https://playback.api.wus2.digitaltwins.azure.net");

protected DigitalTwinsClientBuilder getDigitalTwinsClientBuilder() {
DigitalTwinsClientBuilder builder = new DigitalTwinsClientBuilder()
.endpoint(DIGITALTWINS_URL);

if (interceptorManager.isPlaybackMode()){
builder.httpClient(interceptorManager.getPlaybackClient());
// Use fake credentials for playback mode.
builder.tokenCredential(new FakeCredentials());
return builder;
}

// TODO: investigate whether or not we need to add a retry policy.

// If it is record mode, we add record mode policies to the builder.
// There is no isRecordMode method on interceptorManger.
if (!interceptorManager.isLiveMode()){
builder.addPolicy(interceptorManager.getRecordPolicy());
}

// Only get valid live token when running live tests.
builder.tokenCredential(new ClientSecretCredentialBuilder()
.tenantId(TENANT_ID)
.clientId(CLIENT_ID)
.clientSecret(CLIENT_SECRET)
.build());

return builder;
}

protected DigitalTwinsClientBuilder getDigitalTwinsClientBuilder(HttpPipelinePolicy... policies) {
DigitalTwinsClientBuilder builder = new DigitalTwinsClientBuilder()
.endpoint(DIGITALTWINS_URL);

if (interceptorManager.isPlaybackMode()){
builder.httpClient(interceptorManager.getPlaybackClient());
// Use fake credentials for playback mode.
builder.tokenCredential(new FakeCredentials());
addPolicies(builder, policies);
return builder;
}

addPolicies(builder, policies);

// TODO: investigate whether or not we need to add a retry policy.

// If it is record mode, we add record mode policies to the builder.
// There is no isRecordMode method on interceptorManger.
if (!interceptorManager.isLiveMode()) {
builder.addPolicy(interceptorManager.getRecordPolicy());
}

// Only get valid live token when running live tests.
builder.tokenCredential(new ClientSecretCredentialBuilder()
.tenantId(TENANT_ID)
.clientId(CLIENT_ID)
.clientSecret(CLIENT_SECRET)
.build());

return builder;
}

private static void addPolicies(DigitalTwinsClientBuilder builder, HttpPipelinePolicy... policies) {
if (policies == null) {
return;
}

for (HttpPipelinePolicy policy : policies) {
builder.addPolicy(policy);
}
}

static class FakeCredentials implements TokenCredential
{
@Override
public Mono<AccessToken> getToken(TokenRequestContext tokenRequestContext) {
return Mono.empty();
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.azure.digitaltwins.core;

import com.azure.core.http.rest.PagedIterable;
import com.azure.digitaltwins.core.models.ModelData;
import org.junit.jupiter.api.Test;

public class SampleTest extends DigitalTwinsTestBase {

private static DigitalTwinsClient client;

@Override
protected void beforeTest(){
super.beforeTest();
client = setupClient();
}

@Override
protected void afterTest()
{
super.afterTest();
}

private DigitalTwinsClient setupClient(){
return getDigitalTwinsClientBuilder()
.buildClient();
}

@Test
public void ListTest(){
PagedIterable<ModelData> models = client.listModels();

// Process using the Stream interface by iterating over each page
models
// You can also subscribe to pages by specifying the preferred page size or the associated continuation token to start the processing from.
.streamByPage()
.forEach(page -> {
System.out.println("Response headers status code is " + page.getStatusCode());
page.getValue().forEach(item -> System.out.println("Model retrieved: " + item.getId()));
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Prerequisites

## Install

### Install the latest Powershell 7

- Make sure you run the script using the latest stable version of [powershell 7](https://github.com/PowerShell/PowerShell/releases)

### Install the latest Azure CLI package

- If already installed, check latest version:
- Run `az --version` to make sure `azure-cli` is at least **version 2.3.1**
- If it isn't, update it
- Use this link to install [Azure CLI](https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest])

## Delete

To delete the digital twins instance, you need to first delete the endpoint added by the script (the service doesn't yet support cascading delete).

1. To do this, run the command `az dt endpoint delete -n <dt name> -g <rg name> --en someEventHubEndpoint`.
1. If you have other endpoints that have been added outside this script, you can discover them with the command `az dt endpoint list -n <dt name> -g <rg name>`.
1. Then delete them with the same command in step 1.

## Maintenance

In order to maintain the functionality of the Setup.ps1 file, make sure this document stays updated with all the required changes if you run/alter this script.
Loading

0 comments on commit 4a74849

Please sign in to comment.