From 61f29fcf8b50c0dfda2655d584afb86814ad4f66 Mon Sep 17 00:00:00 2001 From: Luis Majano Date: Tue, 7 May 2024 20:12:27 +0200 Subject: [PATCH] cacheIDExists done --- .../compat/bifs/cache/CacheGetKeysStream.java | 82 ------------------- .../{CacheLookup.java => CacheIdExists.java} | 5 +- .../compat/bifs/cache/CacheIdExistsTest.java | 63 ++++++++++++++ 3 files changed, 65 insertions(+), 85 deletions(-) delete mode 100644 src/main/java/ortus/boxlang/modules/compat/bifs/cache/CacheGetKeysStream.java rename src/main/java/ortus/boxlang/modules/compat/bifs/cache/{CacheLookup.java => CacheIdExists.java} (96%) create mode 100644 src/test/java/ortus/boxlang/modules/compat/bifs/cache/CacheIdExistsTest.java diff --git a/src/main/java/ortus/boxlang/modules/compat/bifs/cache/CacheGetKeysStream.java b/src/main/java/ortus/boxlang/modules/compat/bifs/cache/CacheGetKeysStream.java deleted file mode 100644 index 29b4974..0000000 --- a/src/main/java/ortus/boxlang/modules/compat/bifs/cache/CacheGetKeysStream.java +++ /dev/null @@ -1,82 +0,0 @@ -/** - * [BoxLang] - * - * Copyright [2023] [Ortus Solutions, Corp] - * - * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the - * License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" - * BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language - * governing permissions and limitations under the License. - */ -package ortus.boxlang.modules.compat.bifs.cache; - -import java.util.Set; - -import ortus.boxlang.runtime.bifs.BIF; -import ortus.boxlang.runtime.bifs.BoxBIF; -import ortus.boxlang.runtime.cache.filters.ICacheKeyFilter; -import ortus.boxlang.runtime.cache.filters.RegexFilter; -import ortus.boxlang.runtime.cache.filters.WildcardFilter; -import ortus.boxlang.runtime.cache.providers.ICacheProvider; -import ortus.boxlang.runtime.cache.util.CacheExistsValidator; -import ortus.boxlang.runtime.context.IBoxContext; -import ortus.boxlang.runtime.scopes.ArgumentsScope; -import ortus.boxlang.runtime.scopes.Key; -import ortus.boxlang.runtime.types.Argument; -import ortus.boxlang.runtime.validation.Validator; - -@BoxBIF -public class CacheGetKeysStream extends BIF { - - private static final Validator cacheExistsValidator = new CacheExistsValidator(); - - /** - * Constructor - */ - public CacheGetKeysStream() { - super(); - declaredArguments = new Argument[] { - new Argument( false, Argument.STRING, Key.filter, "" ), - new Argument( false, Argument.STRING, Key.cacheName, Key._DEFAULT, Set.of( cacheExistsValidator ) ), - new Argument( false, Argument.BOOLEAN, Key.useRegex, false ), - }; - } - - /** - * Get all the keys in the cache but return a stream instead of an array. - * If no cache name is provided, the default cache is used. - * If a filter is provided, only keys that match the filter will be returned. - * A filter is a simple string that can contain wildcards and will leverage the {@link WildcardFilter} to match keys. - * - * @param context The context in which the BIF is being invoked. - * @param arguments Argument scope for the BIF. - * - * @argument.filter The filter to apply to the keys, this can be a simple Wildcard filter or a regex filter. The default is a simple wildcard filter. - * - * @argument.cacheName The name of the cache to get the keys from. Default is the default cache. - * - * @argument.useRegex If true, the filter will be treated as a full regular expression filter. Default is false. - * - * @return A stream of keys from the cache. - */ - public Object _invoke( IBoxContext context, ArgumentsScope arguments ) { - ICacheProvider cache = cacheService.getCache( arguments.getAsKey( Key.cacheName ) ); - String filter = arguments.getAsString( Key.filter ); - Boolean useRegex = arguments.getAsBoolean( Key.useRegex ); - - // No filter? get all of them - if ( filter.isEmpty() ) { - return cache.getKeysStream(); - } - - // Build the right filter - ICacheKeyFilter keyFilter = useRegex ? new RegexFilter( filter ) : new WildcardFilter( filter ); - - // Filter the keys - return cache.getKeysStream( keyFilter ); - } -} diff --git a/src/main/java/ortus/boxlang/modules/compat/bifs/cache/CacheLookup.java b/src/main/java/ortus/boxlang/modules/compat/bifs/cache/CacheIdExists.java similarity index 96% rename from src/main/java/ortus/boxlang/modules/compat/bifs/cache/CacheLookup.java rename to src/main/java/ortus/boxlang/modules/compat/bifs/cache/CacheIdExists.java index 9ec838d..072daf2 100644 --- a/src/main/java/ortus/boxlang/modules/compat/bifs/cache/CacheLookup.java +++ b/src/main/java/ortus/boxlang/modules/compat/bifs/cache/CacheIdExists.java @@ -28,15 +28,14 @@ import ortus.boxlang.runtime.validation.Validator; @BoxBIF -@BoxBIF( alias = "cacheIdExists" ) -public class CacheLookup extends BIF { +public class CacheIdExists extends BIF { private static final Validator cacheExistsValidator = new CacheExistsValidator(); /** * Constructor */ - public CacheLookup() { + public CacheIdExists() { super(); declaredArguments = new Argument[] { new Argument( true, Argument.ANY, Key.id ), diff --git a/src/test/java/ortus/boxlang/modules/compat/bifs/cache/CacheIdExistsTest.java b/src/test/java/ortus/boxlang/modules/compat/bifs/cache/CacheIdExistsTest.java new file mode 100644 index 0000000..ad5d97f --- /dev/null +++ b/src/test/java/ortus/boxlang/modules/compat/bifs/cache/CacheIdExistsTest.java @@ -0,0 +1,63 @@ +/** + * [BoxLang] + * + * Copyright [2023] [Ortus Solutions, Corp] + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package ortus.boxlang.modules.compat.bifs.cache; + +import static com.google.common.truth.Truth.assertThat; + +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; + +public class CacheIdExistsTest extends BaseCacheTest { + + @Test + @DisplayName( "Can validate a key exists in the default cache" ) + public void canValidateACacheExists() { + runtime.executeSource( + """ + result = CacheIdExists( "tdd" ); + """, + context ); + + assertThat( variables.get( "result" ) ).isEqualTo( true ); + } + + @Test + @DisplayName( "Can validate a key exists in a specific cache" ) + public void canValidateACacheExistsInSpecificCache() { + runtime.executeSource( + """ + result = CacheIdExists( "tdd", "default" ); + """, + context ); + + assertThat( variables.get( "result" ) ).isEqualTo( true ); + } + + @Test + @DisplayName( "Can validate a key does not exist in the default cache" ) + public void canValidateACacheDoesNotExist() { + runtime.executeSource( + """ + result = CacheIdExists( "doesnotexist" ); + """, + context ); + + assertThat( variables.get( "result" ) ).isEqualTo( false ); + } + +}