Skip to content

Commit

Permalink
Add createIfNotExists to TableServiceClients
Browse files Browse the repository at this point in the history
  • Loading branch information
Brandon Siegel committed Aug 29, 2020
1 parent 844b4ae commit f1fc692
Show file tree
Hide file tree
Showing 12 changed files with 513 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.azure.core.http.HttpHeaders;
import com.azure.core.http.HttpPipeline;
import com.azure.core.http.HttpRequest;
import com.azure.core.http.HttpResponse;
import com.azure.core.http.rest.PagedFlux;
import com.azure.core.http.rest.PagedResponse;
import com.azure.core.http.rest.Response;
Expand All @@ -25,6 +26,7 @@
import com.azure.data.tables.implementation.models.TableProperties;
import com.azure.data.tables.implementation.models.TableQueryResponse;
import com.azure.data.tables.implementation.models.TableResponseProperties;
import com.azure.data.tables.implementation.models.TableServiceErrorException;
import com.azure.data.tables.models.ListTablesOptions;
import com.azure.data.tables.models.TableItem;
import java.net.URI;
Expand Down Expand Up @@ -139,6 +141,39 @@ Mono<Response<Void>> createTableWithResponse(String tableName, Context context)
}
}

/**
* creates the table with the given name if it does not exist, otherwise no action is taken.
*
* @param tableName the name of the table to create
* @return mono void
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Void> createTableIfNotExists(String tableName) {
return createTableIfNotExistsWithResponse(tableName).flatMap(response -> Mono.justOrEmpty(response.getValue()));
}

/**
* creates the table with the given name if it does not exist, otherwise no action is taken.
*
* @param tableName the name of the table to create
* @return a response
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Mono<Response<Void>> createTableIfNotExistsWithResponse(String tableName) {
return withContext(context -> createTableIfNotExistsWithResponse(tableName, context));
}

Mono<Response<Void>> createTableIfNotExistsWithResponse(String tableName, Context context) {
return createTableWithResponse(tableName, context).onErrorResume(e -> e instanceof TableServiceErrorException
&& ((TableServiceErrorException) e).getResponse() != null
&& ((TableServiceErrorException) e).getResponse().getStatusCode() == 409,
e -> {
HttpResponse response = ((TableServiceErrorException) e).getResponse();
return Mono.just(new SimpleResponse<>(response.getRequest(), response.getStatusCode(),
response.getHeaders(), null));
});
}

/**
* deletes the given table. Will error if the table doesn't exists or cannot be found with the given name.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,28 @@ public Response<Void> createTableWithResponse(String tableName, Context context)
return client.createTableWithResponse(tableName, context).block();
}

/**
* creates the table with the given name if it does not exist, otherwise no action is taken.
*
* @param tableName the name of the table to create
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public void createTableIfNotExists(String tableName) {
client.createTableIfNotExists(tableName).block();
}

/**
* creates the table with the given name if it does not exist, otherwise no action is taken.
*
* @param tableName the name of the table to create
* @param context the context of the query
* @return response
*/
@ServiceMethod(returns = ReturnType.SINGLE)
public Response<Void> createTableIfNotExistsWithResponse(String tableName, Context context) {
return client.createTableIfNotExistsWithResponse(tableName, context).block();
}

