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

Testing for Tables Autorest Code #12512

Merged
merged 34 commits into from
Jul 7, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
34 commits
Select commit Hold shift + click to select a range
576d2be
apply old
eboyd23 Jun 15, 2020
dea2498
apply old
eboyd23 Jun 15, 2020
081200a
testing works on this commit
eboyd23 Jun 16, 2020
f6ea265
stashing
eboyd23 Jun 16, 2020
1e1efd8
additional testing
eboyd23 Jul 1, 2020
2397b88
stashing
eboyd23 Jun 22, 2020
c2e8eb7
stashing
eboyd23 Jun 24, 2020
b2ebec7
adding tests
eboyd23 Jun 25, 2020
d54ae25
fixing format and POM
eboyd23 Jun 25, 2020
ac21a3d
arrange-act-assert formatting
eboyd23 Jun 29, 2020
5086d4a
stashing
eboyd23 Jun 29, 2020
68f0cea
adding tests
eboyd23 Jun 30, 2020
f13cca6
reformat
eboyd23 Jun 30, 2020
d4c187a
remove system prints
eboyd23 Jun 30, 2020
335d646
adding playback jsons
eboyd23 Jun 30, 2020
7133df8
recent changes
eboyd23 Jun 30, 2020
2281563
fix pom
eboyd23 Jul 1, 2020
ba399fe
playback mode
eboyd23 Jul 1, 2020
ca8b84c
fix incorrect changes in readme
eboyd23 Jul 1, 2020
e849c5e
fix pom
eboyd23 Jul 1, 2020
0216fde
fixing pom
eboyd23 Jul 1, 2020
8b9e906
fixing version
eboyd23 Jul 1, 2020
3558d0f
fix impl
eboyd23 Jul 1, 2020
5f24375
fixing checkstyles
eboyd23 Jul 1, 2020
229116a
fixing checkstyles p2
eboyd23 Jul 1, 2020
06e255a
connie edits
eboyd23 Jul 2, 2020
8cb259d
connie edits 2
eboyd23 Jul 2, 2020
9b19368
forgot in last commit
eboyd23 Jul 2, 2020
cef9820
add changelog
eboyd23 Jul 2, 2020
1315b59
connie comments
eboyd23 Jul 6, 2020
74c33b5
fixing pipeline issues
eboyd23 Jul 6, 2020
1351fbe
assertion for setup
eboyd23 Jul 6, 2020
b7f8400
Merge branch 'master' of https://github.com/Azure/azure-sdk-for-java …
eboyd23 Jul 6, 2020
767bfdf
fix impl
eboyd23 Jul 7, 2020
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
Next Next commit
apply old
  • Loading branch information
eboyd23 committed Jul 6, 2020
commit 576d2be4c20b40af506a25084bfa986a265af571
12 changes: 12 additions & 0 deletions sdk/tables/azure-data-tables/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,18 @@ Licensed under the MIT License.
<artifactId>azure-core</artifactId>
<version>1.6.0</version> <!-- {x-version-update;com.azure:azure-core;dependency} -->
</dependency>
<dependency>
<groupId>com.azure</groupId>
<artifactId>azure-storage-common</artifactId>
<version>12.7.0-beta.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.8.2</version>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,134 @@
package com.azure.data.tables;

import org.junit.jupiter.api.Test;
import reactor.core.publisher.Mono;

import java.time.Duration;
import java.util.concurrent.TimeUnit;

