|
| 1 | +package org.cloudfoundry.multiapps.controller.client.facade.adapters; |
| 2 | + |
| 3 | +import org.cloudfoundry.client.v3.Metadata; |
| 4 | +import org.cloudfoundry.client.v3.processes.Data; |
| 5 | +import org.cloudfoundry.client.v3.processes.HealthCheck; |
| 6 | +import org.cloudfoundry.client.v3.processes.HealthCheckType; |
| 7 | +import org.cloudfoundry.client.v3.processes.ProcessRelationships; |
| 8 | +import org.cloudfoundry.client.v3.processes.ProcessResource; |
| 9 | +import org.cloudfoundry.client.v3.processes.ReadinessHealthCheck; |
| 10 | +import org.cloudfoundry.client.v3.processes.ReadinessHealthCheckType; |
| 11 | +import org.cloudfoundry.multiapps.controller.client.facade.domain.CloudProcess; |
| 12 | +import org.cloudfoundry.multiapps.controller.client.facade.domain.ImmutableCloudProcess; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | + |
| 15 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 16 | +import static org.junit.jupiter.api.Assertions.assertNull; |
| 17 | + |
| 18 | +class RawCloudProcessTest { |
| 19 | + |
| 20 | + private static final String COMMAND = "bundle exec rackup"; |
| 21 | + private static final Integer DISK_IN_MB = 1024; |
| 22 | + private static final Integer INSTANCES = 2; |
| 23 | + private static final Integer MEMORY_IN_MB = 512; |
| 24 | + private static final String HTTP_ENDPOINT = "/health"; |
| 25 | + private static final Integer TIMEOUT = 30; |
| 26 | + private static final Integer INVOCATION_TIMEOUT = 5; |
| 27 | + private static final Integer INTERVAL = 10; |
| 28 | + private static final String READINESS_HTTP_ENDPOINT = "/ready"; |
| 29 | + private static final Integer READINESS_INVOCATION_TIMEOUT = 4; |
| 30 | + private static final Integer READINESS_INTERVAL = 8; |
| 31 | + |
| 32 | + @Test |
| 33 | + void testDeriveWithFullHealthCheckDataIncludingInterval() { |
| 34 | + RawCloudProcess raw = ImmutableRawCloudProcess.of(buildProcessResource(HealthCheckType.HTTP, buildHealthCheckData(), |
| 35 | + ReadinessHealthCheckType.HTTP, |
| 36 | + buildReadinessHealthCheckData())); |
| 37 | + |
| 38 | + CloudProcess derived = raw.derive(); |
| 39 | + |
| 40 | + assertEquals(buildExpected(org.cloudfoundry.multiapps.controller.client.facade.domain.HealthCheckType.HTTP, INTERVAL, |
| 41 | + ReadinessHealthCheckType.HTTP.getValue()), |
| 42 | + derived); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + void testDeriveWithMissingHealthCheckDataLeavesIntervalNull() { |
| 47 | + // healthCheck.getData() returns null -> all four health-check Integer/String fields stay null |
| 48 | + RawCloudProcess raw = ImmutableRawCloudProcess.of(buildProcessResource(HealthCheckType.PORT, null, |
| 49 | + ReadinessHealthCheckType.PORT, null)); |
| 50 | + |
| 51 | + CloudProcess derived = raw.derive(); |
| 52 | + |
| 53 | + assertEquals(org.cloudfoundry.multiapps.controller.client.facade.domain.HealthCheckType.PORT, derived.getHealthCheckType()); |
| 54 | + assertNull(derived.getHealthCheckTimeout()); |
| 55 | + assertNull(derived.getHealthCheckInvocationTimeout()); |
| 56 | + assertNull(derived.getHealthCheckHttpEndpoint()); |
| 57 | + assertNull(derived.getHealthCheckInterval()); |
| 58 | + assertNull(derived.getReadinessHealthCheckInvocationTimeout()); |
| 59 | + assertNull(derived.getReadinessHealthCheckHttpEndpoint()); |
| 60 | + assertNull(derived.getReadinessHealthCheckInterval()); |
| 61 | + } |
| 62 | + |
| 63 | + @Test |
| 64 | + void testDeriveWithHealthCheckDataPresentButIntervalNull() { |
| 65 | + Data dataWithoutInterval = Data.builder() |
| 66 | + .endpoint(HTTP_ENDPOINT) |
| 67 | + .timeout(TIMEOUT) |
| 68 | + .invocationTimeout(INVOCATION_TIMEOUT) |
| 69 | + .build(); |
| 70 | + RawCloudProcess raw = ImmutableRawCloudProcess.of(buildProcessResource(HealthCheckType.HTTP, dataWithoutInterval, |
| 71 | + ReadinessHealthCheckType.PORT, null)); |
| 72 | + |
| 73 | + CloudProcess derived = raw.derive(); |
| 74 | + |
| 75 | + assertEquals(TIMEOUT, derived.getHealthCheckTimeout()); |
| 76 | + assertEquals(INVOCATION_TIMEOUT, derived.getHealthCheckInvocationTimeout()); |
| 77 | + assertEquals(HTTP_ENDPOINT, derived.getHealthCheckHttpEndpoint()); |
| 78 | + assertNull(derived.getHealthCheckInterval()); |
| 79 | + } |
| 80 | + |
| 81 | + private static ProcessResource buildProcessResource(HealthCheckType healthCheckType, Data healthCheckData, |
| 82 | + ReadinessHealthCheckType readinessHealthCheckType, Data readinessHealthCheckData) { |
| 83 | + HealthCheck.Builder healthCheckBuilder = HealthCheck.builder() |
| 84 | + .type(healthCheckType); |
| 85 | + if (healthCheckData != null) { |
| 86 | + healthCheckBuilder.data(healthCheckData); |
| 87 | + } |
| 88 | + ReadinessHealthCheck.Builder readinessBuilder = ReadinessHealthCheck.builder() |
| 89 | + .type(readinessHealthCheckType); |
| 90 | + if (readinessHealthCheckData != null) { |
| 91 | + readinessBuilder.data(readinessHealthCheckData); |
| 92 | + } |
| 93 | + return ProcessResource.builder() |
| 94 | + .id(RawCloudEntityTest.GUID_STRING) |
| 95 | + .createdAt(RawCloudEntityTest.CREATED_AT_STRING) |
| 96 | + .updatedAt(RawCloudEntityTest.UPDATED_AT_STRING) |
| 97 | + .command(COMMAND) |
| 98 | + .diskInMb(DISK_IN_MB) |
| 99 | + .instances(INSTANCES) |
| 100 | + .memoryInMb(MEMORY_IN_MB) |
| 101 | + .type("web") |
| 102 | + .metadata(Metadata.builder() |
| 103 | + .build()) |
| 104 | + .relationships(ProcessRelationships.builder() |
| 105 | + .build()) |
| 106 | + .healthCheck(healthCheckBuilder.build()) |
| 107 | + .readinessHealthCheck(readinessBuilder.build()) |
| 108 | + .build(); |
| 109 | + } |
| 110 | + |
| 111 | + private static Data buildHealthCheckData() { |
| 112 | + return Data.builder() |
| 113 | + .endpoint(HTTP_ENDPOINT) |
| 114 | + .timeout(TIMEOUT) |
| 115 | + .invocationTimeout(INVOCATION_TIMEOUT) |
| 116 | + .interval(INTERVAL) |
| 117 | + .build(); |
| 118 | + } |
| 119 | + |
| 120 | + private static Data buildReadinessHealthCheckData() { |
| 121 | + return Data.builder() |
| 122 | + .endpoint(READINESS_HTTP_ENDPOINT) |
| 123 | + .invocationTimeout(READINESS_INVOCATION_TIMEOUT) |
| 124 | + .interval(READINESS_INTERVAL) |
| 125 | + .build(); |
| 126 | + } |
| 127 | + |
| 128 | + private static CloudProcess buildExpected(org.cloudfoundry.multiapps.controller.client.facade.domain.HealthCheckType type, |
| 129 | + Integer interval, String readinessHealthCheckType) { |
| 130 | + return ImmutableCloudProcess.builder() |
| 131 | + .command(COMMAND) |
| 132 | + .instances(INSTANCES) |
| 133 | + .memoryInMb(MEMORY_IN_MB) |
| 134 | + .diskInMb(DISK_IN_MB) |
| 135 | + .healthCheckType(type) |
| 136 | + .healthCheckHttpEndpoint(HTTP_ENDPOINT) |
| 137 | + .healthCheckTimeout(TIMEOUT) |
| 138 | + .healthCheckInvocationTimeout(INVOCATION_TIMEOUT) |
| 139 | + .healthCheckInterval(interval) |
| 140 | + .readinessHealthCheckType(readinessHealthCheckType) |
| 141 | + .readinessHealthCheckHttpEndpoint(READINESS_HTTP_ENDPOINT) |
| 142 | + .readinessHealthCheckInvocationTimeout(READINESS_INVOCATION_TIMEOUT) |
| 143 | + .readinessHealthCheckInterval(READINESS_INTERVAL) |
| 144 | + .build(); |
| 145 | + } |
| 146 | +} |
0 commit comments