|
| 1 | +package rest.koios.client.backend.factory; |
| 2 | + |
| 3 | +import org.junit.jupiter.api.Test; |
| 4 | +import rest.koios.client.backend.api.base.BaseService; |
| 5 | + |
| 6 | +import java.lang.reflect.Constructor; |
| 7 | +import java.lang.reflect.InvocationTargetException; |
| 8 | +import java.lang.reflect.Modifier; |
| 9 | + |
| 10 | +import static org.junit.jupiter.api.Assertions.*; |
| 11 | + |
| 12 | +class BackendFactoryTest { |
| 13 | + |
| 14 | + private static String baseUrlOf(BackendService service) { |
| 15 | + return ((BaseService) service.getNetworkService()).getRetrofit().baseUrl().toString(); |
| 16 | + } |
| 17 | + |
| 18 | + @Test |
| 19 | + void mainnetPointsAtMainnetTest() { |
| 20 | + assertEquals("https://api.koios.rest/api/v1/", baseUrlOf(BackendFactory.getKoiosMainnetService())); |
| 21 | + assertEquals("https://api.koios.rest/api/v1/", baseUrlOf(BackendFactory.getKoiosMainnetService("token"))); |
| 22 | + assertEquals("https://api.koios.rest/api/v1/", |
| 23 | + baseUrlOf(BackendFactory.getKoiosMainnetService(ApiVersion.VERSION_1, "token"))); |
| 24 | + } |
| 25 | + |
| 26 | + @Test |
| 27 | + void eachNetworkGetsItsOwnHostTest() { |
| 28 | + assertEquals("https://guild.koios.rest/api/v1/", baseUrlOf(BackendFactory.getKoiosGuildService())); |
| 29 | + assertEquals("https://preview.koios.rest/api/v1/", baseUrlOf(BackendFactory.getKoiosPreviewService())); |
| 30 | + assertEquals("https://preprod.koios.rest/api/v1/", baseUrlOf(BackendFactory.getKoiosPreprodService())); |
| 31 | + } |
| 32 | + |
| 33 | + @Test |
| 34 | + void tokenOverloadsResolveToTheSameHostTest() { |
| 35 | + assertEquals(baseUrlOf(BackendFactory.getKoiosGuildService()), |
| 36 | + baseUrlOf(BackendFactory.getKoiosGuildService("token"))); |
| 37 | + assertEquals(baseUrlOf(BackendFactory.getKoiosPreviewService()), |
| 38 | + baseUrlOf(BackendFactory.getKoiosPreviewService(ApiVersion.VERSION_1, "token"))); |
| 39 | + assertEquals(baseUrlOf(BackendFactory.getKoiosPreprodService()), |
| 40 | + baseUrlOf(BackendFactory.getKoiosPreprodService("token"))); |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + void customRpcUsesTheGivenUrlTest() { |
| 45 | + String url = "http://localhost:8080/api/v1/"; |
| 46 | + assertEquals(url, baseUrlOf(BackendFactory.getCustomRPCService(url))); |
| 47 | + assertEquals(url, baseUrlOf(BackendFactory.getCustomRPCService(url, "token"))); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + void everyServiceIsWiredTest() { |
| 52 | + BackendService service = BackendFactory.getKoiosMainnetService(); |
| 53 | + assertNotNull(service.getNetworkService()); |
| 54 | + assertNotNull(service.getEpochService()); |
| 55 | + assertNotNull(service.getBlockService()); |
| 56 | + assertNotNull(service.getTransactionsService()); |
| 57 | + assertNotNull(service.getAccountService()); |
| 58 | + assertNotNull(service.getAddressService()); |
| 59 | + assertNotNull(service.getAssetService()); |
| 60 | + assertNotNull(service.getPoolService()); |
| 61 | + assertNotNull(service.getScriptService()); |
| 62 | + assertNotNull(service.getGovernanceService()); |
| 63 | + } |
| 64 | + |
| 65 | + @Test |
| 66 | + void apiVersionCarriesItsPathSegmentTest() { |
| 67 | + assertEquals("v1", ApiVersion.VERSION_1.getVersion()); |
| 68 | + assertEquals(1, ApiVersion.values().length); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + void operationTypesCarryTheirUrlsTest() { |
| 73 | + assertEquals("https://api.koios.rest/api/", OperationType.KOIOS_MAINNET.getBaseUrl()); |
| 74 | + assertEquals("https://guild.koios.rest/api/", OperationType.KOIOS_GUILD.getBaseUrl()); |
| 75 | + assertEquals("https://preview.koios.rest/api/", OperationType.KOIOS_PREVIEW.getBaseUrl()); |
| 76 | + assertEquals("https://preprod.koios.rest/api/", OperationType.KOIOS_PREPROD.getBaseUrl()); |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + void factoryIsNotInstantiableTest() throws NoSuchMethodException { |
| 81 | + Constructor<BackendFactory> constructor = BackendFactory.class.getDeclaredConstructor(); |
| 82 | + assertTrue(Modifier.isPrivate(constructor.getModifiers())); |
| 83 | + constructor.setAccessible(true); |
| 84 | + InvocationTargetException thrown = |
| 85 | + assertThrows(InvocationTargetException.class, constructor::newInstance); |
| 86 | + assertInstanceOf(IllegalStateException.class, thrown.getCause()); |
| 87 | + } |
| 88 | +} |
0 commit comments