-
Notifications
You must be signed in to change notification settings - Fork 2.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #39302 from karesti/mock-support
Adds RemoteCache @InjectMock support in Infinispan
- Loading branch information
Showing
6 changed files
with
205 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
...on-tests/infinispan-client/src/main/java/io/quarkus/it/infinispan/client/BookService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package io.quarkus.it.infinispan.client; | ||
|
||
import jakarta.enterprise.context.ApplicationScoped; | ||
import jakarta.inject.Inject; | ||
|
||
import org.infinispan.client.hotrod.RemoteCache; | ||
import org.infinispan.client.hotrod.RemoteCacheManager; | ||
|
||
import io.quarkus.infinispan.client.Remote; | ||
|
||
@ApplicationScoped | ||
public class BookService { | ||
public static final String DEFAULT_DESCRIPTION = "Default description"; | ||
|
||
@Inject | ||
RemoteCacheManager cacheManager; | ||
|
||
@Inject | ||
@Remote(CacheSetup.BOOKS_CACHE) | ||
RemoteCache<String, Book> books; | ||
|
||
public String getBookDescriptionById(String id) { | ||
Book book = books.get(id); | ||
if (book == null) { | ||
return DEFAULT_DESCRIPTION; | ||
} | ||
|
||
return book.description(); | ||
} | ||
|
||
public String getBookDescriptionById(String cacheName, String id) { | ||
RemoteCache<String, Book> cache = cacheManager.getCache(cacheName); | ||
if (cache == null) { | ||
throw new IllegalArgumentException("Cache not found"); | ||
} | ||
Book book = cache.get(id); | ||
if (book == null) { | ||
return DEFAULT_DESCRIPTION; | ||
} | ||
return book.description(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
...nispan-client/src/test/java/io/quarkus/it/infinispan/client/MockInfinispanClientTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package io.quarkus.it.infinispan.client; | ||
|
||
import static io.quarkus.it.infinispan.client.BookService.DEFAULT_DESCRIPTION; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; | ||
import static org.mockito.Mockito.mock; | ||
import static org.mockito.Mockito.when; | ||
|
||
import java.math.BigDecimal; | ||
import java.util.Collections; | ||
|
||
import jakarta.inject.Inject; | ||
|
||
import org.infinispan.client.hotrod.RemoteCache; | ||
import org.infinispan.client.hotrod.RemoteCacheManager; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import io.quarkus.infinispan.client.Remote; | ||
import io.quarkus.test.InjectMock; | ||
import io.quarkus.test.junit.QuarkusTest; | ||
|
||
@QuarkusTest | ||
public class MockInfinispanClientTest { | ||
|
||
@Inject | ||
BookService bookService; | ||
|
||
@InjectMock | ||
RemoteCacheManager cacheManager; | ||
|
||
@InjectMock | ||
@Remote(CacheSetup.BOOKS_CACHE) | ||
RemoteCache<String, Book> bookRemoteCache; | ||
|
||
private final Book BOOK = new Book("Full Saga of Harry Potter", "Best saga ever", 1997, | ||
Collections.emptySet(), Type.FANTASY, new BigDecimal("500.99")); | ||
|
||
@Test | ||
public void mockRemoteCacheManager() { | ||
RemoteCache<String, Book> localMockCache = mock(RemoteCache.class); | ||
when(localMockCache.get("harry_potter")).thenReturn(BOOK); | ||
when(cacheManager.<String, Book> getCache(CacheSetup.BOOKS_CACHE)).thenReturn(localMockCache); | ||
|
||
assertThat(bookService.getBookDescriptionById(CacheSetup.BOOKS_CACHE, "non_exist")).isEqualTo(DEFAULT_DESCRIPTION); | ||
assertThat(bookService.getBookDescriptionById(CacheSetup.BOOKS_CACHE, "harry_potter")).isEqualTo("Best saga ever"); | ||
assertThatIllegalArgumentException().isThrownBy( | ||
() -> bookService.getBookDescriptionById(CacheSetup.DEFAULT_CACHE, "non_exist")); | ||
} | ||
|
||
@Test | ||
public void mockRemoteCache() { | ||
when(bookRemoteCache.get("harry_potter")).thenReturn(BOOK); | ||
assertThat(bookService.getBookDescriptionById("non_exist")).isEqualTo(DEFAULT_DESCRIPTION); | ||
assertThat(bookService.getBookDescriptionById("harry_potter")).isEqualTo("Best saga ever"); | ||
} | ||
} |