public class AzureTablesAsyncClientTest {
@Test
void createTableTest() {
// Ideally, we'd use a builder to create this rather than a constructor.
AzureTableAsyncClient client = new AzureTableAsyncClient();

// Every time this variable is used in a downstream subscriber, it will invoke the `createTable` operation.
Mono<AzureTable> createTableMono = client.createTable("my-table");

// This is a subscription to the Mono. `subscribe` is a non-blocking call.
createTableMono.subscribe(table -> {
System.out.println("Table created is: " + table.getName());
}, error -> {
System.err.println("There was an error creating the table. " + error);
});

// Since .subscribe is a non-blocking call, after it hooks up the asynchronous operation, it will move
// onto the next statement. If `client.createTable` takes 30 seconds, the program would have ended before we
// ever know the response because it will leave this method.
// You'll see that there is no "System.out.println" console output because the program has ended.
}

/**
* Notice how "CREATING TABLE with name: my-table" errors when we try to add another entrythe second time? This is
* because, every time we chain the `createTableMono`, it invokes that `createTable` operation again.
* After an error occurs in one of the operations upstream in the chain, it will call the (error) -> { } handler.
* You'll see that we didn't try to add another entity (ie. new-key-2).
*
* See {@link #createAndUpdateTableFixed()} for a resolved version.
*/
@Test
void createAndUpdateTable() throws InterruptedException {
AzureTableAsyncClient client = new AzureTableAsyncClient();

// Every time this variable is used in a downstream subscriber, it will invoke the `createTable` operation.
Mono<AzureTable> createTableMono = client.createTable("my-table");

// FirstMono -> SecondFlatMap -> Map -> Subscribe
createTableMono.flatMap(azureTable -> {
// We are chaining another operation to this table creation. We want to use the resulting table and
// create an entity in it. We use `flatMap` because it is asynchronous (ie. returns Mono or Flux).
Mono<AzureTableEntity> entity = azureTable.createEntity("my-key", "my-value");
// We return the `createEntity` operation.
return entity;
}).map(azureTableEntity -> {
// This is a transformation, maybe we only care about the value of that entity we added.
return azureTableEntity.getValue();
}).subscribe(theValue -> {
System.out.println("This was added: " + theValue);
}, error -> {
System.err.println("Error: " + error);
});

createTableMono.flatMap(azureTable -> {
// We are chaining another operation to this table creation. We want to use the resulting table and
// create an entity in it. We use `flatMap` because it is asynchronous (ie. returns Mono or Flux).
return azureTable.createEntity("my-key-2", "my-value-2");
}).map(azureTableEntity -> {
// This is a transformation, maybe we only care about the value of that entity we added.
return azureTableEntity.getValue();
}).subscribe(theValue -> {
System.out.println("This was added: " + theValue);
}, error -> {
System.err.println("Error: " + error);
});

TimeUnit.SECONDS.sleep(20);
}

/**
* We've fixed this by caching the result of the `createTable` operation if it is successful. So we don't try to
* create the table again.
*
* See {@link #createAndUpdateTable()}
*/
@Test
void createAndUpdateTableFixed() throws InterruptedException {
AzureTableAsyncClient client = new AzureTableAsyncClient();

// Every time this variable is used in a downstream subscriber, it will invoke the `createTable` operation.
Mono<AzureTable> createTableMono = client.createTable("my-table")
.cache(success -> {
System.out.println("--- Table added. Caching value.");
return Duration.ofSeconds(Long.MAX_VALUE);
}, error -> {
System.out.println("--- Error while adding table. Not caching value.");
return Duration.ZERO;
}, () -> {
// This can occur because Monos can output 0 or 1 items. This would be the case where it output 0 items.
// For example, Mono.empty() will complete, but not output any items.
System.out.println("--- Expected a table to be output, not an empty value. Not caching.");
return Duration.ZERO;
});

// FirstMono -> SecondFlatMap -> Map -> Subscribe
createTableMono.flatMap(azureTable -> {
// We are chaining another operation to this table creation. We want to use the resulting table and
// create an entity in it. We use `flatMap` because it is asynchronous (ie. returns Mono or Flux).
Mono<AzureTableEntity> entity = azureTable.createEntity("my-new-key", "my-new-value");
// We return the `createEntity` operation.
return entity;
}).map(azureTableEntity -> {
// This is a transformation, maybe we only care about the value of that entity we added.
return azureTableEntity.getValue();
}).subscribe(theValue -> {
System.out.println("This was added: " + theValue);
}, error -> {
System.err.println("ERROR: " + error);
});

createTableMono.flatMap(azureTable -> {
// We are chaining another operation to this table creation. We want to use the resulting table and
// create an entity in it. We use `flatMap` because it is asynchronous (ie. returns Mono or Flux).
return azureTable.createEntity("my-new-key-2", "my-new-value-2");
}).map(azureTableEntity -> {
// This is a transformation, maybe we only care about the value of that entity we added.
return azureTableEntity.getValue();
}).subscribe(theValue -> {
System.out.println("This was added: " + theValue);
}, error -> {
System.err.println("ERROR: " + error);
});

TimeUnit.SECONDS.sleep(20);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,201 @@
package com.azure.data.tables;

import com.azure.core.http.*;
import com.azure.core.http.policy.*;

import com.azure.core.util.Context;
import com.azure.core.util.logging.ClientLogger;
import com.azure.data.tables.implementation.AzureTableImpl;
import com.azure.data.tables.implementation.AzureTableImplBuilder;

import com.azure.data.tables.implementation.TablesImpl;
import com.azure.data.tables.implementation.models.OdataMetadataFormat;
import com.azure.data.tables.implementation.models.QueryOptions;
import com.azure.data.tables.implementation.models.ResponseFormat;
import com.azure.data.tables.implementation.models.TableProperties;
import com.azure.storage.common.StorageSharedKeyCredential;
import com.azure.storage.common.implementation.connectionstring.StorageAuthenticationSettings;
import com.azure.storage.common.implementation.connectionstring.StorageConnectionString;

import com.azure.storage.common.policy.StorageSharedKeyCredentialPolicy;
import reactor.test.StepVerifier;



import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.UUID;

public class autorestTest {


@org.junit.Test
void createAndUpdateTableFixed() {
final String connectionString = System.getenv("azure_tables_connection_string");
final List<HttpPipelinePolicy> policies = new ArrayList<>();

StorageConnectionString storageConnectionString
= StorageConnectionString.create(connectionString, new ClientLogger("tables"));

StorageAuthenticationSettings authSettings = storageConnectionString.getStorageAuthSettings();
StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(authSettings.getAccount().getName(),
authSettings.getAccount().getAccessKey());

//storagesharedkey object and the storage auth object
policies.add(new AddDatePolicy());
policies.add(new StorageSharedKeyCredentialPolicy(sharedKeyCredential));
//HttpLoggingPolicy()

final HttpPipeline pipeline = new HttpPipelineBuilder()
.httpClient(null)
.policies(policies.toArray(new HttpPipelinePolicy[0]))
.build();

AzureTableImplBuilder azureTableImplBuilder = new AzureTableImplBuilder();
AzureTableImpl azureTable = azureTableImplBuilder
.pipeline(pipeline)
.url("https://telboytrial.table.core.windows.net/")
.buildClient();

try{
TablesImpl tables = azureTable.getTables();

StepVerifier.create(tables.deleteWithResponseAsync("ebTable","ID23",Context.NONE))
.assertNext(response -> {
System.out.println(response);
Assertions.assertEquals(200, response.getStatusCode());
})
.expectComplete()
.verify();
} catch (Exception e){
System.out.print(e);
}

}


@org.junit.Test
void fromConnie () {
final List<HttpPipelinePolicy> policies = Arrays.asList(
new AddDatePolicy(),
new AddHeadersPolicy(new HttpHeaders().put("Accept", OdataMetadataFormat.APPLICATION_JSON_ODATA_MINIMALMETADATA.toString())),
new TablesSharedKeyCredentialPolicy(sharedKeyCredential),
new HttpLoggingPolicy(new HttpLogOptions()
.setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS))
);
final HttpPipeline pipeline = new HttpPipelineBuilder()
.policies(policies.toArray(new HttpPipelinePolicy[0]))
.build();
AzureTableImpl azureTable = new AzureTableImplBuilder()
.pipeline(pipeline)
.version("2019-02-02")
.url("https://azsstgt28603173b8764a45.table.core.windows.net/")
.buildClient();
TableProperties tableProperties = new TableProperties().setTableName("ebTableSeven");
QueryOptions queryOptions = new QueryOptions();
String requestId = UUID.randomUUID().toString();
StepVerifier.create(azureTable.getTables().createWithResponseAsync(tableProperties, requestId,
ResponseFormat.RETURN_CONTENT, queryOptions, Context.NONE))
.assertNext(response -> {
System.out.println(response);
Assertions.assertEquals(201, response.getStatusCode());
})
.expectComplete()
.verify();
}




@org.junit.Test
void TablesAuth() {
final String connectionString = System.getenv("azure_tables_connection_string");
final List<HttpPipelinePolicy> policies = new ArrayList<>();

StorageConnectionString storageConnectionString
= StorageConnectionString.create(connectionString, new ClientLogger("tables"));
System.out.println(storageConnectionString);

StorageAuthenticationSettings authSettings = storageConnectionString.getStorageAuthSettings();

TablesSharedKeyCredential sharedKeyCredential = new TablesSharedKeyCredential(authSettings.getAccount().getName(),
authSettings.getAccount().getAccessKey());

//storagesharedkey object and the storage auth object
policies.add(new AddDatePolicy());
policies.add(new TablesSharedKeyCredentialPolicy(sharedKeyCredential));
policies.add(new HttpLoggingPolicy(new HttpLogOptions().setLogLevel(HttpLogDetailLevel.BODY_AND_HEADERS)));
//HttpLoggingPolicy()

final HttpPipeline pipeline = new HttpPipelineBuilder()
.httpClient(null)
.policies(policies.toArray(new HttpPipelinePolicy[0]))
.build();

AzureTableImplBuilder azureTableImplBuilder = new AzureTableImplBuilder();
AzureTableImpl azureTable = azureTableImplBuilder
.pipeline(pipeline)
.url("https://telboytrial.table.core.windows.net/")
.buildClient();

try{
TablesImpl tables = azureTable.getTables();

StepVerifier.create(tables.createWithResponseAsync(new TableProperties().setTableName("ebTable"),
"ID23",
ResponseFormat.RETURN_CONTENT,
null,null))
.assertNext(response -> {
System.out.println(response);
Assertions.assertEquals(200, response.getStatusCode());
})
.expectComplete()
.verify();
} catch (Exception e){
System.out.print(e);
}

// try{
// TablesImpl tables = azureTable.getTables();
//
// StepVerifier.create(tables.deleteWithResponseAsync("ebTable","ID23",Context.NONE))
// .assertNext(response -> {
// System.out.println(response);
// Assertions.assertEquals(200, response.getStatusCode());
// })
// .expectComplete()
// .verify();
// } catch (Exception e){
// System.out.print(e);
// }

}









// @Test
// void createTableTest() {
// StringToSign = VERB + "\n" +
// Content-MD5 + "\n" +
// Content-Type + "\n" +
// Date + "\n" +
// CanonicalizedResource;
//
// }
//
//
// @Test
// void createAndUpdateTable() throws InterruptedException {
// TablesImpl ti = new TablesImpl( new AzureTableImpl());
// Mono<TablesCreateResponse> c = ti.createWithResponseAsync( new TableProperties(), "requestId", RETURN_CONTENT,
// null , new Context("key", "value"));
// }

}