blob: d87082cb24674deb06d0ecd6e1ccfc7d8820971b [file] [log] [blame]
/*
* Licensed to the Apache Software Foundation (ASF) under one or more contributor license
* agreements. See the NOTICE file distributed with this work for additional information regarding
* copyright ownership. The ASF licenses this file to You 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 org.apache.geode.redis.internal.executor.string;
import static org.apache.geode.redis.internal.RedisConstants.ERROR_WRONG_TYPE;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.Protocol;
import org.apache.geode.test.awaitility.GeodeAwaitility;
import org.apache.geode.test.dunit.rules.RedisPortSupplier;
public abstract class AbstractGetIntegrationTest implements RedisPortSupplier {
private Jedis jedis;
private static final int REDIS_CLIENT_TIMEOUT =
Math.toIntExact(GeodeAwaitility.getTimeout().toMillis());
@Before
public void setUp() {
jedis = new Jedis("localhost", getPort(), REDIS_CLIENT_TIMEOUT);
}
@After
public void tearDown() {
jedis.flushAll();
jedis.close();
}
@Test
public void givenKeyNotProvided_returnsWrongNumberOfArgumentsError() {
assertThatThrownBy(() -> jedis.sendCommand(Protocol.Command.GET))
.hasMessageContaining("ERR wrong number of arguments for 'get' command");
}
@Test
public void givenMoreThanTwoArguments_returnsWrongNumberOfArgumentsError() {
assertThatThrownBy(() -> jedis.sendCommand(Protocol.Command.GET, "key", "extraArg"))
.hasMessageContaining("ERR wrong number of arguments for 'get' command");
}
@Test
public void testGET_shouldReturnValueOfKey_givenValueIsAString() {
String key = "key";
String value = "value";
String result = jedis.get(key);
assertThat(result).isNull();
jedis.set(key, value);
result = jedis.get(key);
assertThat(result).isEqualTo(value);
}
@Test
public void testGET_shouldReturnNil_givenKeyDoesNotExist() {
String key = "this key does not exist";
String result = jedis.get(key);
assertThat(result).isNull();
}
@Test
public void testGET_shouldReturnEmptyString_givenKeyIsEmpty() {
String key = "emptyKey";
jedis.set(key, "");
String result = jedis.get(key);
assertThat(result).isEmpty();
}
@Test
public void testGET_shouldThrowJedisDataExceptionError_givenValueIsNotAString() {
String key = "key";
String field = "field";
String member = "member";
jedis.sadd(key, field, member);
assertThatThrownBy(() -> jedis.get(key)).hasMessageContaining(ERROR_WRONG_TYPE);
}
}