Merge pull request elastic/elasticsearch#713 from elastic/immutable_set_be_gone

Remove ImmutableSet

Original commit: elastic/x-pack-elasticsearch@fe06937bc7
This commit is contained in:
Nik Everett 2015-09-25 21:46:22 -04:00
commit bee764b9ee
24 changed files with 350 additions and 208 deletions

View File

@ -6,7 +6,7 @@
package org.elasticsearch.shield.audit.index;
import com.google.common.base.Splitter;
import com.google.common.collect.ImmutableSet;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.action.admin.cluster.state.ClusterStateResponse;
import org.elasticsearch.action.admin.indices.exists.indices.IndicesExistsRequest;
@ -66,7 +66,11 @@ import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import java.util.*;
import java.util.ArrayList;
import java.util.EnumSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.Lock;
@ -74,7 +78,17 @@ import java.util.concurrent.locks.ReentrantLock;
import static org.elasticsearch.shield.audit.AuditUtil.indices;
import static org.elasticsearch.shield.audit.AuditUtil.restRequestContent;
import static org.elasticsearch.shield.audit.index.IndexAuditLevel.*;
import static org.elasticsearch.shield.audit.index.IndexAuditLevel.ACCESS_DENIED;
import static org.elasticsearch.shield.audit.index.IndexAuditLevel.ACCESS_GRANTED;
import static org.elasticsearch.shield.audit.index.IndexAuditLevel.ANONYMOUS_ACCESS_DENIED;
import static org.elasticsearch.shield.audit.index.IndexAuditLevel.AUTHENTICATION_FAILED;
import static org.elasticsearch.shield.audit.index.IndexAuditLevel.CONNECTION_DENIED;
import static org.elasticsearch.shield.audit.index.IndexAuditLevel.CONNECTION_GRANTED;
import static org.elasticsearch.shield.audit.index.IndexAuditLevel.RUN_AS_DENIED;
import static org.elasticsearch.shield.audit.index.IndexAuditLevel.RUN_AS_GRANTED;
import static org.elasticsearch.shield.audit.index.IndexAuditLevel.SYSTEM_ACCESS_GRANTED;
import static org.elasticsearch.shield.audit.index.IndexAuditLevel.TAMPERED_REQUEST;
import static org.elasticsearch.shield.audit.index.IndexAuditLevel.parse;
import static org.elasticsearch.shield.audit.index.IndexNameResolver.resolve;
/**
@ -107,7 +121,7 @@ public class IndexAuditTrail extends AbstractComponent implements AuditTrail, Cl
RUN_AS_GRANTED.toString()
};
private static final ImmutableSet<String> forbiddenIndexSettings = ImmutableSet.of("index.mapper.dynamic");
private static final String FORBIDDEN_INDEX_SETTING = "index.mapper.dynamic";
private final AtomicReference<State> state = new AtomicReference<>(State.INITIALIZED);
private final String nodeName;
@ -725,7 +739,7 @@ public class IndexAuditTrail extends AbstractComponent implements AuditTrail, Cl
Settings.Builder builder = Settings.builder();
for (Map.Entry<String, String> entry : newSettings.getAsMap().entrySet()) {
String name = "index." + entry.getKey();
if (forbiddenIndexSettings.contains(name)) {
if (FORBIDDEN_INDEX_SETTING.equals(name)) {
logger.warn("overriding the default [{}} setting is forbidden. ignoring...", name);
continue;
}

View File

@ -5,7 +5,6 @@
*/
package org.elasticsearch.shield.authc.esusers.tool;
import com.google.common.collect.ImmutableSet;
import org.apache.commons.cli.CommandLine;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.cli.CheckFileCommand;
@ -27,7 +26,14 @@ import org.elasticsearch.shield.support.Validation;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
@ -431,7 +437,7 @@ public class ESUsersTool extends CliTool {
@Override
public ExitStatus execute(Settings settings, Environment env) throws Exception {
Settings esusersSettings = Realms.internalRealmSettings(settings, ESUsersRealm.TYPE);
ImmutableSet<String> knownRoles = loadRoleNames(terminal, settings, env);
Set<String> knownRoles = loadRoleNames(terminal, settings, env);
Path userRolesFilePath = FileUserRolesStore.resolveFile(esusersSettings, env);
Map<String, String[]> userRoles = FileUserRolesStore.parseFile(userRolesFilePath, null);
Path userFilePath = FileUserPasswdStore.resolveFile(esusersSettings, env);
@ -493,7 +499,7 @@ public class ESUsersTool extends CliTool {
}
}
private static ImmutableSet<String> loadRoleNames(Terminal terminal, Settings settings, Environment env) {
private static Set<String> loadRoleNames(Terminal terminal, Settings settings, Environment env) {
Path rolesFile = FileRolesStore.resolveFile(settings, env);
try {
return FileRolesStore.parseFileForRoleNames(rolesFile, null);
@ -520,7 +526,7 @@ public class ESUsersTool extends CliTool {
}
private static void verifyRoles(Terminal terminal, Settings settings, Environment env, String[] roles) {
ImmutableSet<String> knownRoles = loadRoleNames(terminal, settings, env);
Set<String> knownRoles = loadRoleNames(terminal, settings, env);
Set<String> unknownRoles = Sets.difference(Sets.newHashSet(roles), knownRoles);
if (!unknownRoles.isEmpty()) {
Path rolesFile = FileRolesStore.resolveFile(settings, env);

View File

@ -6,7 +6,7 @@
package org.elasticsearch.shield.authz;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import org.elasticsearch.cluster.metadata.AliasOrIndex;
import org.elasticsearch.cluster.metadata.IndexMetaData;
import org.elasticsearch.cluster.metadata.MetaData;
@ -30,6 +30,8 @@ import java.util.concurrent.ConcurrentHashMap;
import java.util.function.Function;
import java.util.function.Predicate;
import static java.util.Collections.unmodifiableSet;
/**
* Represents a permission in the system. There are 3 types of permissions:
*
@ -235,6 +237,7 @@ public interface Permission {
return privilege;
}
@Override
public boolean check(String action) {
return predicate.test(action);
}
@ -346,8 +349,8 @@ public interface Permission {
// by at least one indices permission group
SortedMap<String, AliasOrIndex> allAliasesAndIndices = metaData.getAliasAndIndexLookup();
Map<String, ImmutableSet.Builder<String>> fieldsBuilder = new HashMap<>();
Map<String, ImmutableSet.Builder<BytesReference>> queryBuilder = new HashMap<>();
Map<String, Set<String>> rolesFieldsByIndex = new HashMap<>();
Map<String, Set<BytesReference>> roleQueriesByIndex = new HashMap<>();
Map<String, Boolean> grantedBuilder = new HashMap<>();
for (String indexOrAlias : requestedIndicesOrAliases) {
@ -365,20 +368,20 @@ public interface Permission {
granted = true;
for (String index : concreteIndices) {
if (group.getFields() != null) {
ImmutableSet.Builder<String> roleFieldsBuilder = fieldsBuilder.get(index);
if (roleFieldsBuilder == null) {
roleFieldsBuilder = ImmutableSet.builder();
fieldsBuilder.put(index, roleFieldsBuilder);
Set<String> roleFields = rolesFieldsByIndex.get(index);
if (roleFields == null) {
roleFields = new HashSet<>();
rolesFieldsByIndex.put(index, roleFields);
}
roleFieldsBuilder.addAll(group.getFields());
roleFields.addAll(group.getFields());
}
if (group.getQuery() != null) {
ImmutableSet.Builder<BytesReference> roleQueriesBuilder = queryBuilder.get(index);
if (roleQueriesBuilder == null) {
roleQueriesBuilder = ImmutableSet.builder();
queryBuilder.put(index, roleQueriesBuilder);
Set<BytesReference> roleQueries = roleQueriesByIndex.get(index);
if (roleQueries == null) {
roleQueries = new HashSet<>();
roleQueriesByIndex.put(index, roleQueries);
}
roleQueriesBuilder.add(group.getQuery());
roleQueries.add(group.getQuery());
}
}
}
@ -396,19 +399,13 @@ public interface Permission {
ImmutableMap.Builder<String, IndicesAccessControl.IndexAccessControl> indexPermissions = ImmutableMap.builder();
for (Map.Entry<String, Boolean> entry : grantedBuilder.entrySet()) {
String index = entry.getKey();
ImmutableSet.Builder<BytesReference> roleQueriesBuilder = queryBuilder.get(index);
ImmutableSet.Builder<String> roleFieldsBuilder = fieldsBuilder.get(index);
final ImmutableSet<String> roleFields;
if (roleFieldsBuilder != null) {
roleFields = roleFieldsBuilder.build();
} else {
roleFields = null;
Set<BytesReference> roleQueries = roleQueriesByIndex.get(index);
if (roleQueries != null) {
roleQueries = unmodifiableSet(roleQueries);
}
final ImmutableSet<BytesReference> roleQueries;
if (roleQueriesBuilder != null) {
roleQueries = roleQueriesBuilder.build();
} else {
roleQueries = null;
Set<String> roleFields = rolesFieldsByIndex.get(index);
if (roleFields != null) {
roleFields = unmodifiableSet(roleFields);
}
indexPermissions.put(index, new IndicesAccessControl.IndexAccessControl(entry.getValue(), roleFields, roleQueries));
}

View File

@ -5,11 +5,10 @@
*/
package org.elasticsearch.shield.authz;
import com.google.common.collect.ImmutableSet;
import com.google.common.util.concurrent.UncheckedExecutionException;
import dk.brics.automaton.Automaton;
import dk.brics.automaton.BasicAutomata;
import dk.brics.automaton.BasicOperations;
import org.elasticsearch.action.admin.indices.create.CreateIndexAction;
import org.elasticsearch.action.get.GetAction;
import org.elasticsearch.action.get.MultiGetAction;
@ -21,12 +20,16 @@ import org.elasticsearch.common.util.set.Sets;
import org.elasticsearch.shield.support.AutomatonPredicate;
import org.elasticsearch.shield.support.Automatons;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArraySet;
import java.util.function.Predicate;
import static java.util.Collections.singleton;
import static java.util.Collections.unmodifiableSet;
import static org.elasticsearch.common.util.set.Sets.newHashSet;
import static org.elasticsearch.shield.support.Automatons.patterns;
/**
@ -401,6 +404,7 @@ public abstract class Privilege<P extends Privilege<P>> {
return BasicOperations.subsetOf(other.automaton, automaton);
}
@Override
public String toString() {
return name.toString();
}
@ -417,20 +421,20 @@ public abstract class Privilege<P extends Privilege<P>> {
public static final Name NONE = new Name("none");
public static final Name ALL = new Name("all");
private final ImmutableSet<String> parts;
private final Set<String> parts;
public Name(String name) {
assert name != null && !name.contains(",");
parts = ImmutableSet.of(name);
parts = singleton(name);
}
public Name(Set<String> parts) {
assert !parts.isEmpty();
this.parts = ImmutableSet.copyOf(parts);
this.parts = unmodifiableSet(new HashSet<>(parts));
}
public Name(String... parts) {
this(ImmutableSet.copyOf(parts));
this(unmodifiableSet(newHashSet(parts)));
}
@Override

View File

@ -5,8 +5,6 @@
*/
package org.elasticsearch.shield.authz.accesscontrol;
import com.google.common.collect.ImmutableSet;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.bytes.BytesReference;
@ -15,6 +13,8 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import static java.util.Collections.unmodifiableSet;
/**
* Encapsulates the field and document permissions per concrete index based on the current request.
*/
@ -52,10 +52,10 @@ public class IndicesAccessControl {
public static class IndexAccessControl {
private final boolean granted;
private final ImmutableSet<String> fields;
private final ImmutableSet<BytesReference> queries;
private final Set<String> fields;
private final Set<BytesReference> queries;
public IndexAccessControl(boolean granted, ImmutableSet<String> fields, ImmutableSet<BytesReference> queries) {
public IndexAccessControl(boolean granted, Set<String> fields, Set<BytesReference> queries) {
this.granted = granted;
this.fields = fields;
this.queries = queries;
@ -94,27 +94,27 @@ public class IndicesAccessControl {
// this code is a bit of a pita, but right now we can't just initialize an empty set,
// because an empty Set means no permissions on fields and
// <code>null</code> means no field level security
ImmutableSet<String> fields = null;
Set<String> fields = null;
if (this.fields != null || other.getFields() != null) {
Set<String> _fields = new HashSet<>();
fields = new HashSet<>();
if (this.fields != null) {
_fields.addAll(this.fields);
fields.addAll(this.fields);
}
if (other.getFields() != null) {
_fields.addAll(other.getFields());
fields.addAll(other.getFields());
}
fields = ImmutableSet.copyOf(_fields);
fields = unmodifiableSet(fields);
}
ImmutableSet<BytesReference> queries = null;
Set<BytesReference> queries = null;
if (this.queries != null || other.getQueries() != null) {
Set<BytesReference> _queries = new HashSet<>();
queries = new HashSet<>();
if (this.queries != null) {
_queries.addAll(this.queries);
queries.addAll(this.queries);
}
if (other.getQueries() != null) {
_queries.addAll(other.getQueries());
queries.addAll(other.getQueries());
}
queries = ImmutableSet.copyOf(_queries);
queries = unmodifiableSet(queries);
}
return new IndexAccessControl(granted, fields, queries);
}

View File

@ -6,8 +6,7 @@
package org.elasticsearch.shield.authz.store;
import com.fasterxml.jackson.dataformat.yaml.snakeyaml.error.YAMLException;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import org.elasticsearch.ElasticsearchException;
import org.elasticsearch.common.Strings;
import org.elasticsearch.common.bytes.BytesArray;
@ -37,9 +36,21 @@ import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;
import static java.util.Collections.emptyMap;
import static java.util.Collections.emptySet;
import static java.util.Collections.unmodifiableMap;
import static java.util.Collections.unmodifiableSet;
/**
*
*/
@ -51,10 +62,10 @@ public class FileRolesStore extends AbstractLifecycleComponent<RolesStore> imple
private final Path file;
private final RefreshListener listener;
private final ImmutableSet<Permission.Global.Role> reservedRoles;
private final Set<Permission.Global.Role> reservedRoles;
private final ResourceWatcherService watcherService;
private volatile ImmutableMap<String, Permission.Global.Role> permissions;
private volatile Map<String, Permission.Global.Role> permissions;
@Inject
public FileRolesStore(Settings settings, Environment env, ResourceWatcherService watcherService, Set<Permission.Global.Role> reservedRoles) {
@ -66,8 +77,8 @@ public class FileRolesStore extends AbstractLifecycleComponent<RolesStore> imple
this.file = resolveFile(settings, env);
this.listener = listener;
this.watcherService = watcherService;
this.reservedRoles = ImmutableSet.copyOf(reservedRoles);
permissions = ImmutableMap.of();
this.reservedRoles = unmodifiableSet(new HashSet<>(reservedRoles));
permissions = emptyMap();
}
@Override
@ -104,19 +115,19 @@ public class FileRolesStore extends AbstractLifecycleComponent<RolesStore> imple
return env.binFile().getParent().resolve(location);
}
public static ImmutableSet<String> parseFileForRoleNames(Path path, ESLogger logger) {
ImmutableMap<String, Permission.Global.Role> roleMap = parseFile(path, Collections.<Permission.Global.Role>emptySet(), logger, false);
public static Set<String> parseFileForRoleNames(Path path, ESLogger logger) {
Map<String, Permission.Global.Role> roleMap = parseFile(path, Collections.<Permission.Global.Role>emptySet(), logger, false);
if (roleMap == null) {
return ImmutableSet.<String>builder().build();
return emptySet();
}
return roleMap.keySet();
}
public static ImmutableMap<String, Permission.Global.Role> parseFile(Path path, Set<Permission.Global.Role> reservedRoles, ESLogger logger) {
public static Map<String, Permission.Global.Role> parseFile(Path path, Set<Permission.Global.Role> reservedRoles, ESLogger logger) {
return parseFile(path, reservedRoles, logger, true);
}
public static ImmutableMap<String, Permission.Global.Role> parseFile(Path path, Set<Permission.Global.Role> reservedRoles, ESLogger logger, boolean resolvePermission) {
public static Map<String, Permission.Global.Role> parseFile(Path path, Set<Permission.Global.Role> reservedRoles, ESLogger logger, boolean resolvePermission) {
if (logger == null) {
logger = NoOpLogger.INSTANCE;
}
@ -150,7 +161,7 @@ public class FileRolesStore extends AbstractLifecycleComponent<RolesStore> imple
roles.put(reservedRole.name(), reservedRole);
}
return ImmutableMap.copyOf(roles);
return unmodifiableMap(roles);
}
private static Permission.Global.Role parseRole(String segment, Path path, ESLogger logger, boolean resolvePermissions) {

View File

@ -5,7 +5,6 @@
*/
package org.elasticsearch.shield.audit;
import com.google.common.collect.ImmutableSet;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.rest.RestRequest;
import org.elasticsearch.shield.User;
@ -18,8 +17,10 @@ import org.junit.Before;
import org.junit.Test;
import java.net.InetAddress;
import java.util.HashSet;
import java.util.Set;
import static java.util.Collections.unmodifiableSet;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
@ -37,11 +38,11 @@ public class AuditTrailServiceTests extends ESTestCase {
@Before
public void init() throws Exception {
ImmutableSet.Builder<AuditTrail> builder = ImmutableSet.builder();
Set<AuditTrail> auditTrailsBuilder = new HashSet<>();
for (int i = 0; i < randomIntBetween(1, 4); i++) {
builder.add(mock(AuditTrail.class));
auditTrailsBuilder.add(mock(AuditTrail.class));
}
auditTrails = builder.build();
auditTrails = unmodifiableSet(auditTrailsBuilder);
service = new AuditTrailService(Settings.EMPTY, auditTrails);
token = mock(AuthenticationToken.class);
message = mock(TransportMessage.class);

View File

@ -5,7 +5,6 @@
*/
package org.elasticsearch.shield.authc.esusers;
import com.google.common.collect.ImmutableSet;
import org.elasticsearch.action.Action;
import org.elasticsearch.action.ActionListener;
import org.elasticsearch.action.ActionRequest;
@ -16,7 +15,6 @@ import org.elasticsearch.client.ClusterAdminClient;
import org.elasticsearch.client.IndicesAdminClient;
import org.elasticsearch.client.support.Headers;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.elasticsearch.rest.BaseRestHandler;
import org.elasticsearch.rest.RestChannel;
import org.elasticsearch.rest.RestController;
@ -29,14 +27,24 @@ import org.elasticsearch.shield.authc.support.UsernamePasswordToken;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.transport.TransportRequest;
import org.elasticsearch.watcher.ResourceWatcherService;
import org.hamcrest.Matchers;
import org.junit.Before;
import org.junit.Test;
import java.util.Locale;
import static org.hamcrest.Matchers.*;
import static org.mockito.Mockito.*;
import static java.util.Collections.emptySet;
import static org.hamcrest.Matchers.arrayContaining;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.sameInstance;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
/**
*
@ -184,7 +192,7 @@ public class ESUsersRealmTests extends ESTestCase {
RestController restController = mock(RestController.class);
RealmConfig config = new RealmConfig("esusers-test", Settings.EMPTY, globalSettings);
new ESUsersRealm(config, new UserPasswdStore(config), new UserRolesStore(config));
when(restController.relevantHeaders()).thenReturn(ImmutableSet.<String>of());
when(restController.relevantHeaders()).thenReturn(emptySet());
when(client.admin()).thenReturn(adminClient);
when(client.settings()).thenReturn(Settings.EMPTY);
when(client.headers()).thenReturn(Headers.EMPTY);

View File

@ -6,13 +6,24 @@
package org.elasticsearch.shield.authz.accesscontrol;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.NoMergePolicy;
import org.apache.lucene.index.Term;
import org.apache.lucene.search.DocIdSetIterator;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.Query;
import org.apache.lucene.search.QueryCachingPolicy;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.TotalHitCountCollector;
import org.apache.lucene.search.Weight;
import org.apache.lucene.search.join.BitSetProducer;
import org.apache.lucene.store.Directory;
import org.apache.lucene.util.BitSet;
@ -22,16 +33,13 @@ import org.elasticsearch.common.bytes.BytesArray;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.lucene.index.ElasticsearchDirectoryReader;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.index.IndexService;
import org.elasticsearch.index.cache.bitset.BitsetFilterCache;
import org.elasticsearch.index.engine.EngineConfig;
import org.elasticsearch.index.mapper.DocumentMapper;
import org.elasticsearch.index.mapper.MapperService;
import org.elasticsearch.index.query.IndexQueryParserService;
import org.elasticsearch.index.query.ParsedQuery;
import org.elasticsearch.index.shard.ShardId;
import org.elasticsearch.indices.IndicesLifecycle;
import org.elasticsearch.indices.IndicesWarmer;
import org.elasticsearch.shield.authz.InternalAuthorizationService;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.transport.TransportRequest;
@ -39,12 +47,14 @@ import org.mockito.Matchers;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import static java.util.Collections.singleton;
import static org.hamcrest.Matchers.equalTo;
import static org.mockito.Matchers.*;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ -65,7 +75,7 @@ public class ShieldIndexSearcherWrapperIntegrationTests extends ESTestCase {
TransportRequest request = new TransportRequest.Empty();
RequestContext.setCurrent(new RequestContext(request));
IndicesAccessControl.IndexAccessControl indexAccessControl = new IndicesAccessControl.IndexAccessControl(true, null, ImmutableSet.of(new BytesArray("{}")));
IndicesAccessControl.IndexAccessControl indexAccessControl = new IndicesAccessControl.IndexAccessControl(true, null, singleton(new BytesArray("{}")));
request.putInContext(InternalAuthorizationService.INDICES_PERMISSIONS_KEY, new IndicesAccessControl(true, ImmutableMap.of("_index", indexAccessControl)));
IndexQueryParserService parserService = mock(IndexQueryParserService.class);

View File

@ -6,13 +6,26 @@
package org.elasticsearch.shield.authz.accesscontrol;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.document.Document;
import org.apache.lucene.document.Field;
import org.apache.lucene.document.StringField;
import org.apache.lucene.index.*;
import org.apache.lucene.search.*;
import org.apache.lucene.index.DirectoryReader;
import org.apache.lucene.index.IndexWriter;
import org.apache.lucene.index.IndexWriterConfig;
import org.apache.lucene.index.LeafReaderContext;
import org.apache.lucene.index.NoMergePolicy;
import org.apache.lucene.index.PostingsEnum;
import org.apache.lucene.index.Term;
import org.apache.lucene.index.TermsEnum;
import org.apache.lucene.search.IndexSearcher;
import org.apache.lucene.search.LeafCollector;
import org.apache.lucene.search.MatchAllDocsQuery;
import org.apache.lucene.search.QueryCachingPolicy;
import org.apache.lucene.search.Scorer;
import org.apache.lucene.search.TermQuery;
import org.apache.lucene.search.Weight;
import org.apache.lucene.search.similarities.BM25Similarity;
import org.apache.lucene.store.Directory;
import org.apache.lucene.store.RAMDirectory;
@ -41,14 +54,17 @@ import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.transport.TransportRequest;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.IOException;
import static java.util.Collections.emptySet;
import static java.util.Collections.singleton;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.shield.authz.accesscontrol.ShieldIndexSearcherWrapper.intersectScorerAndRoleBits;
import static org.hamcrest.Matchers.*;
import static org.mockito.Matchers.eq;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.sameInstance;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ -99,7 +115,7 @@ public class ShieldIndexSearcherWrapperUnitTests extends ESTestCase {
.endObject().endObject();
mapperService.merge("type", new CompressedXContent(mappingSource.string()), false, false);
IndicesAccessControl.IndexAccessControl indexAccessControl = new IndicesAccessControl.IndexAccessControl(true, ImmutableSet.<String>of(), null);
IndicesAccessControl.IndexAccessControl indexAccessControl = new IndicesAccessControl.IndexAccessControl(true, emptySet(), null);
request.putInContext(InternalAuthorizationService.INDICES_PERMISSIONS_KEY, new IndicesAccessControl(true, ImmutableMap.of("_index", indexAccessControl)));
FieldSubsetReader.FieldSubsetDirectoryReader result = (FieldSubsetReader.FieldSubsetDirectoryReader) shieldIndexSearcherWrapper.wrap(esIn);
@ -309,7 +325,7 @@ public class ShieldIndexSearcherWrapperUnitTests extends ESTestCase {
}
private void assertResolvedFields(String expression, String... expectedFields) {
IndicesAccessControl.IndexAccessControl indexAccessControl = new IndicesAccessControl.IndexAccessControl(true, ImmutableSet.of(expression), null);
IndicesAccessControl.IndexAccessControl indexAccessControl = new IndicesAccessControl.IndexAccessControl(true, singleton(expression), null);
request.putInContext(InternalAuthorizationService.INDICES_PERMISSIONS_KEY, new IndicesAccessControl(true, ImmutableMap.of("_index", indexAccessControl)));
FieldSubsetReader.FieldSubsetDirectoryReader result = (FieldSubsetReader.FieldSubsetDirectoryReader) shieldIndexSearcherWrapper.wrap(esIn);
assertThat(result.getFieldNames().size() - shieldIndexSearcherWrapper.getAllowedMetaFields().size(), equalTo(expectedFields.length));

View File

@ -5,7 +5,6 @@
*/
package org.elasticsearch.shield.authz.store;
import com.google.common.collect.ImmutableSet;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.env.Environment;
import org.elasticsearch.shield.audit.logfile.CapturingLogger;
@ -30,7 +29,17 @@ import java.util.Set;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
import static org.hamcrest.Matchers.*;
import static java.util.Collections.singleton;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.empty;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasKey;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.hamcrest.Matchers.startsWith;
/**
*
@ -250,7 +259,7 @@ public class FileRolesStoreTests extends ESTestCase {
public void testThatRoleNamesDoesNotResolvePermissions() throws Exception {
Path path = getDataPath("invalid_roles.yml");
CapturingLogger logger = new CapturingLogger(CapturingLogger.Level.ERROR);
ImmutableSet<String> roleNames = FileRolesStore.parseFileForRoleNames(path, logger);
Set<String> roleNames = FileRolesStore.parseFileForRoleNames(path, logger);
assertThat(roleNames.size(), is(5));
assertThat(roleNames, containsInAnyOrder("valid_role", "role1", "role2", "role3", "role4"));
@ -261,11 +270,9 @@ public class FileRolesStoreTests extends ESTestCase {
@Test
public void testReservedRoles() throws Exception {
Set<Permission.Global.Role> reservedRoles = ImmutableSet.<Permission.Global.Role>builder()
.add(Permission.Global.Role.builder("reserved")
Set<Permission.Global.Role> reservedRoles = singleton(Permission.Global.Role.builder("reserved")
.cluster(Privilege.Cluster.ALL)
.build())
.build();
.build());
CapturingLogger logger = new CapturingLogger(CapturingLogger.Level.INFO);
@ -294,11 +301,9 @@ public class FileRolesStoreTests extends ESTestCase {
@Test
public void testReservedRolesNonExistentRolesFile() throws Exception {
Set<Permission.Global.Role> reservedRoles = ImmutableSet.<Permission.Global.Role>builder()
.add(Permission.Global.Role.builder("reserved")
Set<Permission.Global.Role> reservedRoles = singleton(Permission.Global.Role.builder("reserved")
.cluster(Privilege.Cluster.ALL)
.build())
.build();
.build());
CapturingLogger logger = new CapturingLogger(CapturingLogger.Level.INFO);

View File

@ -5,12 +5,10 @@
*/
package org.elasticsearch.transport;
import com.google.common.collect.ImmutableSet;
import org.apache.lucene.util.IOUtils;
import org.elasticsearch.action.Action;
import org.elasticsearch.common.io.PathUtils;
import org.elasticsearch.common.io.Streams;
import org.elasticsearch.common.util.Callback;
import org.elasticsearch.license.plugin.LicensePlugin;
import org.elasticsearch.shield.action.ShieldActionModule;
import org.elasticsearch.test.ESIntegTestCase.ClusterScope;
@ -25,22 +23,29 @@ import java.lang.reflect.Modifier;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.*;
import java.nio.file.DirectoryStream;
import java.nio.file.FileSystem;
import java.nio.file.FileSystems;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import static java.util.Collections.unmodifiableSet;
import static org.hamcrest.CoreMatchers.hasItems;
import static org.hamcrest.Matchers.hasItem;
import static org.hamcrest.Matchers.is;
@ClusterScope(numClientNodes = 0, numDataNodes = 1)
public class KnownActionsTests extends ShieldIntegTestCase {
private static ImmutableSet<String> knownActions;
private static ImmutableSet<String> knownHandlers;
private static ImmutableSet<String> codeActions;
private static Set<String> knownActions;
private static Set<String> knownHandlers;
private static Set<String> codeActions;
@BeforeClass
public static void init() throws Exception {
@ -81,38 +86,26 @@ public class KnownActionsTests extends ShieldIntegTestCase {
}
}
public static ImmutableSet<String> loadKnownActions() {
final ImmutableSet.Builder<String> knownActionsBuilder = ImmutableSet.builder();
try (InputStream input = KnownActionsTests.class.getResourceAsStream("actions")) {
Streams.readAllLines(input, new Callback<String>() {
@Override
public void handle(String action) {
knownActionsBuilder.add(action);
}
});
} catch (IOException ioe) {
throw new IllegalStateException("could not load known actions", ioe);
}
return knownActionsBuilder.build();
public static Set<String> loadKnownActions() {
return readSetFromResource("actions");
}
public static ImmutableSet<String> loadKnownHandlers() {
final ImmutableSet.Builder<String> knownHandlersBuilder = ImmutableSet.builder();
try (InputStream input = KnownActionsTests.class.getResourceAsStream("handlers")) {
Streams.readAllLines(input, new Callback<String>() {
@Override
public void handle(String action) {
knownHandlersBuilder.add(action);
}
});
} catch (IOException ioe) {
throw new IllegalStateException("could not load known handlers", ioe);
}
return knownHandlersBuilder.build();
public static Set<String> loadKnownHandlers() {
return readSetFromResource("handlers");
}
private static ImmutableSet<String> loadCodeActions() throws IOException, ReflectiveOperationException, URISyntaxException {
ImmutableSet.Builder<String> actions = ImmutableSet.builder();
private static Set<String> readSetFromResource(String resource) {
Set<String> knownActions = new HashSet<>();
try (InputStream input = KnownActionsTests.class.getResourceAsStream(resource)) {
Streams.readAllLines(input, action -> knownActions.add(action));
} catch (IOException ioe) {
throw new IllegalStateException("could not load known " + resource, ioe);
}
return unmodifiableSet(knownActions);
}
private static Set<String> loadCodeActions() throws IOException, ReflectiveOperationException, URISyntaxException {
Set<String> actions = new HashSet<>();
// loading es core actions
loadActions(collectSubClasses(Action.class, Action.class), actions);
@ -123,10 +116,10 @@ public class KnownActionsTests extends ShieldIntegTestCase {
// also loading all actions from the licensing plugin
loadActions(collectSubClasses(Action.class, LicensePlugin.class), actions);
return actions.build();
return unmodifiableSet(actions);
}
private static void loadActions(Collection<Class<?>> clazzes, ImmutableSet.Builder<String> actions) throws ReflectiveOperationException {
private static void loadActions(Collection<Class<?>> clazzes, Set<String> actions) throws ReflectiveOperationException {
for (Class<?> clazz : clazzes) {
if (!Modifier.isAbstract(clazz.getModifiers())) {
Field field = null;

View File

@ -5,7 +5,6 @@
*/
package org.elasticsearch.watcher.history;
import com.google.common.collect.ImmutableSet;
import org.elasticsearch.action.index.IndexRequest;
import org.elasticsearch.common.component.AbstractComponent;
import org.elasticsearch.common.inject.Inject;
@ -33,7 +32,6 @@ public class HistoryStore extends AbstractComponent {
public static final String DOC_TYPE = "watch_record";
static final DateTimeFormatter indexTimeFormat = DateTimeFormat.forPattern("YYYY.MM.dd");
private static final ImmutableSet<String> forbiddenIndexSettings = ImmutableSet.of("index.mapper.dynamic");
private final ClientProxy client;

View File

@ -5,7 +5,6 @@
*/
package org.elasticsearch.watcher.input.http;
import com.google.common.collect.ImmutableSet;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.Nullable;
import org.elasticsearch.common.ParseField;
@ -20,9 +19,12 @@ import org.elasticsearch.watcher.watch.Payload;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import static java.util.Collections.unmodifiableSet;
/**
*
*/
@ -171,7 +173,7 @@ public class HttpInput implements Input {
public static class Builder implements Input.Builder<HttpInput> {
private final HttpRequestTemplate request;
private final ImmutableSet.Builder<String> extractKeys = ImmutableSet.builder();
private final Set<String> extractKeys = new HashSet<>();
private HttpContentType expectedResponseXContentType = null;
private Builder(HttpRequestTemplate request) {
@ -184,7 +186,7 @@ public class HttpInput implements Input {
}
public Builder extractKeys(String... keys) {
extractKeys.add(keys);
Collections.addAll(extractKeys, keys);
return this;
}
@ -195,8 +197,7 @@ public class HttpInput implements Input {
@Override
public HttpInput build() {
ImmutableSet<String> keys = extractKeys.build();
return new HttpInput(request, expectedResponseXContentType, keys.isEmpty() ? null : keys);
return new HttpInput(request, expectedResponseXContentType, extractKeys.isEmpty() ? null : unmodifiableSet(extractKeys));
}
}

View File

@ -5,7 +5,6 @@
*/
package org.elasticsearch.watcher.input.search;
import com.google.common.collect.ImmutableSet;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.action.search.SearchRequest;
import org.elasticsearch.common.Nullable;
@ -23,9 +22,12 @@ import org.joda.time.DateTimeZone;
import java.io.IOException;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import static java.util.Collections.unmodifiableSet;
/**
*
*/
@ -193,7 +195,7 @@ public class SearchInput implements Input {
public static class Builder implements Input.Builder<SearchInput> {
private final SearchRequest request;
private final ImmutableSet.Builder<String> extractKeys = ImmutableSet.builder();
private final Set<String> extractKeys = new HashSet<>();
private TimeValue timeout;
private DateTimeZone dynamicNameTimeZone;
@ -207,7 +209,7 @@ public class SearchInput implements Input {
}
public Builder extractKeys(String... keys) {
extractKeys.add(keys);
Collections.addAll(extractKeys, keys);
return this;
}
@ -223,8 +225,7 @@ public class SearchInput implements Input {
@Override
public SearchInput build() {
Set<String> keys = extractKeys.build();
return new SearchInput(request, keys.isEmpty() ? null : keys, timeout, dynamicNameTimeZone);
return new SearchInput(request, extractKeys.isEmpty() ? null : unmodifiableSet(extractKeys), timeout, dynamicNameTimeZone);
}
}

View File

@ -6,8 +6,7 @@
package org.elasticsearch.watcher.support;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import com.google.common.util.concurrent.MoreExecutors;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateRequest;
import org.elasticsearch.action.admin.indices.template.put.PutIndexTemplateResponse;
import org.elasticsearch.cluster.ClusterChangedEvent;
@ -27,20 +26,22 @@ import org.elasticsearch.watcher.support.init.proxy.ClientProxy;
import org.elasticsearch.watcher.watch.WatchStore;
import java.io.InputStream;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.concurrent.Executor;
import static java.util.Collections.unmodifiableSet;
/**
*/
public class WatcherIndexTemplateRegistry extends AbstractComponent implements ClusterStateListener, NodeSettingsService.Listener {
private static final ImmutableSet<String> forbiddenIndexSettings = ImmutableSet.of("index.mapper.dynamic");
private static final String FORBIDDEN_INDEX_SETTING = "index.mapper.dynamic";
private final ClientProxy client;
private final ThreadPool threadPool;
private final ClusterService clusterService;
private final ImmutableSet<TemplateConfig> indexTemplates;
private final Set<TemplateConfig> indexTemplates;
private volatile ImmutableMap<String, Settings> customIndexSettings;
@ -51,7 +52,7 @@ public class WatcherIndexTemplateRegistry extends AbstractComponent implements C
this.client = client;
this.threadPool = threadPool;
this.clusterService = clusterService;
this.indexTemplates = ImmutableSet.copyOf(configs);
this.indexTemplates = unmodifiableSet(new HashSet<>(configs));
clusterService.add(this);
nodeSettingsService.addListener(this);
@ -126,7 +127,7 @@ public class WatcherIndexTemplateRegistry extends AbstractComponent implements C
Settings.Builder builder = Settings.builder().put(existingSettings);
for (Map.Entry<String, String> newSettingsEntry : newSettings.getAsMap().entrySet()) {
String name = "index." + newSettingsEntry.getKey();
if (forbiddenIndexSettings.contains(name)) {
if (FORBIDDEN_INDEX_SETTING.equals(name)) {
logger.warn("overriding the default [{}} setting is forbidden. ignoring...", name);
continue;
}

View File

@ -5,7 +5,6 @@
*/
package org.elasticsearch.watcher.trigger.schedule.support;
import com.google.common.collect.ImmutableSet;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.Strings;
@ -19,6 +18,7 @@ import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import static org.elasticsearch.common.util.set.Sets.newHashSet;
import static org.elasticsearch.watcher.support.Exceptions.illegalArgument;
import static org.elasticsearch.watcher.support.Strings.join;
@ -87,7 +87,7 @@ public class MonthTimes implements Times {
if (!Arrays.equals(days, that.days)) return false;
// order doesn't matter
if (!ImmutableSet.copyOf(times).equals(ImmutableSet.copyOf(that.times))) return false;
if (!newHashSet(times).equals(newHashSet(that.times))) return false;
return true;
}

View File

@ -5,15 +5,19 @@
*/
package org.elasticsearch.watcher.trigger.schedule.support;
import com.google.common.collect.ImmutableSet;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import java.io.IOException;
import java.util.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Set;
import static org.elasticsearch.common.util.set.Sets.newHashSet;
import static org.elasticsearch.watcher.support.Strings.join;
/**
@ -73,7 +77,7 @@ public class WeekTimes implements Times {
if (!days.equals(that.days)) return false;
// we don't care about order
if (!ImmutableSet.copyOf(times).equals(ImmutableSet.copyOf(that.times))) return false;
if (!newHashSet(times).equals(newHashSet(that.times))) return false;
return true;
}

View File

@ -5,7 +5,6 @@
*/
package org.elasticsearch.watcher.trigger.schedule.support;
import com.google.common.collect.ImmutableSet;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.ParseFieldMatcher;
import org.elasticsearch.common.Strings;
@ -14,8 +13,14 @@ import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentParser;
import java.io.IOException;
import java.util.*;
import java.util.Arrays;
import java.util.Collections;
import java.util.EnumSet;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import static org.elasticsearch.common.util.set.Sets.newHashSet;
import static org.elasticsearch.watcher.support.Exceptions.illegalArgument;
import static org.elasticsearch.watcher.support.Strings.join;
@ -91,7 +96,7 @@ public class YearTimes implements Times {
if (!Arrays.equals(days, that.days)) return false;
if (!months.equals(that.months)) return false;
// order doesn't matter
if (!ImmutableSet.copyOf(times).equals(ImmutableSet.copyOf(that.times))) return false;
if (!newHashSet(times).equals(newHashSet(that.times))) return false;
return true;
}

View File

@ -6,7 +6,7 @@
package org.elasticsearch.watcher.actions.index;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.action.search.SearchResponse;
import org.elasticsearch.common.settings.Settings;
@ -32,11 +32,18 @@ import org.junit.Test;
import java.util.Arrays;
import java.util.Map;
import static java.util.Collections.unmodifiableSet;
import static org.elasticsearch.common.util.set.Sets.newHashSet;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.index.query.QueryBuilders.matchAllQuery;
import static org.elasticsearch.search.aggregations.AggregationBuilders.terms;
import static org.elasticsearch.search.builder.SearchSourceBuilder.searchSource;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.hasSize;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.joda.time.DateTimeZone.UTC;
/**
@ -129,7 +136,7 @@ public class IndexActionTests extends ESIntegTestCase {
Object list = randomFrom(
new Map[] { ImmutableMap.of("foo", "bar"), ImmutableMap.of("foo", "bar1") },
Arrays.asList(ImmutableMap.of("foo", "bar"), ImmutableMap.of("foo", "bar1")),
ImmutableSet.of(ImmutableMap.of("foo", "bar"), ImmutableMap.of("foo", "bar1"))
unmodifiableSet(newHashSet(ImmutableMap.of("foo", "bar"), ImmutableMap.of("foo", "bar1")))
);
IndexAction action = new IndexAction("test-index", "test-type", timestampField, null, null);

View File

@ -6,7 +6,6 @@
package org.elasticsearch.watcher.support;
import com.google.common.collect.ImmutableMap;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
@ -14,9 +13,15 @@ import org.elasticsearch.common.xcontent.XContentParser;
import org.elasticsearch.test.ESTestCase;
import org.junit.Test;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import static java.util.concurrent.TimeUnit.*;
import static java.util.concurrent.TimeUnit.DAYS;
import static java.util.concurrent.TimeUnit.HOURS;
import static java.util.concurrent.TimeUnit.MILLISECONDS;
import static java.util.concurrent.TimeUnit.MINUTES;
import static java.util.concurrent.TimeUnit.SECONDS;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.watcher.test.WatcherTestUtils.xContentParser;
import static org.hamcrest.CoreMatchers.nullValue;
@ -63,11 +68,10 @@ public class WatcherDateTimeUtilsTests extends ESTestCase {
@Test
public void testParseTimeValue_String() throws Exception {
int value = randomIntBetween(2, 200);
ImmutableMap<String, TimeValue> values = ImmutableMap.<String, TimeValue>builder()
.put(value + "s", TimeValue.timeValueSeconds(value))
.put(value + "m", TimeValue.timeValueMinutes(value))
.put(value + "h", TimeValue.timeValueHours(value))
.build();
Map<String, TimeValue> values = new HashMap<>();
values.put(value + "s", TimeValue.timeValueSeconds(value));
values.put(value + "m", TimeValue.timeValueMinutes(value));
values.put(value + "h", TimeValue.timeValueHours(value));
String key = randomFrom(values.keySet().toArray(new String[values.size()]));
@ -84,11 +88,10 @@ public class WatcherDateTimeUtilsTests extends ESTestCase {
@Test(expected = ElasticsearchParseException.class)
public void testParseTimeValue_String_Negative() throws Exception {
int value = -1 * randomIntBetween(2, 200);
ImmutableMap<String, TimeValue> values = ImmutableMap.<String, TimeValue>builder()
.put(value + "s", TimeValue.timeValueSeconds(value))
.put(value + "m", TimeValue.timeValueMinutes(value))
.put(value + "h", TimeValue.timeValueHours(value))
.build();
Map<String, TimeValue> values = new HashMap<>();
values.put(value + "s", TimeValue.timeValueSeconds(value));
values.put(value + "m", TimeValue.timeValueMinutes(value));
values.put(value + "h", TimeValue.timeValueHours(value));
String key = randomFrom(values.keySet().toArray(new String[values.size()]));

View File

@ -6,9 +6,9 @@
package org.elasticsearch.watcher.support.text.xmustache;
import com.fasterxml.jackson.core.io.JsonStringEncoder;
import org.elasticsearch.common.bytes.BytesReference;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentType;
import org.elasticsearch.script.CompiledScript;
@ -25,7 +25,13 @@ import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import static org.hamcrest.Matchers.*;
import static java.util.Collections.singleton;
import static org.elasticsearch.common.util.set.Sets.newHashSet;
import static org.hamcrest.Matchers.both;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.notNullValue;
/**
*
@ -46,14 +52,21 @@ public class XMustacheTests extends ESTestCase {
Map<String, Object> vars = new HashMap<>();
Object data = randomFrom(
new String[] { "foo", "bar" },
Arrays.asList("foo", "bar"),
ImmutableSet.of("foo", "bar"));
Arrays.asList("foo", "bar"));
vars.put("data", data);
Object output = engine.execute(mustache, vars);
assertThat(output, notNullValue());
assertThat(output, instanceOf(BytesReference.class));
BytesReference bytes = (BytesReference) output;
assertThat(bytes.toUtf8(), equalTo("foo bar"));
// Sets can come out in any order
vars.put("data", newHashSet("foo", "bar"));
output = engine.execute(mustache, vars);
assertThat(output, notNullValue());
assertThat(output, instanceOf(BytesReference.class));
bytes = (BytesReference) output;
assertThat(bytes.toUtf8(), both(containsString("foo")).and(containsString("bar")));
}
@Test
@ -64,7 +77,7 @@ public class XMustacheTests extends ESTestCase {
Object data = randomFrom(
new String[][] { new String[] { "foo", "bar" }},
Collections.singletonList(new String[] { "foo", "bar" }),
ImmutableSet.of(new String[] { "foo", "bar" })
singleton(new String[] { "foo", "bar" })
);
vars.put("data", data);
Object output = engine.execute(mustache, vars);
@ -81,14 +94,22 @@ public class XMustacheTests extends ESTestCase {
Map<String, Object> vars = new HashMap<>();
Object data = randomFrom(
new Map[] { ImmutableMap.<String, Object>of("key", "foo"), ImmutableMap.<String, Object>of("key", "bar") },
Arrays.asList(ImmutableMap.<String, Object>of("key", "foo"), ImmutableMap.<String, Object>of("key", "bar")),
ImmutableSet.of(ImmutableMap.<String, Object>of("key", "foo"), ImmutableMap.<String, Object>of("key", "bar")));
Arrays.asList(ImmutableMap.<String, Object>of("key", "foo"), ImmutableMap.<String, Object>of("key", "bar")));
vars.put("data", data);
Object output = engine.execute(mustache, vars);
assertThat(output, notNullValue());
assertThat(output, instanceOf(BytesReference.class));
BytesReference bytes = (BytesReference) output;
assertThat(bytes.toUtf8(), equalTo("foo bar"));
// HashSet iteration order isn't fixed
vars.put("data", newHashSet(ImmutableMap.<String, Object>of("key", "foo"), ImmutableMap.<String, Object>of("key", "bar")));
output = engine.execute(mustache, vars);
assertThat(output, notNullValue());
assertThat(output, instanceOf(BytesReference.class));
bytes = (BytesReference) output;
assertThat(bytes.toUtf8(), both(containsString("foo")).and(containsString("bar")));
}
@Test

View File

@ -6,7 +6,7 @@
package org.elasticsearch.watcher.transform.script;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import org.elasticsearch.common.settings.Settings;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
@ -32,10 +32,18 @@ import java.util.Arrays;
import java.util.Collections;
import java.util.Map;
import static java.util.Collections.singleton;
import static org.elasticsearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.elasticsearch.watcher.support.Exceptions.illegalArgument;
import static org.elasticsearch.watcher.test.WatcherTestUtils.*;
import static org.hamcrest.Matchers.*;
import static org.elasticsearch.watcher.test.WatcherTestUtils.EMPTY_PAYLOAD;
import static org.elasticsearch.watcher.test.WatcherTestUtils.getScriptServiceProxy;
import static org.elasticsearch.watcher.test.WatcherTestUtils.mockExecutionContext;
import static org.elasticsearch.watcher.test.WatcherTestUtils.simplePayload;
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.hasEntry;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
@ -133,7 +141,7 @@ public class ScriptTransformTests extends ESTestCase {
Map<String, Object> model = Variables.createCtxModel(ctx, payload);
ExecutableScript executable = mock(ExecutableScript.class);
Object value = randomFrom("value", 1, new String[] { "value" }, Arrays.asList("value"), ImmutableSet.of("value"));
Object value = randomFrom("value", 1, new String[] { "value" }, Arrays.asList("value"), singleton("value"));
when(executable.run()).thenReturn(value);
when(service.executable(compiledScript, model)).thenReturn(executable);

View File

@ -6,7 +6,7 @@
package org.elasticsearch.watcher.watch;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import org.elasticsearch.ElasticsearchParseException;
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.logging.ESLogger;
@ -16,7 +16,11 @@ import org.elasticsearch.common.unit.TimeValue;
import org.elasticsearch.common.xcontent.XContentBuilder;
import org.elasticsearch.common.xcontent.XContentFactory;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.watcher.actions.*;
import org.elasticsearch.watcher.actions.ActionFactory;
import org.elasticsearch.watcher.actions.ActionRegistry;
import org.elasticsearch.watcher.actions.ActionStatus;
import org.elasticsearch.watcher.actions.ActionWrapper;
import org.elasticsearch.watcher.actions.ExecutableActions;
import org.elasticsearch.watcher.actions.email.DataAttachment;
import org.elasticsearch.watcher.actions.email.EmailAction;
import org.elasticsearch.watcher.actions.email.EmailActionFactory;
@ -92,19 +96,43 @@ import org.elasticsearch.watcher.transform.search.SearchTransformFactory;
import org.elasticsearch.watcher.trigger.Trigger;
import org.elasticsearch.watcher.trigger.TriggerEngine;
import org.elasticsearch.watcher.trigger.TriggerService;
import org.elasticsearch.watcher.trigger.schedule.*;
import org.elasticsearch.watcher.trigger.schedule.support.*;
import org.elasticsearch.watcher.trigger.schedule.CronSchedule;
import org.elasticsearch.watcher.trigger.schedule.DailySchedule;
import org.elasticsearch.watcher.trigger.schedule.HourlySchedule;
import org.elasticsearch.watcher.trigger.schedule.IntervalSchedule;
import org.elasticsearch.watcher.trigger.schedule.MonthlySchedule;
import org.elasticsearch.watcher.trigger.schedule.Schedule;
import org.elasticsearch.watcher.trigger.schedule.ScheduleRegistry;
import org.elasticsearch.watcher.trigger.schedule.ScheduleTrigger;
import org.elasticsearch.watcher.trigger.schedule.ScheduleTriggerEngine;
import org.elasticsearch.watcher.trigger.schedule.WeeklySchedule;
import org.elasticsearch.watcher.trigger.schedule.YearlySchedule;
import org.elasticsearch.watcher.trigger.schedule.support.DayOfWeek;
import org.elasticsearch.watcher.trigger.schedule.support.Month;
import org.elasticsearch.watcher.trigger.schedule.support.MonthTimes;
import org.elasticsearch.watcher.trigger.schedule.support.WeekTimes;
import org.elasticsearch.watcher.trigger.schedule.support.YearTimes;
import org.joda.time.DateTime;
import org.joda.time.DateTimeZone;
import org.junit.Before;
import org.junit.Test;
import java.util.*;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import static java.util.Collections.singleton;
import static org.elasticsearch.watcher.input.InputBuilders.searchInput;
import static org.elasticsearch.watcher.test.WatcherTestUtils.matchAllRequest;
import static org.elasticsearch.watcher.trigger.TriggerBuilders.schedule;
import static org.hamcrest.Matchers.*;
import static org.hamcrest.Matchers.equalTo;
import static org.hamcrest.Matchers.instanceOf;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.notNullValue;
import static org.hamcrest.Matchers.nullValue;
import static org.joda.time.DateTimeZone.UTC;
import static org.mockito.Mockito.mock;
@ -147,7 +175,7 @@ public class WatchTests extends ESTestCase {
Trigger trigger = new ScheduleTrigger(schedule);
ScheduleRegistry scheduleRegistry = registry(schedule);
TriggerEngine triggerEngine = new ParseOnlyScheduleTriggerEngine(Settings.EMPTY, scheduleRegistry, clock);
TriggerService triggerService = new TriggerService(Settings.EMPTY, ImmutableSet.of(triggerEngine));
TriggerService triggerService = new TriggerService(Settings.EMPTY, singleton(triggerEngine));
SecretService secretService = new SecretService.PlainText();
ExecutableInput input = randomInput();
@ -197,7 +225,7 @@ public class WatchTests extends ESTestCase {
ClockMock clock = new ClockMock();
ScheduleRegistry scheduleRegistry = registry(randomSchedule());
TriggerEngine triggerEngine = new ParseOnlyScheduleTriggerEngine(Settings.EMPTY, scheduleRegistry, clock);
TriggerService triggerService = new TriggerService(Settings.EMPTY, ImmutableSet.of(triggerEngine));
TriggerService triggerService = new TriggerService(Settings.EMPTY, singleton(triggerEngine));
SecretService secretService = new SecretService.PlainText();
ExecutableCondition condition = randomCondition();
ConditionRegistry conditionRegistry = registry(condition);
@ -228,7 +256,7 @@ public class WatchTests extends ESTestCase {
Schedule schedule = randomSchedule();
ScheduleRegistry scheduleRegistry = registry(schedule);
TriggerEngine triggerEngine = new ParseOnlyScheduleTriggerEngine(Settings.EMPTY, scheduleRegistry, SystemClock.INSTANCE);
TriggerService triggerService = new TriggerService(Settings.EMPTY, ImmutableSet.of(triggerEngine));
TriggerService triggerService = new TriggerService(Settings.EMPTY, singleton(triggerEngine));
SecretService secretService = new SecretService.PlainText();
ConditionRegistry conditionRegistry = registry(new ExecutableAlwaysCondition(logger));