Skip to content

Instantly share code, notes, and snippets.

@jlojosnegros
Last active May 13, 2019 22:10
Show Gist options
  • Save jlojosnegros/ebcc51255796e4aee1d1a460023fe5cf to your computer and use it in GitHub Desktop.
Save jlojosnegros/ebcc51255796e4aee1d1a460023fe5cf to your computer and use it in GitHub Desktop.
package dummy;
import io.lettuce.core.api.StatefulRedisConnection;
import io.lettuce.core.api.sync.RedisCommands;
import io.micronaut.context.ApplicationContext;
import io.micronaut.http.client.HttpClient;
import io.micronaut.runtime.server.EmbeddedServer;
import io.micronaut.validation.Validated;
import lombok.Builder;
import lombok.EqualsAndHashCode;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
import org.junit.jupiter.api.AfterAll;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.inject.Inject;
import javax.inject.Singleton;
import java.io.Serializable;
import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import static org.junit.jupiter.api.Assertions.assertNotNull;
public class DummyTest {
private static final Logger LOG = LoggerFactory.getLogger(DummyTest.class);
private static EmbeddedServer server;
private static HttpClient client;
@Inject
private CatalogContentRepository repository;
@BeforeAll
public static void setupServer() {
server = ApplicationContext.run(EmbeddedServer.class);
client = server
.getApplicationContext()
.createBean(HttpClient.class, server.getURL());
}
@AfterAll
public static void stopServer() {
if (server != null) {
server.stop();
}
if (client != null) {
client.stop();
}
}
@Test
public void testIssue() throws Exception {
final Date now = Date.from(Instant.now());
CatalogContent expectedContentOne = CatalogContent.builder()
.contentId(1)
.status(ContentStatus.AVAILABLE)
.title("uno")
.streamId(1)
.available(now)
.tags(Set.of("tag1", "tag2"))
.build();
repository.save(expectedContentOne);
CatalogContent expectedContentTwo = CatalogContent.builder()
.contentId(2)
.status(ContentStatus.SOON)
.title("dos")
.streamId(2)
.available(now)
.tags(Set.of("tag1", "tag2"))
.build();
repository.save(expectedContentTwo);
List<CatalogContent> expectedContents = List.of(expectedContentOne, expectedContentTwo);
String body = client.toBlocking().retrieve("/catalog/content");
assertNotNull(body);
}
}
@Singleton
@Validated
class CatalogContentRepository {
private static final Logger LOG = LoggerFactory.getLogger(CatalogContentRepository.class);
private StatefulRedisConnection<String, CatalogContent> connection;
public CatalogContentRepository(StatefulRedisConnection<String, CatalogContent> connection) {
this.connection = connection;
}
public List<CatalogContent> findAll() {
LOG.info("CatalogContentRepository::findAll");
RedisCommands<String, CatalogContent> redisApi = connection.sync();
List<String> keys = redisApi.keys("*");
LOG.info("keys:" + keys);
List<CatalogContent> contents = keys.stream()
.map(redisApi::get)
.collect(Collectors.toList());
LOG.info("contents: " + contents);
return contents;
}
public void save(CatalogContent content) {
RedisCommands<String, CatalogContent> redisApi = connection.sync();
redisApi.set(String.valueOf(content.getContentId()),content);
}
}
@Builder
@Getter
@ToString
@EqualsAndHashCode
class CatalogContent implements Serializable {
private static final long serialVersionUID = 1L;
//@jlom todo @Id
private long contentId;
@Setter
private String title;
@Setter
private long streamId;
@Setter
private Set<String> tags;
@Setter
private Date available;
@Setter
private ContentStatus status;
}
enum ContentStatus implements Serializable {
AVAILABLE,
UNAVAILABLE,
SOON,
ON_HOLD,
VOTING;
private static final long serialVersionUID = 1L;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment