Skip to content

Commit

Permalink
Implement Challenge redislabs-training#1
Browse files Browse the repository at this point in the history
  • Loading branch information
matejkec committed May 4, 2023
1 parent 3b4a18a commit 478b572
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -41,8 +44,18 @@ public Site findById(long id) {
// Challenge #1
@Override
public Set<Site> findAll() {
// START Challenge #1
return Collections.emptySet();
// END Challenge #1
try(Jedis jedis = jedisPool.getResource()) {
String key = RedisSchema.getSiteIDsKey();
Set<String> siteHashKeys = jedis.smembers(key);

Set<Site> sites = new HashSet<>();
for (String siteHashKey : siteHashKeys) {
Map<String, String> fields = jedis.hgetAll(siteHashKey);
if(fields != null && !fields.isEmpty()) {
sites.add(new Site(fields));
}
}
return sites;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit 478b572

Please sign in to comment.