Skip to content

Commit

Permalink
move api/v2/version endpoint to OPTIONS /api
Browse files Browse the repository at this point in the history
  • Loading branch information
bartcharbon committed May 23, 2019
1 parent 2dd3de5 commit 681e992
Show file tree
Hide file tree
Showing 6 changed files with 147 additions and 1 deletion.
15 changes: 15 additions & 0 deletions molgenis-api/pom.xml
Expand Up @@ -34,6 +34,10 @@
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-web</artifactId>
</dependency>
<dependency>
<groupId>io.swagger</groupId>
<artifactId>swagger-annotations</artifactId>
</dependency>
<dependency>
<groupId>com.google.auto.value</groupId>
<artifactId>auto-value</artifactId>
Expand All @@ -46,5 +50,16 @@
<version>${project.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.molgenis</groupId>
<artifactId>molgenis-web</artifactId>
<version>${project.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
</project>
Expand Up @@ -17,7 +17,7 @@ public abstract class ApiController {
*/
public ApiController(String apiId, Integer apiVersion) {
requireNonNull(apiId);
if (!API_ID_PATTERN.matcher(apiId).matches()) {
if (!apiId.isEmpty() && !API_ID_PATTERN.matcher(apiId).matches()) {
throw new IllegalArgumentException("API identifier must match pattern [a-z]");
}
this.apiId = requireNonNull(apiId);
Expand Down
@@ -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 molgenis-api/src/main/java/org/molgenis/api/RootApiController.java
@@ -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 molgenis-api/src/main/java/org/molgenis/api/VersionResponse.java
@@ -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);
}
}
@@ -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);
}
}

0 comments on commit 681e992

Please sign in to comment.