Skip to content

Commit

Permalink
Merge pull request Azure#58 from Azure/moderakh/2.0.x
Browse files Browse the repository at this point in the history
added support for Conflict.getResource(.) and synced code
  • Loading branch information
moderakh authored Aug 15, 2018
2 parents cc314c1 + 4eb39ea commit 0164ebe
Show file tree
Hide file tree
Showing 8 changed files with 146 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,4 @@ private void cleanUpGeneratedDatabases() {
}
}
}

32 changes: 32 additions & 0 deletions sdk/src/main/java/com/microsoft/azure/cosmosdb/Conflict.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
package com.microsoft.azure.cosmosdb;

import com.microsoft.azure.cosmosdb.internal.Constants;
import com.microsoft.azure.cosmosdb.rx.internal.Strings;

import java.lang.reflect.InvocationTargetException;

/**
* Represents a conflict in the version of a particular resource in the Azure Cosmos DB database service.
Expand Down Expand Up @@ -65,4 +68,33 @@ public String getOperationKind() {
public String getResouceType() {
return super.getString(Constants.Properties.RESOURCE_TYPE);
}

/**
* Gets the resource ID for the conflict in the Azure Cosmos DB service.
* @return resource Id for the conflict.
*/
public String getSourceResourceId() {
return super.getString(Constants.Properties.SOURCE_RESOURCE_ID);
}

