|
8 | 8 | import java.io.IOException; |
9 | 9 | import java.nio.file.Files; |
10 | 10 | import java.nio.file.Path; |
| 11 | +import java.time.Duration; |
11 | 12 | import java.time.Instant; |
| 13 | +import java.time.temporal.ChronoUnit; |
12 | 14 |
|
13 | 15 | import org.junit.jupiter.api.Test; |
14 | 16 | import org.junit.jupiter.api.io.TempDir; |
15 | 17 |
|
| 18 | +import com.devonfw.tools.ide.os.OperatingSystem; |
| 19 | +import com.devonfw.tools.ide.os.SystemArchitecture; |
| 20 | +import com.devonfw.tools.ide.url.model.file.UrlChecksum; |
| 21 | +import com.devonfw.tools.ide.url.model.file.UrlDownloadFile; |
| 22 | +import com.devonfw.tools.ide.url.model.file.UrlStatusFile; |
16 | 23 | import com.devonfw.tools.ide.url.model.file.json.StatusJson; |
17 | 24 | import com.devonfw.tools.ide.url.model.file.json.UrlStatus; |
| 25 | +import com.devonfw.tools.ide.url.model.file.json.UrlStatusState; |
| 26 | +import com.devonfw.tools.ide.url.model.folder.UrlEdition; |
18 | 27 | import com.devonfw.tools.ide.url.model.folder.UrlRepository; |
| 28 | +import com.devonfw.tools.ide.url.model.folder.UrlTool; |
| 29 | +import com.devonfw.tools.ide.url.model.folder.UrlVersion; |
19 | 30 | import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo; |
20 | 31 | import com.github.tomakehurst.wiremock.junit5.WireMockTest; |
21 | 32 |
|
@@ -93,80 +104,153 @@ public void testUrlUpdaterIsNotUpdatingWhenStatusManualIsTrue(@TempDir Path temp |
93 | 104 | } |
94 | 105 |
|
95 | 106 | /** |
96 | | - * Tests if the timestamps of the status.json get updated properly. Creates an initial status.json with a success timestamp. Updates the status.json with an |
97 | | - * error timestamp and compares it with the success timestamp. Updates the status.json with a final success timestamp and compares it with the error |
98 | | - * timestamp. |
| 107 | + * Tests if the timestamps of the status.json get updated properly if a first error is detected. |
99 | 108 | * <p> |
100 | 109 | * See: <a href="https://github.com/devonfw/ide/issues/1343">#1343</a> for reference. |
101 | 110 | * |
102 | 111 | * @param tempDir Temporary directory |
103 | 112 | * @param wmRuntimeInfo wireMock server on a random port |
104 | 113 | */ |
105 | 114 | @Test |
106 | | - public void testUrlUpdaterStatusJsonRefreshBugStillExisting(@TempDir Path tempDir, WireMockRuntimeInfo wmRuntimeInfo) { |
| 115 | + public void testStatusJsonUpdateOnFirstError(@TempDir Path tempDir, WireMockRuntimeInfo wmRuntimeInfo) { |
107 | 116 |
|
108 | 117 | // arrange |
109 | | - stubFor(any(urlMatching("/os/.*")).willReturn(aResponse().withStatus(200).withBody("aBody"))); |
110 | | - |
111 | | - UrlRepository urlRepository = UrlRepository.load(tempDir); |
112 | | - UrlUpdaterMockSingle updater = new UrlUpdaterMockSingle(wmRuntimeInfo); |
113 | | - |
114 | | - String statusUrl = wmRuntimeInfo.getHttpBaseUrl() + "/os/windows_x64_url.tgz"; |
115 | 118 | String toolName = "mocked"; |
116 | 119 | String editionName = "mocked"; |
117 | 120 | String versionName = "1.0"; |
| 121 | + String url = wmRuntimeInfo.getHttpBaseUrl() + "/os/windows_x64_url.tgz"; |
| 122 | + Instant now = Instant.now(); |
| 123 | + Instant lastMonth = now.minus(31, ChronoUnit.DAYS); |
| 124 | + UrlRepository urlRepository = UrlRepository.load(tempDir); |
| 125 | + UrlTool urlTool = urlRepository.getOrCreateChild(toolName); |
| 126 | + UrlEdition urlEdition = urlTool.getOrCreateChild(editionName); |
| 127 | + UrlVersion urlVersion = urlEdition.getOrCreateChild(versionName); |
| 128 | + // we create the structure of our tool version and URL to simulate it was valid last moth |
| 129 | + UrlStatusFile statusFile = urlVersion.getOrCreateStatus(); |
| 130 | + UrlStatus status = statusFile.getStatusJson().getOrCreateUrlStatus(url); |
| 131 | + UrlStatusState successState = new UrlStatusState(lastMonth); // ensure that we trigger a recheck of the URL |
| 132 | + status.setSuccess(successState); |
| 133 | + UrlDownloadFile urlDownloadFile = urlVersion.getOrCreateUrls(OperatingSystem.WINDOWS, SystemArchitecture.X64); |
| 134 | + urlDownloadFile.addUrl(url); |
| 135 | + UrlChecksum urlChecksum = urlVersion.getOrCreateChecksum(urlDownloadFile.getName()); |
| 136 | + urlChecksum.setChecksum("1234567890"); |
| 137 | + urlVersion.save(); |
| 138 | + UrlUpdaterMockSingle updater = new UrlUpdaterMockSingle(wmRuntimeInfo); |
| 139 | + // now we want to simulate that the url got broken (404) and the updater is properly handling this |
| 140 | + stubFor(any(urlMatching("/os/.*")).willReturn(aResponse().withStatus(404))); |
118 | 141 |
|
119 | 142 | // act |
120 | 143 | updater.update(urlRepository); |
121 | 144 |
|
122 | | - Path versionsPath = tempDir.resolve(toolName).resolve(editionName).resolve(versionName); |
123 | | - |
124 | 145 | // assert |
125 | | - assertThat(versionsPath.resolve("status.json")).exists(); |
126 | | - |
127 | 146 | StatusJson statusJson = retrieveStatusJson(urlRepository, toolName, editionName, versionName); |
| 147 | + status = statusJson.getStatus(url); |
| 148 | + successState = status.getSuccess(); |
| 149 | + assertThat(successState).isNotNull(); |
| 150 | + assertThat(successState.getTimestamp()).isEqualTo(lastMonth); |
| 151 | + UrlStatusState errorState = status.getError(); |
| 152 | + assertThat(errorState).isNotNull(); |
| 153 | + assertThat(errorState.getCode()).isEqualTo(404); |
| 154 | + assertThat(Duration.between(errorState.getTimestamp(), now)).isLessThan(Duration.ofSeconds(5)); |
| 155 | + } |
128 | 156 |
|
129 | | - UrlStatus urlStatus = statusJson.getOrCreateUrlStatus(statusUrl); |
130 | | - |
131 | | - Instant successTimestamp = urlStatus.getSuccess().getTimestamp(); |
132 | | - |
133 | | - assertThat(successTimestamp).isNotNull(); |
134 | | - |
135 | | - stubFor(any(urlMatching("/os/.*")).willReturn(aResponse().withStatus(404))); |
136 | | - |
137 | | - // re-initialize UrlRepository for error timestamp |
138 | | - UrlRepository urlRepositoryWithError = UrlRepository.load(tempDir); |
139 | | - updater.update(urlRepositoryWithError); |
140 | | - |
141 | | - statusJson = retrieveStatusJson(urlRepositoryWithError, toolName, editionName, versionName); |
142 | | - |
143 | | - urlStatus = statusJson.getOrCreateUrlStatus(statusUrl); |
144 | | - successTimestamp = urlStatus.getSuccess().getTimestamp(); |
145 | | - Instant errorTimestamp = urlStatus.getError().getTimestamp(); |
146 | | - Integer errorCode = urlStatus.getError().getCode(); |
147 | | - |
148 | | - assertThat(errorCode).isEqualTo(404); |
149 | | - assertThat(errorTimestamp).isAfter(successTimestamp); |
| 157 | + /** |
| 158 | + * Tests if the timestamps of the status.json get updated properly on success after an error. |
| 159 | + * <p> |
| 160 | + * See: <a href="https://github.com/devonfw/ide/issues/1343">#1343</a> for reference. |
| 161 | + * |
| 162 | + * @param tempDir Temporary directory |
| 163 | + * @param wmRuntimeInfo wireMock server on a random port |
| 164 | + */ |
| 165 | + @Test |
| 166 | + public void testSuccessStateUpdatedAfterError(@TempDir Path tempDir, WireMockRuntimeInfo wmRuntimeInfo) { |
150 | 167 |
|
| 168 | + // arrange |
| 169 | + String toolName = "mocked"; |
| 170 | + String editionName = "mocked"; |
| 171 | + String versionName = "1.0"; |
| 172 | + String url = wmRuntimeInfo.getHttpBaseUrl() + "/os/windows_x64_url.tgz"; |
| 173 | + Instant now = Instant.now(); |
| 174 | + Instant lastMonth = now.minus(31, ChronoUnit.DAYS); |
| 175 | + Instant lastSuccess = lastMonth.minus(1, ChronoUnit.DAYS); |
| 176 | + UrlRepository urlRepository = UrlRepository.load(tempDir); |
| 177 | + UrlTool urlTool = urlRepository.getOrCreateChild(toolName); |
| 178 | + UrlEdition urlEdition = urlTool.getOrCreateChild(editionName); |
| 179 | + UrlVersion urlVersion = urlEdition.getOrCreateChild(versionName); |
| 180 | + // we create the structure of our tool version and URL to simulate it was valid last moth |
| 181 | + UrlStatusFile statusFile = urlVersion.getOrCreateStatus(); |
| 182 | + UrlStatus status = statusFile.getStatusJson().getOrCreateUrlStatus(url); |
| 183 | + UrlStatusState successState = new UrlStatusState(lastSuccess); |
| 184 | + status.setSuccess(successState); |
| 185 | + UrlStatusState errorState = new UrlStatusState(lastMonth); |
| 186 | + errorState.setCode(404); |
| 187 | + status.setError(errorState); |
| 188 | + UrlDownloadFile urlDownloadFile = urlVersion.getOrCreateUrls(OperatingSystem.WINDOWS, SystemArchitecture.X64); |
| 189 | + urlDownloadFile.addUrl(url); |
| 190 | + UrlChecksum urlChecksum = urlVersion.getOrCreateChecksum(urlDownloadFile.getName()); |
| 191 | + urlChecksum.setChecksum("1234567890"); |
| 192 | + urlVersion.save(); |
| 193 | + UrlUpdaterMockSingle updater = new UrlUpdaterMockSingle(wmRuntimeInfo); |
| 194 | + // now we want to simulate that the broken url is working again |
151 | 195 | stubFor(any(urlMatching("/os/.*")).willReturn(aResponse().withStatus(200).withHeader("Content-Type", "text/plain"))); |
152 | 196 |
|
153 | | - // re-initialize UrlRepository for error timestamp |
154 | | - UrlRepository urlRepositoryWithSuccess = UrlRepository.load(tempDir); |
155 | | - updater.update(urlRepositoryWithSuccess); |
156 | | - |
157 | | - assertThat(versionsPath.resolve("status.json")).exists(); |
| 197 | + // act |
| 198 | + updater.update(urlRepository); |
158 | 199 |
|
159 | | - statusJson = retrieveStatusJson(urlRepositoryWithSuccess, toolName, editionName, versionName); |
| 200 | + // assert |
| 201 | + status = retrieveStatusJson(urlRepository, toolName, editionName, versionName).getStatus(url); |
| 202 | + successState = status.getSuccess(); |
| 203 | + assertThat(successState).isNotNull(); |
| 204 | + assertThat(Duration.between(successState.getTimestamp(), now)).isLessThan(Duration.ofSeconds(5)); |
| 205 | + errorState = status.getError(); |
| 206 | + assertThat(errorState).isNotNull(); |
| 207 | + assertThat(errorState.getCode()).isEqualTo(404); |
| 208 | + assertThat(errorState.getTimestamp()).isEqualTo(lastMonth); |
| 209 | + } |
160 | 210 |
|
161 | | - urlStatus = statusJson.getOrCreateUrlStatus(statusUrl); |
| 211 | + /** |
| 212 | + * Tests if the the tool version gets entirely removed if all versions are broken for a long time. |
| 213 | + * |
| 214 | + * @param tempDir Temporary directory |
| 215 | + * @param wmRuntimeInfo wireMock server on a random port |
| 216 | + */ |
| 217 | + @Test |
| 218 | + public void testVersionRemovedIfErrorPersists(@TempDir Path tempDir, WireMockRuntimeInfo wmRuntimeInfo) { |
162 | 219 |
|
163 | | - successTimestamp = urlStatus.getSuccess().getTimestamp(); |
164 | | - errorTimestamp = urlStatus.getError().getTimestamp(); |
165 | | - errorCode = urlStatus.getError().getCode(); |
| 220 | + // arrange |
| 221 | + String toolName = "mocked"; |
| 222 | + String editionName = "mocked"; |
| 223 | + String versionName = "1.0"; |
| 224 | + String url = wmRuntimeInfo.getHttpBaseUrl() + "/os/windows_x64_url.tgz"; |
| 225 | + Instant now = Instant.now(); |
| 226 | + Instant lastMonth = now.minus(31, ChronoUnit.DAYS); |
| 227 | + Instant lastSuccess = lastMonth.minus(1, ChronoUnit.DAYS); |
| 228 | + UrlRepository urlRepository = UrlRepository.load(tempDir); |
| 229 | + UrlTool urlTool = urlRepository.getOrCreateChild(toolName); |
| 230 | + UrlEdition urlEdition = urlTool.getOrCreateChild(editionName); |
| 231 | + UrlVersion urlVersion = urlEdition.getOrCreateChild(versionName); |
| 232 | + // we create the structure of our tool version and URL to simulate it was valid last moth |
| 233 | + UrlStatusFile statusFile = urlVersion.getOrCreateStatus(); |
| 234 | + UrlStatus status = statusFile.getStatusJson().getOrCreateUrlStatus(url); |
| 235 | + UrlStatusState successState = new UrlStatusState(lastSuccess); |
| 236 | + status.setSuccess(successState); |
| 237 | + UrlStatusState errorState = new UrlStatusState(lastMonth); |
| 238 | + errorState.setCode(404); |
| 239 | + status.setError(errorState); |
| 240 | + UrlDownloadFile urlDownloadFile = urlVersion.getOrCreateUrls(OperatingSystem.WINDOWS, SystemArchitecture.X64); |
| 241 | + urlDownloadFile.addUrl(url); |
| 242 | + UrlChecksum urlChecksum = urlVersion.getOrCreateChecksum(urlDownloadFile.getName()); |
| 243 | + urlChecksum.setChecksum("1234567890"); |
| 244 | + urlVersion.save(); |
| 245 | + UrlUpdaterMockSingle updater = new UrlUpdaterMockSingle(wmRuntimeInfo); |
| 246 | + // now we want to simulate that the url got broken (404) and the updater is properly handling this |
| 247 | + stubFor(any(urlMatching("/os/.*")).willReturn(aResponse().withStatus(404))); |
166 | 248 |
|
167 | | - assertThat(errorCode).isEqualTo(200); |
168 | | - assertThat(errorTimestamp).isAfter(successTimestamp); |
| 249 | + // act |
| 250 | + updater.update(urlRepository); |
169 | 251 |
|
| 252 | + // assert |
| 253 | + assertThat(urlVersion.getPath()).doesNotExist(); |
170 | 254 | } |
171 | 255 |
|
172 | 256 | /** |
|
0 commit comments