Skip to content

Commit

Permalink
feat: 1520 gtfs validator desktop app deprecation (#1555)
Browse files Browse the repository at this point in the history
  • Loading branch information
qcdyx authored Sep 13, 2023
1 parent 96a1b4d commit cf7e96d
Show file tree
Hide file tree
Showing 6 changed files with 110 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ public synchronized void resolve() {
*
* @throws IOException
*/
private Optional<String> resolveCurrentVersion() throws IOException {
public Optional<String> resolveCurrentVersion() throws IOException {
ScanResult scan = new ClassGraph().scan();
Optional<String> gtfsValidatorCoreVersion = Optional.empty();
for (Resource resource : scan.getResourcesWithPath("META-INF/MANIFEST.MF")) {
Expand Down
20 changes: 20 additions & 0 deletions web/service/open-api-spec.json
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,26 @@
"security": []
}
},
"/version": {
"get": {
"summary": "version",
"description": "This endpoint returns the current version of gtfs validator",
"operationId": "version",
"responses": {
"200": {
"description": "",
"headers": {},
"content": {
"text/plain": {
"schema": {}
}
}
}
},
"deprecated": false,
"security": []
}
},
"/error": {
"post": {
"summary": "error",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,11 @@ public static void main(String[] args) {

@Bean
public ValidationRunner validationRunner() {
return new ValidationRunner(new VersionResolver());
return new ValidationRunner(versionResolver());
}

@Bean
public VersionResolver versionResolver() {
return new VersionResolver();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import java.net.URL;
import java.nio.file.Path;
import java.util.*;
import org.mobilitydata.gtfsvalidator.util.VersionResolver;
import org.mobilitydata.gtfsvalidator.web.service.util.JobMetadata;
import org.mobilitydata.gtfsvalidator.web.service.util.StorageHelper;
import org.mobilitydata.gtfsvalidator.web.service.util.ValidationHandler;
Expand All @@ -43,6 +44,8 @@ public class ValidationController {
@Autowired private StorageHelper storageHelper;
@Autowired private ValidationHandler validationHandler;

@Autowired private VersionResolver versionResolver;

/**
* Creates a new job id and returns it to the client. If a url is provided, the file is downloaded
* from the url and saved to GCS. If no url is provided, a unique url is generated for the client
Expand Down Expand Up @@ -121,6 +124,22 @@ public ResponseEntity runValidator(
}
}

@GetMapping("/version")
public VersionResponse currentVersion() {
VersionResponse versionResponse;
try {
Optional<String> versionInfo = versionResolver.resolveCurrentVersion();
if (versionInfo.isEmpty()) {
throw new ResponseStatusException(
HttpStatus.INTERNAL_SERVER_ERROR, "Current Version Not Found");
}
versionResponse = new VersionResponse(versionInfo.get());
} catch (IOException e) {
throw new ResponseStatusException(HttpStatus.INTERNAL_SERVER_ERROR, "Error", e);
}
return versionResponse;
}

@PostMapping("/error")
public ResponseEntity Error() {
try {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package org.mobilitydata.gtfsvalidator.web.service.controller;

import lombok.AllArgsConstructor;
import lombok.Data;

@Data
@AllArgsConstructor
public class VersionResponse {
private String version;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
package org.mobilitydata.gtfsvalidator.web.service.controller;

import static org.mockito.Mockito.*;

import java.io.IOException;
import java.util.Optional;
import org.junit.jupiter.api.Test;
import org.mobilitydata.gtfsvalidator.util.VersionResolver;
import org.mobilitydata.gtfsvalidator.web.service.util.StorageHelper;
import org.mobilitydata.gtfsvalidator.web.service.util.ValidationHandler;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;

@WebMvcTest(ValidationController.class)
public class VersionEndpointTest {
@Autowired private MockMvc mockMvc;
@MockBean private StorageHelper storageHelper;
// must be mocked or application context fails to load correctly
@MockBean private ValidationHandler handler;
@MockBean private VersionResolver versionResolver;

private final String newVersion = "myVersion";

@Test
public void testVersionEndpoint() throws Exception {
doReturn(Optional.of(newVersion)).when(versionResolver).resolveCurrentVersion();
mockMvc
.perform(MockMvcRequestBuilders.get("/version").contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().isOk())
.andExpect(MockMvcResultMatchers.jsonPath("$.version").isString())
.andExpect(MockMvcResultMatchers.jsonPath("$.version").value(newVersion));
}

@Test
public void testFailedVersionEndpoint() throws Exception {
doThrow(new IOException()).when(versionResolver).resolveCurrentVersion();
mockMvc
.perform(MockMvcRequestBuilders.get("/version").contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().is5xxServerError());
}

@Test
public void testVersionNotFound() throws Exception {
doReturn(Optional.empty()).when(versionResolver).resolveCurrentVersion();
mockMvc
.perform(MockMvcRequestBuilders.get("/version").contentType(MediaType.APPLICATION_JSON))
.andExpect(MockMvcResultMatchers.status().is5xxServerError());
}
}

0 comments on commit cf7e96d

Please sign in to comment.