/**
* Gets the conflicting resource in the Azure Cosmos DB service.
* @param <T> The type of the object.
* @param klass The returned type of conflicting resource.
* @return The conflicting resource.
*/
public <T extends Resource> T getResource(Class<T> klass) {
String resourceAsString = super.getString(Constants.Properties.CONTENT);

if (!Strings.isNullOrEmpty(resourceAsString)) {
try {
return klass.getConstructor(String.class).newInstance(resourceAsString);
} catch (InstantiationException | IllegalAccessException | IllegalArgumentException
| InvocationTargetException | NoSuchMethodException | SecurityException e) {
throw new IllegalStateException("Failed to instantiate class object.", e);
}
} else {
return null;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ public static final class Properties {
// Conflict.
public static final String CONFLICT = "conflict";
public static final String OPERATION_TYPE = "operationType";
public static final String SOURCE_RESOURCE_ID = "resourceId";

// Offer resource
public static final String OFFER_TYPE = "offerType";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@
import java.util.Locale;
import java.util.UUID;

import com.fasterxml.jackson.core.JsonParser;
import org.apache.commons.lang3.StringUtils;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.uuid.EthernetAddress;
Expand All @@ -62,7 +62,7 @@ public final class Utils {
// so Thu, 04 Jan 2018 00:30:37 GMT is accepted by the cosmos db service,
// but Thu, 4 Jan 2018 00:30:37 GMT is not.
// Therefore, we need a custom date time formatter.
private static final DateTimeFormatter RFC_1123_DATE_TIME = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss zzz",Locale.US);
private static final DateTimeFormatter RFC_1123_DATE_TIME = DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);

static {
Utils.simpleObjectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2232,7 +2232,7 @@ private Observable<ResourceResponse<Conflict>> deleteConflictInternal(String con
logger.debug("Deleting a Conflict. conflictLink [{}]", conflictLink);
String path = Utils.joinPath(conflictLink, null);
Map<String, String> requestHeaders = getRequestHeaders(options);
RxDocumentServiceRequest request = RxDocumentServiceRequest.create(OperationType.Read,
RxDocumentServiceRequest request = RxDocumentServiceRequest.create(OperationType.Delete,
ResourceType.Conflict, path, requestHeaders);
Observable<RxDocumentServiceRequest> reqObs = addPartitionKeyInformation(request, null, options).toObservable();
return reqObs.flatMap(req -> this.delete(request).map(response -> toResourceResponse(response, Conflict.class)));
Expand Down
70 changes: 70 additions & 0 deletions sdk/src/test/java/com/microsoft/azure/cosmosdb/ConflictTests.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
/*
* The MIT License (MIT)
* Copyright (c) 2018 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.cosmosdb;

import org.apache.commons.io.IOUtils;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class ConflictTests {
private String conflictAsString;

@BeforeClass(groups = { "unit" })
public void setup() throws Exception {
conflictAsString = IOUtils.toString(
getClass().getClassLoader().getResourceAsStream("sampleConflict.json"), "UTF-8");
}

@Test(groups = { "unit" })
public void getSourceResourceId() {
Conflict conf = new Conflict(conflictAsString);
assertThat(conf.getSourceResourceId()).isEqualTo("k6d9ALgBmD+ChB4AAAAAAA==");
}

@Test(groups = { "unit" })
public void getOperationKind() {
Conflict conf = new Conflict(conflictAsString);
assertThat(conf.getOperationKind()).isEqualTo("create");
conf.getSourceResourceId();
}

@Test(groups = { "unit" })
public void getResourceType() {
Conflict conf = new Conflict(conflictAsString);
assertThat(conf.getResouceType()).isEqualTo("document");
conf.getSourceResourceId();
}

@Test(groups = { "unit" })
public void getResource() {
Conflict conf = new Conflict(conflictAsString);
Document doc = conf.getResource(Document.class);
assertThat(doc.getId()).isEqualTo("0007312a-a1c5-4b54-9e39-35de2367fa33");
assertThat(doc.getInt("regionId")).isEqualTo(2);
assertThat(doc.getResourceId()).isEqualTo("k6d9ALgBmD+ChB4AAAAAAA==");
assertThat(doc.getETag()).isEqualTo("\"00000200-0000-0000-0000-5b6e214b0000\"");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,36 @@
import java.time.format.DateTimeFormatter;
import java.util.Locale;

import org.testng.annotations.AfterTest;
import org.testng.annotations.BeforeTest;
import org.testng.annotations.Test;

public class UtilsTest {
public class TimeTokenTest {

private Locale defaultLocale;

@BeforeTest
public void beforeMethod() {
defaultLocale = Locale.getDefault();
}

@Test(groups = { "internal" })
public void localeUS() {
Locale.setDefault(Locale.ITALIAN);
DateTimeFormatter RFC_1123_DATE_TIME =
DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss zzz",Locale.US);
String time=Utils.nowAsRFC1123();
Locale.setDefault(Locale.US);
RFC_1123_DATE_TIME.parse(time);
public void nonLocaleUS() {
Locale.setDefault(Locale.ITALIAN);
DateTimeFormatter RFC_1123_DATE_TIME =
DateTimeFormatter.ofPattern("EEE, dd MMM yyyy HH:mm:ss zzz", Locale.US);
String time = Utils.nowAsRFC1123();
Locale.setDefault(Locale.US);
RFC_1123_DATE_TIME.parse(time);
}

@AfterTest
public void afterMethod() {
// set back default locale before test
if (defaultLocale != null) {
Locale.setDefault(defaultLocale);
} else {
Locale.setDefault(Locale.US);
}
}
}
11 changes: 11 additions & 0 deletions sdk/src/test/resources/sampleConflict.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"id": "k6d9ALgBmD8BAAAAAAAAQA==",
"_rid": "k6d9ALgBmD8BAAAAAAAAQA==",
"_self": "dbs/k6d9AA==/colls/k6d9ALgBmD8=/conflicts/k6d9ALgBmD8BAAAAAAAAQA==/",
"_etag": "\"00004a0f-0000-0000-0000-5b6e214b0000\"",
"resourceType": "document",
"operationType": "create",
"resourceId": "k6d9ALgBmD+ChB4AAAAAAA==",
"content": "{\"id\":\"0007312a-a1c5-4b54-9e39-35de2367fa33\",\"regionId\":2,\"regionEndpoint\":\"https://test-southeastasia.documents.azure.com:443/\",\"_rid\":\"k6d9ALgBmD+ChB4AAAAAAA==\",\"_self\":\"dbs\\/k6d9AA==\\/colls\\/k6d9ALgBmD8=\\/docs\\/k6d9ALgBmD+ChB4AAAAAAA==\\/\",\"_etag\":\"\\\"00000200-0000-0000-0000-5b6e214b0000\\\"\",\"_attachments\":\"attachments\\/\",\"_ts\":1533944139}",
"_ts": 1533944139
}

0 comments on commit 0164ebe

Please sign in to comment.