Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
move api/v2/version endpoint to OPTIONS /api
- Loading branch information
1 parent
2dd3de5
commit 681e992
Showing
6 changed files
with
147 additions
and
1 deletion.
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
18 changes: 18 additions & 0 deletions
18
molgenis-api/src/main/java/org/molgenis/api/AppVersionResponse.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,18 @@ | ||
package org.molgenis.api; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import java.time.Instant; | ||
import org.molgenis.util.AutoGson; | ||
|
||
@AutoValue | ||
@AutoGson(autoValueClass = AutoValue_AppVersionResponse.class) | ||
@SuppressWarnings("squid:S1610") | ||
public abstract class AppVersionResponse { | ||
public abstract String getVersion(); | ||
|
||
public abstract Instant getBuildDate(); | ||
|
||
public static AppVersionResponse create(String version, Instant date) { | ||
return new AutoValue_AppVersionResponse(version, date); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
molgenis-api/src/main/java/org/molgenis/api/RootApiController.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,44 @@ | ||
package org.molgenis.api; | ||
|
||
import static java.time.ZonedDateTime.now; | ||
import static java.util.Objects.requireNonNull; | ||
import static org.molgenis.api.ApiNamespace.API_PATH; | ||
import static org.springframework.web.bind.annotation.RequestMethod.OPTIONS; | ||
|
||
import java.text.ParseException; | ||
import java.text.SimpleDateFormat; | ||
import java.time.Instant; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.beans.factory.annotation.Value; | ||
import org.springframework.web.bind.annotation.RequestMapping; | ||
import org.springframework.web.bind.annotation.RestController; | ||
|
||
@RestController | ||
@RequestMapping(API_PATH) | ||
public class RootApiController extends ApiController { | ||
|
||
private final String molgenisVersion; | ||
private String molgenisBuildDate; | ||
public static final String VERSION_DATE_FORMAT = "yyyy-MM-dd hh:mm z"; | ||
|
||
public RootApiController( | ||
@Value("${molgenis.version:@null}") String molgenisVersion, | ||
@Value("${molgenis.build.date:@null}") String molgenisBuildDate) { | ||
super("", 1); | ||
this.molgenisVersion = requireNonNull(molgenisVersion); | ||
this.molgenisBuildDate = requireNonNull(molgenisBuildDate); | ||
} | ||
|
||
@Autowired | ||
@RequestMapping(method = OPTIONS) | ||
public VersionResponse getVersion() throws ParseException { | ||
Instant date; | ||
if (molgenisBuildDate.equals("${maven.build.timestamp}")) { | ||
date = now().toInstant(); | ||
} else { | ||
date = new SimpleDateFormat(VERSION_DATE_FORMAT).parse(molgenisBuildDate).toInstant(); | ||
} | ||
AppVersionResponse appVersionResponse = AppVersionResponse.create(molgenisVersion, date); | ||
return VersionResponse.create(appVersionResponse); | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
molgenis-api/src/main/java/org/molgenis/api/VersionResponse.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,15 @@ | ||
package org.molgenis.api; | ||
|
||
import com.google.auto.value.AutoValue; | ||
import org.molgenis.util.AutoGson; | ||
|
||
@AutoValue | ||
@AutoGson(autoValueClass = AutoValue_VersionResponse.class) | ||
@SuppressWarnings("squid:S1610") | ||
public abstract class VersionResponse { | ||
public abstract AppVersionResponse getApp(); | ||
|
||
public static VersionResponse create(AppVersionResponse appVersion) { | ||
return new AutoValue_VersionResponse(appVersion); | ||
} | ||
} |
54 changes: 54 additions & 0 deletions
54
molgenis-api/src/test/java/org/molgenis/api/VersionControllerTest.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,54 @@ | ||
package org.molgenis.api; | ||
|
||
import static org.springframework.http.MediaType.APPLICATION_JSON_UTF8; | ||
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.options; | ||
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
import static org.testng.Assert.assertEquals; | ||
|
||
import org.molgenis.web.converter.GsonConfig; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.http.converter.json.GsonHttpMessageConverter; | ||
import org.springframework.security.test.context.support.WithSecurityContextTestExecutionListener; | ||
import org.springframework.test.context.ContextConfiguration; | ||
import org.springframework.test.context.TestExecutionListeners; | ||
import org.springframework.test.context.TestPropertySource; | ||
import org.springframework.test.context.testng.AbstractTestNGSpringContextTests; | ||
import org.springframework.test.web.servlet.MockMvc; | ||
import org.springframework.test.web.servlet.MvcResult; | ||
import org.springframework.test.web.servlet.setup.MockMvcBuilders; | ||
import org.testng.annotations.BeforeMethod; | ||
import org.testng.annotations.Test; | ||
|
||
@TestExecutionListeners(listeners = WithSecurityContextTestExecutionListener.class) | ||
@TestPropertySource(properties = {"molgenis.version = 10.3.8"}) | ||
@ContextConfiguration(classes = {GsonConfig.class}) | ||
public class VersionControllerTest extends AbstractTestNGSpringContextTests { | ||
|
||
@Autowired private GsonHttpMessageConverter gsonHttpMessageConverter; | ||
|
||
private MockMvc mockMvc; | ||
|
||
@BeforeMethod | ||
public void beforeMethod() { | ||
RootApiController rootApiController = | ||
new RootApiController("versionString", "2019-05-21 10:32 UTC"); | ||
mockMvc = | ||
MockMvcBuilders.standaloneSetup(rootApiController) | ||
.setMessageConverters(gsonHttpMessageConverter) | ||
.build(); | ||
} | ||
|
||
@Test | ||
public void testGetVersion() throws Exception { | ||
MvcResult result = | ||
mockMvc | ||
.perform(options("/api").contentType(APPLICATION_JSON_UTF8)) | ||
.andExpect(status().isOk()) | ||
.andReturn(); | ||
String actual = result.getResponse().getContentAsString(); | ||
String expected = | ||
"{\"app\":{\"version\":\"versionString\",\"buildDate\":\"2019-05-21T10:32:00Z\"}}"; | ||
|
||
assertEquals(actual, expected); | ||
} | ||
} |