Skip to content

Commit

Permalink
createOrUpdateSkillset (Azure#253)
Browse files Browse the repository at this point in the history
  • Loading branch information
chenmliu authored Nov 13, 2019
1 parent 2be2ce7 commit 47258a1
Show file tree
Hide file tree
Showing 9 changed files with 285 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1150,19 +1150,55 @@ Mono<PagedResponse<Skillset>> listSkillsetsWithResponse(String select,
}

/**
* @return the updated Skillset.
* @throws NotImplementedException not implemented
* Creates a new Azure Cognitive Search skillset or updates a skillset if it already exists.
*
* @param skillset the definition of the skillset to create or update
* @return the skillset that was created or updated.
*/
public Mono<Skillset> createOrUpdateSkillset() {
throw logger.logExceptionAsError(new NotImplementedException("not implemented."));
public Mono<Skillset> createOrUpdateSkillset(Skillset skillset) {
return this.createOrUpdateSkillsetWithResponse(skillset, null, null)
.map(Response::getValue);
}

/**
* @return a response containing the updated Skillset.
* @throws NotImplementedException not implemented
* Creates a new Azure Cognitive Search skillset or updates a skillset if it already exists.
*
* @param skillset the definition of the skillset to create or update
* @param requestOptions additional parameters for the operation.
* Contains the tracking ID sent with the request to help with debugging
* @return the skillset that was created or updated.
*/
public Mono<Response<Skillset>> createOrUpdateSkillsetWithResponse() {
throw logger.logExceptionAsError(new NotImplementedException("not implemented."));
public Mono<Skillset> createOrUpdateSkillset(Skillset skillset,
RequestOptions requestOptions) {
return this.createOrUpdateSkillsetWithResponse(skillset, requestOptions)
.map(Response::getValue);
}

/**
* Creates a new Azure Cognitive Search skillset or updates a skillset if it already exists.
*
* @param skillset the definition of the skillset to create or update
* @param requestOptions additional parameters for the operation.
* Contains the tracking ID sent with the request to help with debugging
* @return a response containing the skillset that was created or updated.
*/
public Mono<Response<Skillset>> createOrUpdateSkillsetWithResponse(Skillset skillset,
RequestOptions requestOptions) {
return withContext(context -> this.createOrUpdateSkillsetWithResponse(skillset,
requestOptions,
context));
}

Mono<Response<Skillset>> createOrUpdateSkillsetWithResponse(Skillset skillset,
RequestOptions requestOptions,
Context context) {
return restClient
.skillsets()
.createOrUpdateWithRestResponseAsync(skillset.getName(),
skillset,
requestOptions,
context)
.map(Function.identity());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1020,19 +1020,45 @@ public PagedResponse<Skillset> listSkillsetsWithResponse(String select,
}

/**
* @return the updated Skillset.
* @throws NotImplementedException not implemented
* Creates a new Azure Cognitive Search skillset or updates a skillset if it already exists.
*
* @param skillset the definition of the skillset to create or update
* @return the skillset that was created or updated.
*/
public Skillset createOrUpdateSkillset() {
throw logger.logExceptionAsError(new NotImplementedException("not implemented."));
public Skillset createOrUpdateSkillset(Skillset skillset) {
return asyncClient.createOrUpdateSkillset(skillset).block();
}

/**
* @return a response containing the updated Skillset.
* @throws NotImplementedException not implemented
* Creates a new Azure Cognitive Search skillset or updates a skillset if it already exists.
*
* @param skillset the definition of the skillset to create or update
* @param requestOptions additional parameters for the operation.
* Contains the tracking ID sent with the request to help with debugging
* @return the skillset that was created or updated.
*/
public Response<Skillset> createOrUpdateSkillsetWithResponse() {
throw logger.logExceptionAsError(new NotImplementedException("not implemented."));
public Skillset createOrUpdateSkillset(Skillset skillset,
RequestOptions requestOptions) {


return asyncClient.createOrUpdateSkillset(skillset, requestOptions).block();
}

/**
* Creates a new Azure Cognitive Search skillset or updates a skillset if it already exists.
*
* @param skillset the definition of the skillset to create or update
* @param requestOptions additional parameters for the operation.
* Contains the tracking ID sent with the request to help with debugging
* @param context additional context that is passed through the HTTP pipeline during the service call
* @return a response containing the skillset that was created or updated.
*/
public Response<Skillset> createOrUpdateSkillsetWithResponse(Skillset skillset,
RequestOptions requestOptions,
Context context) {
return asyncClient.createOrUpdateSkillsetWithResponse(skillset,
requestOptions,
context).block();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package com.azure.search;

import com.azure.core.exception.HttpResponseException;

import com.azure.core.http.rest.PagedFlux;
import com.azure.search.models.EntityCategory;
import com.azure.search.models.KeyPhraseExtractionSkillLanguage;
Expand All @@ -20,6 +19,7 @@

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

public class SkillsetManagementAsyncTests extends SkillsetManagementTestBase {
private SearchServiceAsyncClient client;
Expand Down Expand Up @@ -351,6 +351,36 @@ public void deleteSkillsetIsIdempotent() {
.verifyComplete();
}

@Override
public void createOrUpdateCreatesWhenSkillsetDoesNotExist() {
Skillset skillset = createTestOcrSkillSet(1, TextExtractionAlgorithm.PRINTED, false);

RequestOptions requestOptions = new RequestOptions()
.setClientRequestId(UUID.randomUUID());

StepVerifier
.create(client.createOrUpdateSkillsetWithResponse(skillset, requestOptions))
.assertNext(res -> Assert.assertEquals(HttpResponseStatus.CREATED.code(), res.getStatusCode()))
.verifyComplete();
}

@Override
public void createOrUpdateUpdatesWhenSkillsetExists() {
Skillset skillset = createTestOcrSkillSet(1, TextExtractionAlgorithm.HANDWRITTEN, false);
RequestOptions requestOptions = new RequestOptions()
.setClientRequestId(UUID.randomUUID());
StepVerifier
.create(client.createOrUpdateSkillsetWithResponse(skillset, requestOptions))
.assertNext(res -> Assert.assertEquals(HttpResponseStatus.CREATED.code(), res.getStatusCode()))
.verifyComplete();

skillset = createTestOcrSkillSet(2, TextExtractionAlgorithm.PRINTED, false);
StepVerifier
.create(client.createOrUpdateSkillsetWithResponse(skillset, requestOptions))
.assertNext(res -> Assert.assertEquals(HttpResponseStatus.OK.code(), res.getStatusCode()))
.verifyComplete();
}

@Override
public void existsReturnsFalseForNonExistingSkillset() {
StepVerifier
Expand All @@ -362,6 +392,7 @@ public void existsReturnsFalseForNonExistingSkillset() {
@Override
public void existsReturnsTrueForExistingSkillset() {
Skillset skillset = createSkillsetWithOcrDefaultSettings(false);

client.createSkillset(skillset).block();

StepVerifier
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import com.azure.core.exception.HttpResponseException;
import com.azure.core.http.rest.PagedIterable;
import com.azure.core.http.rest.Response;
import com.azure.core.util.Context;
import com.azure.search.models.EntityCategory;
import com.azure.search.models.KeyPhraseExtractionSkillLanguage;
import com.azure.search.models.OcrSkillLanguage;
Expand All @@ -19,6 +20,7 @@

import java.util.Arrays;
import java.util.List;
import java.util.UUID;
import java.util.stream.Collectors;

public class SkillsetManagementSyncTests extends SkillsetManagementTestBase {
Expand Down Expand Up @@ -288,6 +290,37 @@ public void deleteSkillsetIsIdempotent() {
Assert.assertEquals(HttpResponseStatus.NOT_FOUND.code(), deleteResponse.getStatusCode());
}

@Override
public void createOrUpdateCreatesWhenSkillsetDoesNotExist() {
Skillset skillset = createTestOcrSkillSet(1, TextExtractionAlgorithm.HANDWRITTEN, false);

RequestOptions requestOptions = new RequestOptions()
.setClientRequestId(UUID.randomUUID());

Response<Skillset> createOrUpdateResponse = client.createOrUpdateSkillsetWithResponse(skillset,
requestOptions,
Context.NONE);

Assert.assertEquals(HttpResponseStatus.CREATED.code(), createOrUpdateResponse.getStatusCode());
}

@Override
public void createOrUpdateUpdatesWhenSkillsetExists() {
Skillset skillset = createTestOcrSkillSet(1, TextExtractionAlgorithm.HANDWRITTEN, false);
RequestOptions requestOptions = new RequestOptions()
.setClientRequestId(UUID.randomUUID());
Response<Skillset> createOrUpdateResponse = client.createOrUpdateSkillsetWithResponse(skillset,
requestOptions,
Context.NONE);
Assert.assertEquals(HttpResponseStatus.CREATED.code(), createOrUpdateResponse.getStatusCode());

skillset = createTestOcrSkillSet(2, TextExtractionAlgorithm.PRINTED, false);
createOrUpdateResponse = client.createOrUpdateSkillsetWithResponse(skillset,
requestOptions,
Context.NONE);
Assert.assertEquals(HttpResponseStatus.OK.code(), createOrUpdateResponse.getStatusCode());
}

@Override
public void existsReturnsFalseForNonExistingSkillset() {
Assert.assertFalse(client.skillsetExists("nonexistent"));
Expand All @@ -296,6 +329,7 @@ public void existsReturnsFalseForNonExistingSkillset() {
@Override
public void existsReturnsTrueForExistingSkillset() {
Skillset skillset = createSkillsetWithOcrDefaultSettings(false);

client.createSkillset(skillset);

Assert.assertTrue(client.skillsetExists(skillset.getName()));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,18 @@ public abstract class SkillsetManagementTestBase extends SearchServiceTestBase {
@Test
public abstract void deleteSkillsetIsIdempotent();

@Test
public abstract void createOrUpdateCreatesWhenSkillsetDoesNotExist();

@Test
public abstract void createOrUpdateUpdatesWhenSkillsetExists();

@Test
public abstract void existsReturnsFalseForNonExistingSkillset();

@Test
public abstract void existsReturnsTrueForExistingSkillset();


protected void assertSkillsetsEqual(Skillset expected, Skillset actual) {
expected.setETag("none");
actual.setETag("none");
Expand Down Expand Up @@ -502,6 +507,41 @@ protected Skillset createTestSkillsetOcrSplitText(OcrSkillLanguage ocrLanguageCo
.setSkills(skills);
}

protected Skillset createTestOcrSkillSet(int repeat, TextExtractionAlgorithm algorithm, boolean shouldDetectOrientation) {
List<Skill> skills = new ArrayList<>();

List<InputFieldMappingEntry> inputs = Arrays.asList(
new InputFieldMappingEntry()
.setName("url")
.setSource("/document/url"),
new InputFieldMappingEntry().setName("queryString")
.setSource("/document/queryString")
);

for (int i = 0; i < repeat; i++) {
List<OutputFieldMappingEntry> outputs = Collections.singletonList(
new OutputFieldMappingEntry()
.setName("text")
.setTargetName("mytext" + i)
);

skills.add(new OcrSkill()
.setDefaultLanguageCode(OcrSkillLanguage.EN)
.setTextExtractionAlgorithm(algorithm)
.setShouldDetectOrientation(shouldDetectOrientation)
.setName("myocr-" + i)
.setDescription("Tested OCR skill")
.setContext(CONTEXT_VALUE)
.setInputs(inputs)
.setOutputs(outputs));
}

return new Skillset()
.setName("testskillset")
.setDescription("Skillset for testing OCR")
.setSkills(skills);
}

protected Skillset createSkillsetWithOcrDefaultSettings(Boolean shouldDetectOrientation) {
List<InputFieldMappingEntry> inputs = Arrays.asList(
new InputFieldMappingEntry()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"networkCallRecords" : [ {
"Method" : "PUT",
"Uri" : "https://azs-sdka95496911b77.search.windows.net/skillsets('testskillset')?api-version=2019-05-06",
"Headers" : {
"Content-Type" : "application/json; charset=utf-8"
},
"Response" : {
"Pragma" : "no-cache",
"retry-after" : "0",
"request-id" : "69f339f7-aac2-49f1-8c9a-5187e9c30aee",
"StatusCode" : "201",
"Date" : "Tue, 12 Nov 2019 19:47:28 GMT",
"Strict-Transport-Security" : "max-age=15724800; includeSubDomains",
"Cache-Control" : "no-cache",
"ETag" : "W/\"0x8D767A9264EB301\"",
"elapsed-time" : "68",
"OData-Version" : "4.0",
"Expires" : "-1",
"Content-Length" : "691",
"Body" : "{\"@odata.context\":\"https://azs-sdka95496911b77.search.windows.net/$metadata#skillsets/$entity\",\"@odata.etag\":\"\\\"0x8D767A9264EB301\\\"\",\"name\":\"testskillset\",\"description\":\"Skillset for testing OCR\",\"skills\":[{\"@odata.type\":\"#Microsoft.Skills.Vision.OcrSkill\",\"name\":\"myocr-0\",\"description\":\"Tested OCR skill\",\"context\":\"/document\",\"textExtractionAlgorithm\":\"handwritten\",\"lineEnding\":null,\"defaultLanguageCode\":\"en\",\"detectOrientation\":false,\"inputs\":[{\"name\":\"url\",\"source\":\"/document/url\",\"sourceContext\":null,\"inputs\":[]},{\"name\":\"queryString\",\"source\":\"/document/queryString\",\"sourceContext\":null,\"inputs\":[]}],\"outputs\":[{\"name\":\"text\",\"targetName\":\"mytext0\"}]}],\"cognitiveServices\":null}",
"Preference-Applied" : "odata.include-annotations=\"*\"",
"Content-Type" : "application/json; odata.metadata=minimal",
"Location" : "https://azs-sdka95496911b77.search.windows.net/skillsets('testskillset')?api-version=2019-05-06"
},
"Exception" : null
} ],
"variables" : [ ]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
{
"networkCallRecords" : [ {
"Method" : "PUT",
"Uri" : "https://azs-sdk34b47195c38d.search.windows.net/skillsets('testskillset')?api-version=2019-05-06",
"Headers" : {
"Content-Type" : "application/json; charset=utf-8"
},
"Response" : {
"Pragma" : "no-cache",
"retry-after" : "0",
"request-id" : "c0dce938-945b-4e4c-a3dd-50c0fc0b5e9e",
"StatusCode" : "201",
"Date" : "Tue, 12 Nov 2019 19:47:49 GMT",
"Strict-Transport-Security" : "max-age=15724800; includeSubDomains",
"Cache-Control" : "no-cache",
"ETag" : "W/\"0x8D767A9328E4E43\"",
"elapsed-time" : "64",
"OData-Version" : "4.0",
"Expires" : "-1",
"Content-Length" : "691",
"Body" : "{\"@odata.context\":\"https://azs-sdk34b47195c38d.search.windows.net/$metadata#skillsets/$entity\",\"@odata.etag\":\"\\\"0x8D767A9328E4E43\\\"\",\"name\":\"testskillset\",\"description\":\"Skillset for testing OCR\",\"skills\":[{\"@odata.type\":\"#Microsoft.Skills.Vision.OcrSkill\",\"name\":\"myocr-0\",\"description\":\"Tested OCR skill\",\"context\":\"/document\",\"textExtractionAlgorithm\":\"handwritten\",\"lineEnding\":null,\"defaultLanguageCode\":\"en\",\"detectOrientation\":false,\"inputs\":[{\"name\":\"url\",\"source\":\"/document/url\",\"sourceContext\":null,\"inputs\":[]},{\"name\":\"queryString\",\"source\":\"/document/queryString\",\"sourceContext\":null,\"inputs\":[]}],\"outputs\":[{\"name\":\"text\",\"targetName\":\"mytext0\"}]}],\"cognitiveServices\":null}",
"Preference-Applied" : "odata.include-annotations=\"*\"",
"Content-Type" : "application/json; odata.metadata=minimal",
"Location" : "https://azs-sdk34b47195c38d.search.windows.net/skillsets('testskillset')?api-version=2019-05-06"
},
"Exception" : null
}, {
"Method" : "PUT",
"Uri" : "https://azs-sdk34b47195c38d.search.windows.net/skillsets('testskillset')?api-version=2019-05-06",
"Headers" : {
"Content-Type" : "application/json; charset=utf-8"
},
"Response" : {
"Pragma" : "no-cache",
"retry-after" : "0",
"request-id" : "c0dce938-945b-4e4c-a3dd-50c0fc0b5e9e",
"StatusCode" : "200",
"Date" : "Tue, 12 Nov 2019 19:47:49 GMT",
"Strict-Transport-Security" : "max-age=15724800; includeSubDomains",
"Cache-Control" : "no-cache",
"ETag" : "W/\"0x8D767A932C34A86\"",
"elapsed-time" : "58",
"OData-Version" : "4.0",
"Expires" : "-1",
"Content-Length" : "1142",
"Body" : "{\"@odata.context\":\"https://azs-sdk34b47195c38d.search.windows.net/$metadata#skillsets/$entity\",\"@odata.etag\":\"\\\"0x8D767A932C34A86\\\"\",\"name\":\"testskillset\",\"description\":\"Skillset for testing OCR\",\"skills\":[{\"@odata.type\":\"#Microsoft.Skills.Vision.OcrSkill\",\"name\":\"myocr-0\",\"description\":\"Tested OCR skill\",\"context\":\"/document\",\"textExtractionAlgorithm\":\"printed\",\"lineEnding\":null,\"defaultLanguageCode\":\"en\",\"detectOrientation\":false,\"inputs\":[{\"name\":\"url\",\"source\":\"/document/url\",\"sourceContext\":null,\"inputs\":[]},{\"name\":\"queryString\",\"source\":\"/document/queryString\",\"sourceContext\":null,\"inputs\":[]}],\"outputs\":[{\"name\":\"text\",\"targetName\":\"mytext0\"}]},{\"@odata.type\":\"#Microsoft.Skills.Vision.OcrSkill\",\"name\":\"myocr-1\",\"description\":\"Tested OCR skill\",\"context\":\"/document\",\"textExtractionAlgorithm\":\"printed\",\"lineEnding\":null,\"defaultLanguageCode\":\"en\",\"detectOrientation\":false,\"inputs\":[{\"name\":\"url\",\"source\":\"/document/url\",\"sourceContext\":null,\"inputs\":[]},{\"name\":\"queryString\",\"source\":\"/document/queryString\",\"sourceContext\":null,\"inputs\":[]}],\"outputs\":[{\"name\":\"text\",\"targetName\":\"mytext1\"}]}],\"cognitiveServices\":null}",
"Preference-Applied" : "odata.include-annotations=\"*\"",
"Content-Type" : "application/json; odata.metadata=minimal"
},
"Exception" : null
} ],
"variables" : [ ]
}
Loading

0 comments on commit 47258a1

Please sign in to comment.