From 478b572f6ed1710a6ebf678cc084edce360955de Mon Sep 17 00:00:00 2001 From: matejkec Date: Thu, 4 May 2023 19:46:48 +0200 Subject: [PATCH] Implement Challenge #1 --- .../RU102J/dao/SiteDaoRedisImpl.java | 21 +++++++++++++++---- .../RU102J/dao/SiteDaoRedisImplTest.java | 9 +------- 2 files changed, 18 insertions(+), 12 deletions(-) diff --git a/src/main/java/com/redislabs/university/RU102J/dao/SiteDaoRedisImpl.java b/src/main/java/com/redislabs/university/RU102J/dao/SiteDaoRedisImpl.java index f1def9f..90ad37f 100644 --- a/src/main/java/com/redislabs/university/RU102J/dao/SiteDaoRedisImpl.java +++ b/src/main/java/com/redislabs/university/RU102J/dao/SiteDaoRedisImpl.java @@ -4,7 +4,10 @@ import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; -import java.util.*; +import java.util.Collections; +import java.util.HashSet; +import java.util.Map; +import java.util.Set; public class SiteDaoRedisImpl implements SiteDao { private final JedisPool jedisPool; @@ -41,8 +44,18 @@ public Site findById(long id) { // Challenge #1 @Override public Set findAll() { - // START Challenge #1 - return Collections.emptySet(); - // END Challenge #1 + try(Jedis jedis = jedisPool.getResource()) { + String key = RedisSchema.getSiteIDsKey(); + Set siteHashKeys = jedis.smembers(key); + + Set sites = new HashSet<>(); + for (String siteHashKey : siteHashKeys) { + Map fields = jedis.hgetAll(siteHashKey); + if(fields != null && !fields.isEmpty()) { + sites.add(new Site(fields)); + } + } + return sites; + } } } diff --git a/src/test/java/com/redislabs/university/RU102J/dao/SiteDaoRedisImplTest.java b/src/test/java/com/redislabs/university/RU102J/dao/SiteDaoRedisImplTest.java index f461f96..d88b916 100644 --- a/src/test/java/com/redislabs/university/RU102J/dao/SiteDaoRedisImplTest.java +++ b/src/test/java/com/redislabs/university/RU102J/dao/SiteDaoRedisImplTest.java @@ -2,23 +2,18 @@ import com.redislabs.university.RU102J.HostPort; import com.redislabs.university.RU102J.TestKeyManager; -import com.redislabs.university.RU102J.api.MeterReading; import com.redislabs.university.RU102J.api.Site; import org.junit.*; import redis.clients.jedis.Jedis; import redis.clients.jedis.JedisPool; import redis.clients.jedis.JedisPoolConfig; -import java.time.ZoneOffset; -import java.time.ZonedDateTime; import java.util.HashSet; import java.util.Map; import java.util.Set; import static org.hamcrest.MatcherAssert.assertThat; -import static org.hamcrest.Matchers.is; -import static org.hamcrest.Matchers.nullValue; -import static org.hamcrest.Matchers.empty; +import static org.hamcrest.Matchers.*; import static org.junit.Assert.assertEquals; public class SiteDaoRedisImplTest { @@ -97,7 +92,6 @@ public void findByIdWithMissingSite() { * Challenge #1 Part 1. Use this test case to * implement the challenge in Chapter 1. */ - @Ignore @Test public void findAllWithMultipleSites() { SiteDaoRedisImpl dao = new SiteDaoRedisImpl(jedisPool); @@ -113,7 +107,6 @@ public void findAllWithMultipleSites() { * Challenge #1 Part 2. Use this test case to * implement the challenge in Chapter 1. */ - @Ignore @Test public void findAllWithEmptySites() { SiteDaoRedisImpl dao = new SiteDaoRedisImpl(jedisPool);