Skip to content

Commit

Permalink
collection CRUD sample
Browse files Browse the repository at this point in the history
  • Loading branch information
moderakh committed Oct 2, 2017
1 parent aaad9fa commit 1aa5860
Show file tree
Hide file tree
Showing 5 changed files with 353 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .gitignore
Expand Up @@ -19,3 +19,8 @@ hs_err_pid*

# Eclipse
.settings/
.classpath
.project

# Windows dll
*.dll
55 changes: 55 additions & 0 deletions examples/documentdb-examples/pom.xml
@@ -0,0 +1,55 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.microsoft.azure</groupId>
<artifactId>documentdb-examples</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>documentdb-examples</name>
<url>http://maven.apache.org</url>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<slf4j.version>1.7.6</slf4j.version>
<log4j.version>1.2.17</log4j.version>
</properties>

<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.microsoft.azure</groupId>
<artifactId>azure-documentdb</artifactId>
<version>1.13.0</version>
</dependency>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>23.0</version>
</dependency>
<dependency>
<groupId>org.hamcrest</groupId>
<artifactId>hamcrest-all</artifactId>
<version>1.3</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>${slf4j.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>log4j</groupId>
<artifactId>log4j</artifactId>
<version>${log4j.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,118 @@
/**
* The MIT License (MIT)
* Copyright (c) 2017 Microsoft Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.microsoft.azure.documentdb.examples;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.equalTo;

import java.util.Iterator;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.microsoft.azure.documentdb.Database;
import com.microsoft.azure.documentdb.DocumentClient;
import com.microsoft.azure.documentdb.DocumentClientException;
import com.microsoft.azure.documentdb.FeedResponse;
import com.microsoft.azure.documentdb.RequestOptions;
import com.microsoft.azure.documentdb.ResourceResponse;

public class DatabaseTests
{
private DocumentClient client;
private String databaseId = "exampleDB";

@Before
public void setUp() {
client = new DocumentClient(TestConfigurations.HOST, TestConfigurations.MASTER_KEY, null, null);
deleteDatabase();
}

@After
public void shutDown() {
deleteDatabase();
if (client != null) {
client.close();
}
}

@Test
public void databaseCreateAndRead() throws DocumentClientException {

Database databaseDefinition = new Database();
databaseDefinition.setId(databaseId);

// this will create a database with resource URI: /dbs/${databaseId}
RequestOptions options = new RequestOptions();
client.createDatabase(databaseDefinition, options);

String databaseLink = String.format("/dbs/%s", databaseId);

ResourceResponse<Database> response = client.readDatabase(databaseLink, options);
Database readDatabase = response.getResource();

assertThat(readDatabase.getId(), equalTo(databaseDefinition.getId()));
}

@Test
public void databaseCreateAndDelete() throws DocumentClientException {

Database databaseDefinition = new Database();
databaseDefinition.setId(databaseId);

// this will create a database with resource URI: /dbs/${databaseId}
RequestOptions options = new RequestOptions();
client.createDatabase(databaseDefinition, options);

String databaseLink = String.format("/dbs/%s", databaseId);

client.deleteDatabase(databaseLink, options);
}

@Test
public void databaseCreateAndQuery() throws DocumentClientException {

Database databaseDefinition = new Database();
databaseDefinition.setId(databaseId);

RequestOptions options = new RequestOptions();
client.createDatabase(databaseDefinition, options);

FeedResponse<Database> queryResults = client.queryDatabases(String.format("SELECT * FROM r where r.id = '%s'", databaseId), null);

Iterator<Database> it = queryResults.getQueryIterator();

assertThat(it.hasNext(), equalTo(true));
Database foundDatabase = it.next();
assertThat(foundDatabase.getId(), equalTo(databaseDefinition.getId()));
}

private void deleteDatabase() {
try {
client.deleteDatabase(String.format("/dbs/%s", databaseId), null);
} catch (Exception e) {
}
}
}
@@ -0,0 +1,137 @@
/**
* The MIT License (MIT)
* Copyright (c) 2017 Microsoft Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/

package com.microsoft.azure.documentdb.examples;

import java.util.ArrayList;
import java.util.Collection;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.microsoft.azure.documentdb.Database;
import com.microsoft.azure.documentdb.Document;
import com.microsoft.azure.documentdb.DocumentClient;
import com.microsoft.azure.documentdb.DocumentClientException;
import com.microsoft.azure.documentdb.DocumentCollection;
import com.microsoft.azure.documentdb.PartitionKeyDefinition;
import com.microsoft.azure.documentdb.RequestOptions;

public class DocumentCollectionTests
{
private DocumentClient client;
private String databaseId = "exampleDB";

@Before
public void setUp() throws DocumentClientException {
// create client
client = new DocumentClient(TestConfigurations.HOST, TestConfigurations.MASTER_KEY, null, null);

// removes the database "exampleDB" (if exists)
deleteDatabase();

// create database exampleDB;
Database databaseDefinition = new Database();
databaseDefinition.setId(databaseId);
client.createDatabase(databaseDefinition, null);
}

@After
public void shutDown() {
deleteDatabase();
if (client != null) {
client.close();
}
}

@Test
public void createSinglePartitionCollection() throws DocumentClientException {

String databaseLink = String.format("/dbs/%s", databaseId);
String collectionId = "testCollection";

DocumentCollection collectionDefinition = new DocumentCollection();
collectionDefinition.setId(collectionId);

// create a collection
client.createCollection(databaseLink, collectionDefinition, null);
}

@Test
public void deleteCollection() throws DocumentClientException {

String databaseLink = String.format("/dbs/%s", databaseId);
String collectionId = "testCollection";

// create collection
DocumentCollection collectionDefinition = new DocumentCollection();
collectionDefinition.setId(collectionId);
client.createCollection(databaseLink, collectionDefinition, null);

// delete collection
String collectionLink = String.format("/dbs/%s/colls/%s", databaseId, collectionId);
client.deleteCollection(collectionLink, null);
}

@Test
public void createMultiPartitionCollection() throws DocumentClientException {

String databaseLink = String.format("/dbs/%s", databaseId);
String collectionId = "testCollection";

DocumentCollection collectionDefinition = new DocumentCollection();
collectionDefinition.setId(collectionId);

// set /city as the partition key path
PartitionKeyDefinition partitionKeyDefinition = new PartitionKeyDefinition();
Collection<String> paths = new ArrayList<String>();
paths.add("/city");
partitionKeyDefinition.setPaths(paths);
collectionDefinition.setPartitionKey(partitionKeyDefinition);

// set the throughput to be 20,000
RequestOptions options = new RequestOptions();
options.setOfferThroughput(20000);

// create a collection
client.createCollection(databaseLink, collectionDefinition, options);

// create a document in the collection
String collectionLink = String.format("/dbs/%s/colls/%s", databaseId, collectionId);
Document documentDefinition = new Document(
"{" +
" \"id\": \"test-document\"," +
" \"city\" : \"Seattle\"" +
"} ") ;

client.createDocument(collectionLink, documentDefinition, options, false);
}

private void deleteDatabase() {
try {
client.deleteDatabase(String.format("/dbs/%s", databaseId), null);
} catch (Exception e) {
}
}
}
@@ -0,0 +1,38 @@
/**
* The MIT License (MIT)
* Copyright (c) 2017 Microsoft Corporation
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/


package com.microsoft.azure.documentdb.examples;

/**
* Contains the configurations for test file
*/
public final class TestConfigurations {
// Replace MASTER_KEY and HOST with values from your DocumentDB account.
// The default values are credentials of the local emulator, which are not used in any production environment.
// <!--[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine")]-->
public static final String MASTER_KEY =
"C2y6yDjf5/R+ob0N8A7Cgv30VRDJIWEHLM+4QDU5DE2nQ9nDuVTqobD4b8mGGyPMbIZnqyMsEcaGQy67XIw/Jw==";
public static final String HOST = "https://localhost:443/";

}

0 comments on commit 1aa5860

Please sign in to comment.