/**
* deletes the given table. Will error if the table doesn't exists or cannot be found with the given name.
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import com.azure.core.http.policy.HttpLogOptions;
import com.azure.core.http.policy.RetryPolicy;
import com.azure.core.test.TestBase;
import com.azure.data.tables.implementation.models.TableServiceErrorException;
import com.azure.data.tables.models.ListTablesOptions;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.Assertions;
Expand Down Expand Up @@ -65,6 +66,19 @@ void serviceCreateTableAsync() {
.verify();
}

@Test
void serviceCreateTableFailsIfExistsAsync() {
// Arrange
String tableName = testResourceNamer.randomName("test", 20);
serviceClient.createTable(tableName).block(TIMEOUT);

//Act & Assert
StepVerifier.create(serviceClient.createTable(tableName))
.expectErrorMatches(e -> e instanceof TableServiceErrorException
&& ((TableServiceErrorException) e).getResponse().getStatusCode() == 409)
.verify();
}

@Test
void serviceCreateTableWithResponseAsync() {
// Arrange
Expand All @@ -75,7 +89,60 @@ void serviceCreateTableWithResponseAsync() {
StepVerifier.create(serviceClient.createTableWithResponse(tableName))
.assertNext(response -> {
Assertions.assertEquals(expectedStatusCode, response.getStatusCode());
})
.expectComplete()
.verify();
}

@Test
void serviceCreateTableIfNotExistsAsync() {
// Arrange
String tableName = testResourceNamer.randomName("test", 20);

//Act & Assert
StepVerifier.create(serviceClient.createTableIfNotExists(tableName))
.expectComplete()
.verify();
}

@Test
void serviceCreateTableIfNotExistsSucceedsIfExistsAsync() {
// Arrange
String tableName = testResourceNamer.randomName("test", 20);
serviceClient.createTable(tableName).block(TIMEOUT);

//Act & Assert
StepVerifier.create(serviceClient.createTableIfNotExists(tableName))
.expectComplete()
.verify();
}

@Test
void serviceCreateTableIfNotExistsWithResponseAsync() {
// Arrange
String tableName = testResourceNamer.randomName("test", 20);
int expectedStatusCode = 204;

//Act & Assert
StepVerifier.create(serviceClient.createTableIfNotExistsWithResponse(tableName))
.assertNext(response -> {
Assertions.assertEquals(expectedStatusCode, response.getStatusCode());
})
.expectComplete()
.verify();
}

@Test
void serviceCreateTableIfNotExistsWithResponseSucceedsIfExistsAsync() {
// Arrange
String tableName = testResourceNamer.randomName("test", 20);
int expectedStatusCode = 409;
serviceClient.createTable(tableName).block(TIMEOUT);

//Act & Assert
StepVerifier.create(serviceClient.createTableIfNotExistsWithResponse(tableName))
.assertNext(response -> {
Assertions.assertEquals(expectedStatusCode, response.getStatusCode());
})
.expectComplete()
.verify();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import com.azure.core.http.policy.HttpLogOptions;
import com.azure.core.http.policy.RetryPolicy;
import com.azure.core.test.TestBase;
import com.azure.data.tables.implementation.models.TableServiceErrorException;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

public class TableServiceClientTest extends TestBase {
Expand Down Expand Up @@ -36,8 +38,37 @@ void serviceCreateTable() {
// Arrange
String tableName = testResourceNamer.randomName("test", 20);

// Act
// Act & Assert
serviceClient.createTable(tableName);
}

@Test
void serviceCreateTableFailsIfExists() {
// Arrange
String tableName = testResourceNamer.randomName("test", 20);
serviceClient.createTable(tableName);

// Act & Assert
Assertions.assertThrows(TableServiceErrorException.class, () -> serviceClient.createTable(tableName));
}

@Test
void serviceCreateTableIfNotExists() {
// Arrange
String tableName = testResourceNamer.randomName("test", 20);

// Act & Assert
serviceClient.createTableIfNotExists(tableName);
}

@Test
void serviceCreateTableIfNotExistsSucceedsIfExists() {
// Arrange
String tableName = testResourceNamer.randomName("test", 20);
serviceClient.createTable(tableName);

//Act & Assert
serviceClient.createTableIfNotExists(tableName);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"networkCallRecords" : [ {
"Method" : "POST",
"Uri" : "https://REDACTED.table.core.windows.net/Tables",
"Headers" : {
"x-ms-version" : "2019-02-02",
"User-Agent" : "azsdk-java-UnknownName/UnknownVersion (11.0.8; Mac OS X; 10.15.6)",
"x-ms-client-request-id" : "d86532b0-9bfa-474d-9a54-49d1c0294cd2",
"Content-Type" : "application/json"
},
"Response" : {
"x-ms-version" : "2019-02-02",
"Server" : "Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0",
"X-Content-Type-Options" : "nosniff",
"retry-after" : "0",
"StatusCode" : "204",
"Date" : "Thu, 27 Aug 2020 21:38:34 GMT",
"Cache-Control" : "no-cache",
"DataServiceId" : "https://brsiegelsample.table.core.windows.net/Tables('test15790b43b7')",
"Content-Length" : "0",
"x-ms-request-id" : "f56aa02c-4002-00c8-61ba-7c286d000000",
"Preference-Applied" : "return-no-content",
"x-ms-client-request-id" : "d86532b0-9bfa-474d-9a54-49d1c0294cd2",
"Location" : "https://brsiegelsample.table.core.windows.net/Tables('test15790b43b7')"
},
"Exception" : null
}, {
"Method" : "POST",
"Uri" : "https://REDACTED.table.core.windows.net/Tables",
"Headers" : {
"x-ms-version" : "2019-02-02",
"User-Agent" : "azsdk-java-UnknownName/UnknownVersion (11.0.8; Mac OS X; 10.15.6)",
"x-ms-client-request-id" : "f8f6d880-45e0-4b30-b42f-8c55014bcc94",
"Content-Type" : "application/json"
},
"Response" : {
"Transfer-Encoding" : "chunked",
"x-ms-version" : "2019-02-02",
"Server" : "Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0",
"X-Content-Type-Options" : "nosniff",
"retry-after" : "0",
"StatusCode" : "409",
"Date" : "Thu, 27 Aug 2020 21:38:34 GMT",
"Cache-Control" : "no-cache",
"x-ms-request-id" : "f56aa030-4002-00c8-63ba-7c286d000000",
"Body" : "{\"odata.error\":{\"code\":\"TableAlreadyExists\",\"message\":{\"lang\":\"en-US\",\"value\":\"The table specified already exists.\\nRequestId:f56aa030-4002-00c8-63ba-7c286d000000\\nTime:2020-08-27T21:38:34.8728592Z\"}}}",
"Preference-Applied" : "return-no-content",
"x-ms-client-request-id" : "f8f6d880-45e0-4b30-b42f-8c55014bcc94",
"Content-Type" : "application/json;odata=minimalmetadata;streaming=true;charset=utf-8"
},
"Exception" : null
} ],
"variables" : [ "test15790b43b7" ]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
{
"networkCallRecords" : [ {
"Method" : "POST",
"Uri" : "https://REDACTED.table.core.windows.net/Tables",
"Headers" : {
"x-ms-version" : "2019-02-02",
"User-Agent" : "azsdk-java-UnknownName/UnknownVersion (11.0.8; Mac OS X; 10.15.6)",
"x-ms-client-request-id" : "4b14eb7d-4782-47b6-87f1-ef7c18ec6375",
"Content-Type" : "application/json"
},
"Response" : {
"x-ms-version" : "2019-02-02",
"Server" : "Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0",
"X-Content-Type-Options" : "nosniff",
"retry-after" : "0",
"StatusCode" : "204",
"Date" : "Thu, 27 Aug 2020 21:37:02 GMT",
"Cache-Control" : "no-cache",
"DataServiceId" : "https://brsiegelsample.table.core.windows.net/Tables('test95670e2797')",
"Content-Length" : "0",
"x-ms-request-id" : "18358b1a-5002-0030-3fba-7c7493000000",
"Preference-Applied" : "return-no-content",
"x-ms-client-request-id" : "4b14eb7d-4782-47b6-87f1-ef7c18ec6375",
"Location" : "https://brsiegelsample.table.core.windows.net/Tables('test95670e2797')"
},
"Exception" : null
}, {
"Method" : "POST",
"Uri" : "https://REDACTED.table.core.windows.net/Tables",
"Headers" : {
"x-ms-version" : "2019-02-02",
"User-Agent" : "azsdk-java-UnknownName/UnknownVersion (11.0.8; Mac OS X; 10.15.6)",
"x-ms-client-request-id" : "8866db20-4dbd-4b0c-842e-f9b9d5820482",
"Content-Type" : "application/json"
},
"Response" : {
"Transfer-Encoding" : "chunked",
"x-ms-version" : "2019-02-02",
"Server" : "Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0",
"X-Content-Type-Options" : "nosniff",
"retry-after" : "0",
"StatusCode" : "409",
"Date" : "Thu, 27 Aug 2020 21:37:02 GMT",
"Cache-Control" : "no-cache",
"x-ms-request-id" : "18358b2d-5002-0030-50ba-7c7493000000",
"Body" : "{\"odata.error\":{\"code\":\"TableAlreadyExists\",\"message\":{\"lang\":\"en-US\",\"value\":\"The table specified already exists.\\nRequestId:18358b2d-5002-0030-50ba-7c7493000000\\nTime:2020-08-27T21:37:02.7996625Z\"}}}",
"Preference-Applied" : "return-no-content",
"x-ms-client-request-id" : "8866db20-4dbd-4b0c-842e-f9b9d5820482",
"Content-Type" : "application/json;odata=minimalmetadata;streaming=true;charset=utf-8"
},
"Exception" : null
} ],
"variables" : [ "test95670e2797" ]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"networkCallRecords" : [ {
"Method" : "POST",
"Uri" : "https://REDACTED.table.core.windows.net/Tables",
"Headers" : {
"x-ms-version" : "2019-02-02",
"User-Agent" : "azsdk-java-UnknownName/UnknownVersion (11.0.8; Mac OS X; 10.15.6)",
"x-ms-client-request-id" : "028ae606-12ab-429a-b0d0-a6ee00215377",
"Content-Type" : "application/json"
},
"Response" : {
"x-ms-version" : "2019-02-02",
"Server" : "Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0",
"X-Content-Type-Options" : "nosniff",
"retry-after" : "0",
"StatusCode" : "204",
"Date" : "Thu, 27 Aug 2020 21:38:31 GMT",
"Cache-Control" : "no-cache",
"DataServiceId" : "https://brsiegelsample.table.core.windows.net/Tables('test79947b18e1')",
"Content-Length" : "0",
"x-ms-request-id" : "8ef6e693-1002-0088-30ba-7c2f55000000",
"Preference-Applied" : "return-no-content",
"x-ms-client-request-id" : "028ae606-12ab-429a-b0d0-a6ee00215377",
"Location" : "https://brsiegelsample.table.core.windows.net/Tables('test79947b18e1')"
},
"Exception" : null
} ],
"variables" : [ "test79947b18e1" ]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"networkCallRecords" : [ {
"Method" : "POST",
"Uri" : "https://REDACTED.table.core.windows.net/Tables",
"Headers" : {
"x-ms-version" : "2019-02-02",
"User-Agent" : "azsdk-java-UnknownName/UnknownVersion (11.0.8; Mac OS X; 10.15.6)",
"x-ms-client-request-id" : "5c560ffe-c0ab-44a2-8dfd-c171966c9ecf",
"Content-Type" : "application/json"
},
"Response" : {
"x-ms-version" : "2019-02-02",
"Server" : "Windows-Azure-Table/1.0 Microsoft-HTTPAPI/2.0",
"X-Content-Type-Options" : "nosniff",
"retry-after" : "0",
"StatusCode" : "204",
"Date" : "Thu, 27 Aug 2020 21:36:51 GMT",
"Cache-Control" : "no-cache",
"DataServiceId" : "https://brsiegelsample.table.core.windows.net/Tables('test002158a072')",
"Content-Length" : "0",
"x-ms-request-id" : "140844c0-6002-003b-5bba-7c8ff8000000",
"Preference-Applied" : "return-no-content",
"x-ms-client-request-id" : "5c560ffe-c0ab-44a2-8dfd-c171966c9ecf",
"Location" : "https://brsiegelsample.table.core.windows.net/Tables('test002158a072')"
},
"Exception" : null
} ],
"variables" : [ "test002158a072" ]
}
Loading

0 comments on commit f1fc692

Please sign in to comment.