Skip to content

Commit

Permalink
Remove Usage of AccessController in azure-search-documents (#24765)
Browse files Browse the repository at this point in the history
Remove Usage of AccessController in azure-search-documents
  • Loading branch information
alzimmermsft authored Oct 15, 2021
1 parent 933638c commit 3bde19a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -476,7 +476,7 @@ ModelWithPrimitiveCollections preparePrimitivesModel() {
}

String setupIndexWithDataTypes() {
SearchIndex index = new SearchIndex("data-types-tests-index")
SearchIndex index = new SearchIndex(testResourceNamer.randomName("data-types-tests-index", 64))
.setFields(Arrays.asList(
new SearchField("Key", SearchFieldDataType.STRING)
.setKey(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -972,7 +972,7 @@ void assertListEqualHotelIds(List<String> expected, List<SearchResult> actual) {
}

String createIndexWithNonNullableTypes() {
SearchIndex index = new SearchIndex("non-nullable-index")
SearchIndex index = new SearchIndex(testResourceNamer.randomName("non-nullable-index", 64))
.setFields(Arrays.asList(
new SearchField("Key", SearchFieldDataType.STRING)
.setHidden(false)
Expand Down Expand Up @@ -1008,7 +1008,7 @@ String createIndexWithNonNullableTypes() {
}

String createIndexWithValueTypes() {
SearchIndex index = new SearchIndex("testindex")
SearchIndex index = new SearchIndex(testResourceNamer.randomName("testindex", 64))
.setFields(Arrays.asList(
new SearchField("Key", SearchFieldDataType.STRING)
.setKey(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,21 +36,20 @@
import com.azure.search.documents.indexes.models.TagScoringFunction;
import com.azure.search.documents.indexes.models.TagScoringParameters;
import com.azure.search.documents.indexes.models.TextWeights;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import reactor.core.Exceptions;

import java.io.InputStreamReader;
import java.io.Reader;
import java.lang.reflect.Field;
import java.time.Duration;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;

import static com.azure.search.documents.TestHelpers.BLOB_DATASOURCE_NAME;
import static com.azure.search.documents.TestHelpers.HOTEL_INDEX_NAME;
import static com.azure.search.documents.TestHelpers.MAPPER;
import static com.azure.search.documents.TestHelpers.SQL_DATASOURCE_NAME;
import static com.azure.search.documents.indexes.DataSourceSyncTests.FAKE_AZURE_SQL_CONNECTION_STRING;

Expand Down Expand Up @@ -89,24 +88,16 @@ protected String createHotelIndex() {
}

protected String setupIndexFromJsonFile(String jsonFile) {
Reader indexData = new InputStreamReader(Objects.requireNonNull(getClass().getClassLoader()
.getResourceAsStream(jsonFile)));
try {
return setupIndex(TestHelpers.MAPPER.readValue(indexData, SearchIndex.class));
ObjectNode jsonData = (ObjectNode) MAPPER.readTree(TestHelpers.loadResource(jsonFile));
jsonData.set("name", new TextNode(testResourceNamer.randomName(jsonData.get("name").asText(), 64)));
return setupIndex(MAPPER.treeToValue(jsonData, SearchIndex.class));
} catch (Exception e) {
throw Exceptions.propagate(e);
}
}

protected String setupIndex(SearchIndex index) {
try {
Field searchIndexName = index.getClass().getDeclaredField("name");
searchIndexName.setAccessible(true);

searchIndexName.set(index, testResourceNamer.randomName(index.getName(), 64));
} catch (Exception e) {
throw Exceptions.propagate(e);
}
getSearchIndexClientBuilder().buildClient().createOrUpdateIndex(index);

return index.getName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,17 @@
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.fasterxml.jackson.databind.node.TextNode;
import org.reactivestreams.Publisher;
import reactor.core.Exceptions;
import reactor.test.StepVerifier;

import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.security.AccessController;
import java.security.PrivilegedAction;
import java.net.URI;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.time.OffsetDateTime;
import java.time.ZoneOffset;
import java.util.Arrays;
Expand All @@ -38,11 +39,11 @@
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

import static com.azure.search.documents.SearchTestBase.API_KEY;
import static com.azure.search.documents.SearchTestBase.ENDPOINT;
import static com.azure.search.documents.SearchTestBase.HOTELS_DATA_JSON;
import static com.azure.search.documents.SearchTestBase.HOTELS_TESTS_INDEX_DATA_JSON;
import static com.azure.search.documents.SearchTestBase.SERVICE_THROTTLE_SAFE_RETRY_POLICY;
import static com.azure.search.documents.implementation.util.Utility.MAP_STRING_OBJECT_TYPE_REFERENCE;
import static com.azure.search.documents.implementation.util.Utility.getDefaultSerializerAdapter;
Expand All @@ -68,6 +69,8 @@ public final class TestHelpers {
new TypeReference<List<Map<String, Object>>>() {
};

private static final Map<String, byte[]> LOADED_FILE_DATA = new ConcurrentHashMap<>();

/**
* Assert whether two objects are equal.
*
Expand Down Expand Up @@ -315,22 +318,13 @@ public static <T> T convertMapToValue(Map<String, Object> value, Class<T> clazz)
}
}

@SuppressWarnings("removal")
public static SearchIndexClient setupSharedIndex(String indexName) {
InputStream stream = Objects.requireNonNull(AutocompleteSyncTests.class
.getClassLoader()
.getResourceAsStream(HOTELS_TESTS_INDEX_DATA_JSON));

try {
SearchIndex index = MAPPER.readValue(stream, SearchIndex.class);
byte[] hotelsTestIndexDataJsonData = loadResource(indexName);
JsonNode jsonNode = MAPPER.readTree(hotelsTestIndexDataJsonData);
((ObjectNode) jsonNode).set("name", new TextNode(indexName));

Field searchIndexName = index.getClass().getDeclaredField("name");
AccessController.doPrivileged((PrivilegedAction<Object>) () -> {
searchIndexName.setAccessible(true);
return null;
});

searchIndexName.set(index, indexName);
SearchIndex index = MAPPER.treeToValue(jsonNode, SearchIndex.class);

SearchIndexClient searchIndexClient = new SearchIndexClientBuilder()
.endpoint(ENDPOINT)
Expand Down Expand Up @@ -366,4 +360,18 @@ public static String createGeographyPolygon(String... coordinates) {

return builder.append("))'").toString();
}

static byte[] loadResource(String fileName) {
return LOADED_FILE_DATA.computeIfAbsent(fileName, fName -> {
try {
URI fileUri = AutocompleteSyncTests.class.getClassLoader()
.getResource(fileName)
.toURI();

return Files.readAllBytes(Paths.get(fileUri));
} catch (Exception ex) {
throw new RuntimeException(ex);
}
});
}
}

0 comments on commit 3bde19a

Please sign in to comment.