Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

README - compilable code snippets #6609

Merged
merged 6 commits into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -558,7 +558,7 @@
<Match>
<Or>
<Class name="~.*JavaDoc(CodeSnippets|CodeSamples|Samples)"/>
<Class name="com.azure.storage.blob.batch.ReadmeCodeSamples"/>
<Class name="~.*ReadmeSamples"/>
<Class name="com.azure.storage.file.datalake.GetSetAccessControlExample"/>
</Or>
<Bug pattern="DLS_DEAD_LOCAL_STORE,
Expand Down
2 changes: 2 additions & 0 deletions sdk/core/azure-core-http-netty/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ The following sections provide several code snippets covering some of the most c

Create a Netty Http client that uses port 80 and has no proxy.

<!-- embedme ./src/samples/java/com/azure/core/http/netty/ReadmeSamples.java#L23-L23 -->
```java
HttpClient client = new NettyAsyncHttpClientBuilder().build();
```
Expand All @@ -41,6 +42,7 @@ HttpClient client = new NettyAsyncHttpClientBuilder().build();

Create a Netty Http client that is using a proxy.

<!-- embedme ./src/samples/java/com/azure/core/http/netty/ReadmeSamples.java#L30-L32 -->
```java
HttpClient client = new NettyAsyncHttpClientBuilder()
.proxy(new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress("<proxy-host>", 8888)))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.core.http.netty;

import com.azure.core.http.HttpClient;
import com.azure.core.http.ProxyOptions;
import java.net.InetSocketAddress;

/**
* WARNING: MODIFYING THIS FILE WILL REQUIRE CORRESPONDING UPDATES TO README.md FILE. LINE NUMBERS
* ARE USED TO EXTRACT APPROPRIATE CODE SEGMENTS FROM THIS FILE. ADD NEW CODE AT THE BOTTOM TO AVOID CHANGING
* LINE NUMBERS OF EXISTING CODE SAMPLES.
*
* Class containing code snippets that will be injected to README.md.
*/
public class ReadmeSamples {

/**
* Sample code for creating async Netty HTTP client.
*/
public void createBasicClient() {
HttpClient client = new NettyAsyncHttpClientBuilder().build();
}

/**
* Sample code for creating async Netty HTTP client with proxy.
*/
public void createProxyClient() {
HttpClient client = new NettyAsyncHttpClientBuilder()
.proxy(new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress("<proxy-host>", 8888)))
.build();
}

}
4 changes: 3 additions & 1 deletion sdk/core/azure-core-http-okhttp/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ The following sections provide several code snippets covering some of the most c

Create an OkHttp client using a connection timeout of 60 seconds and a read timeout of 120 seconds.

<!-- embedme ./src/samples/java/com/azure/core/http/okhttp/ReadmeSamples.java#L23-L23 -->
```java
HttpClient client = new OkHttpAsyncHttpClientBuilder().build();
```
Expand All @@ -41,9 +42,10 @@ HttpClient client = new OkHttpAsyncHttpClientBuilder().build();

Create an OkHttp client that is using a proxy.

<!-- embedme ./src/samples/java/com/azure/core/http/okhttp/ReadmeSamples.java#L30-L32 -->
```java
HttpClient client = new OkHttpAsyncHttpClientBuilder()
.proxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("<proxy-host>", 8888)))
.proxy(new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress("<proxy-host>", 8888)))
.build();
```

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.core.http.okhttp;

import com.azure.core.http.HttpClient;
import com.azure.core.http.ProxyOptions;
import java.net.InetSocketAddress;

/**
* WARNING: MODIFYING THIS FILE WILL REQUIRE CORRESPONDING UPDATES TO README.md FILE. LINE NUMBERS
* ARE USED TO EXTRACT APPROPRIATE CODE SEGMENTS FROM THIS FILE. ADD NEW CODE AT THE BOTTOM TO AVOID CHANGING
* LINE NUMBERS OF EXISTING CODE SAMPLES.
*
* Class containing code snippets that will be injected to README.md.
*/
public class ReadmeSamples {

/**
* Sample code for creating async Netty HTTP client.
*/
public void createBasicClient() {
HttpClient client = new OkHttpAsyncHttpClientBuilder().build();
}

/**
* Sample code for creating async Netty HTTP client with proxy.
*/
public void createProxyClient() {
HttpClient client = new OkHttpAsyncHttpClientBuilder()
.proxy(new ProxyOptions(ProxyOptions.Type.HTTP, new InetSocketAddress("<proxy-host>", 8888)))
.build();
}

}
45 changes: 30 additions & 15 deletions sdk/core/azure-core-test/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,17 @@ To use this package, add the following to your _pom.xml_.
Use [TestBase][TestBase.java] to easily create live and playback test cases. Extending from `TestBase` provides an
`interceptorManager` that keeps track of all network calls.

<!-- embedme ./src/samples/java/com/azure/core/test/ReadmeSamples.java#L27-L39 -->
```java
// Set the AZURE_TEST_MODE environment variable to either PLAYBACK or RECORD to determine if tests are playback or
// live. By default, tests are run in playback mode.
/**
* Set the AZURE_TEST_MODE environment variable to either PLAYBACK or RECORD to determine if tests are playback or
* live. By default, tests are run in playback mode.
*/
public class SessionTests extends TestBase {
// Use your JUnit or TestNG annotation here for your testcase

/**
* Use JUnit or TestNG annotation here for your testcase
*/
public void fooTest() {
// Do some network calls.
}
Expand All @@ -52,39 +58,48 @@ public class SessionTests extends TestBase {
Record network calls using [RecordNetworkCallPolicy][RecordNetworkCallPolicy.java]. Each HTTP request sent from the test
client, is persisted to [RecordedData][RecordedData.java].

<!-- embedme ./src/samples/java/com/azure/core/test/ReadmeSamples.java#L41-L60 -->
```java
/**
* Sample code for recording network calls.
*/
public class Foo {
public void recordNetworkCalls() {
// All network calls are kept in the networkData variable.
RecordedData networkData = new RecordedData();
HttpPipeline pipeline = new HttpPipeline(new RecordNetworkCallPolicy(recordedData));

// All network calls are kept in the recordedData variable.
RecordedData recordedData = new RecordedData();
HttpPipeline pipeline = new HttpPipelineBuilder()
.policies(new RecordNetworkCallPolicy(recordedData))
.build();

// Send requests through the HttpPipeline.
pipeline.send(new HttpRequest(HttpMethod.GET, "http://bing.com"));

// Get a record that was sent through the pipeline.
NetworkCallRecord networkCall = networkData.findFirstAndRemoveNetworkCall(record -> {
return record.uri().equals("http://bing.com");
NetworkCallRecord networkCall = recordedData.findFirstAndRemoveNetworkCall(record -> {
return record.getUri().equals("http://bing.com");
});
}
}

```

### Playback session records

Playback test session records by creating a [RecordedData][RecordedData.java].

<!-- embedme ./src/samples/java/com/azure/core/test/ReadmeSamples.java#L62-L78 -->
```java
public class Foo {
/**
* Sample code for using playback to test.
*/
public class FooBar {
public void playbackNetworkCalls() {
RecordedData recordedData = new RecordedData();

// Add some network calls to be replayed by playbackClient

// Creates a HTTP client that plays back responses in recordedData.
HttpClient playbackClient = new PlaybackClient(recordedData, null);

// Send an HTTP GET request to http://bing.com. If recordedData contains a NetworkCallRecord with a matching HTTP
// method and matching URL, it is returned as a response.
Mono<HttpResponse> response = playbackClient.send(new HttpRequest(HttpMethod.GET, "http://bing.com"));
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.azure.core.test;

import com.azure.core.http.HttpClient;
import com.azure.core.http.HttpMethod;
import com.azure.core.http.HttpPipeline;
import com.azure.core.http.HttpPipelineBuilder;
import com.azure.core.http.HttpRequest;
import com.azure.core.http.HttpResponse;
import com.azure.core.test.http.PlaybackClient;
import com.azure.core.test.models.NetworkCallRecord;
import com.azure.core.test.models.RecordedData;
import com.azure.core.test.policy.RecordNetworkCallPolicy;
import reactor.core.publisher.Mono;

/**
* WARNING: MODIFYING THIS FILE WILL REQUIRE CORRESPONDING UPDATES TO README.md FILE. LINE NUMBERS
* ARE USED TO EXTRACT APPROPRIATE CODE SEGMENTS FROM THIS FILE. ADD NEW CODE AT THE BOTTOM TO AVOID CHANGING
* LINE NUMBERS OF EXISTING CODE SAMPLES.
*
* Class containing code snippets that will be injected to README.md.
*/
public class ReadmeSamples {

/**
* Set the AZURE_TEST_MODE environment variable to either PLAYBACK or RECORD to determine if tests are playback or
* live. By default, tests are run in playback mode.
*/
public class SessionTests extends TestBase {

/**
* Use JUnit or TestNG annotation here for your testcase
*/
public void fooTest() {
// Do some network calls.
}
}

/**
* Sample code for recording network calls.
*/
public class Foo {
public void recordNetworkCalls() {
// All network calls are kept in the recordedData variable.
RecordedData recordedData = new RecordedData();
HttpPipeline pipeline = new HttpPipelineBuilder()
.policies(new RecordNetworkCallPolicy(recordedData))
.build();

// Send requests through the HttpPipeline.
pipeline.send(new HttpRequest(HttpMethod.GET, "http://bing.com"));

// Get a record that was sent through the pipeline.
NetworkCallRecord networkCall = recordedData.findFirstAndRemoveNetworkCall(record -> {
return record.getUri().equals("http://bing.com");
});
}
}

/**
* Sample code for using playback to test.
*/
public class FooBar {
public void playbackNetworkCalls() {
RecordedData recordedData = new RecordedData();

// Add some network calls to be replayed by playbackClient

// Creates a HTTP client that plays back responses in recordedData.
HttpClient playbackClient = new PlaybackClient(recordedData, null);

// Send an HTTP GET request to http://bing.com. If recordedData contains a NetworkCallRecord with a matching HTTP
// method and matching URL, it is returned as a response.
Mono<HttpResponse> response = playbackClient.send(new HttpRequest(HttpMethod.GET, "http://bing.com"));
}
}
}
Loading