Unverified Commit d5279b25 authored by Selvakumar Kamatchinathan's avatar Selvakumar Kamatchinathan Committed by GitHub
Browse files

Merge pull request #6 from Zhicheng-Liu/T2D-213

T2D-213 Add phenotype registry api
parents 4637843c e96d3920
......@@ -23,6 +23,7 @@ import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
import org.springframework.data.rest.core.config.RepositoryRestConfiguration;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurer;
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter;
import uk.ac.ebi.ampt2d.registry.entities.Phenotype;
import uk.ac.ebi.ampt2d.registry.entities.Property;
@Configuration
......@@ -37,6 +38,7 @@ public class SpringDataRestConfiguration {
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.getCorsRegistry().addMapping("/**").allowedMethods("*").allowedOrigins("*");
config.exposeIdsFor(
Phenotype.class,
Property.class
);
}
......
......@@ -78,7 +78,8 @@ public class SwaggerConfiguration {
.apiInfo(getApiInfo())
.pathMapping("/")
.tags(
new Tag("Property Entity", "Property definition")
new Tag("Property Entity", "Property definition"),
new Tag("Phenotype Entity", "Phenotype definition")
)
.genericModelSubstitutes(ResponseEntity.class)
.alternateTypeRules(getSubstitutionRules())
......
/*
*
* Copyright 2018 EMBL - European Bioinformatics Institute
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package uk.ac.ebi.ampt2d.registry.entities;
import com.fasterxml.jackson.annotation.JsonProperty;
import io.swagger.annotations.ApiModelProperty;
import org.springframework.data.annotation.CreatedDate;
import org.springframework.data.annotation.LastModifiedDate;
import org.springframework.data.jpa.domain.support.AuditingEntityListener;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.EntityListeners;
import javax.persistence.EnumType;
import javax.persistence.Enumerated;
import javax.persistence.Id;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.time.ZonedDateTime;
@Entity
@EntityListeners(AuditingEntityListener.class)
public class Phenotype {
public enum Group {
ANTHROPOMETRIC,
CARDIOVASCULAR,
GLYCEMIC,
LIPIDS,
RENAL,
HEPATIC
}
@ApiModelProperty(position = 1)
@JsonProperty
@NotNull
@Size(min = 1, max = 255)
@Id
@Column(nullable = false, unique = true, updatable = false)
private String id;
@ApiModelProperty(position = 3)
@JsonProperty
@NotNull
@Enumerated(EnumType.STRING)
@Column(nullable = false)
private Group phenotypeGroup;
@CreatedDate
@Column(updatable = false)
private ZonedDateTime createdDate;
@LastModifiedDate
private ZonedDateTime lastModifiedDate;
}
/*
*
* Copyright 2018 EMBL - European Bioinformatics Institute
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
package uk.ac.ebi.ampt2d.registry.repositories;
import org.springframework.data.repository.CrudRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import uk.ac.ebi.ampt2d.registry.entities.Phenotype;
import java.util.List;
@RepositoryRestResource
public interface PhenotypeRepository extends CrudRepository<Phenotype, String> {
List<Phenotype> findByPhenotypeGroup(@Param("phenotypeGroup") Phenotype.Group group);
}
......@@ -26,15 +26,14 @@ import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.MvcResult;
import uk.ac.ebi.ampt2d.registry.repositories.PhenotypeRepository;
import uk.ac.ebi.ampt2d.registry.repositories.PropertyRepository;
import static org.hamcrest.Matchers.containsString;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.patch;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.header;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
......@@ -46,11 +45,15 @@ public class PropertyRegistryServiceApplicationTests {
@Autowired
private MockMvc mockMvc;
@Autowired
private PhenotypeRepository phenotypeRepository;
@Autowired
private PropertyRepository propertyRepository;
@Before
public void deleteAllBeforeTests() throws Exception {
phenotypeRepository.deleteAll();
propertyRepository.deleteAll();
}
......@@ -58,29 +61,108 @@ public class PropertyRegistryServiceApplicationTests {
public void shouldReturnRepositoryIndex() throws Exception {
mockMvc.perform(get("/")).andExpect(status().isOk()).andExpect(
jsonPath("$._links.properties").exists());
mockMvc.perform(get("/")).andExpect(status().isOk()).andExpect(
jsonPath("$._links.phenotypes").exists());
}
private String postTestEntity(String uri, String content) throws Exception {
MvcResult mvcResult = mockMvc.perform(post(uri).content(content))
.andExpect(status().isCreated())
.andReturn();
return mvcResult.getResponse().getHeader("Location");
}
private String postTestPhenotype() throws Exception {
String content = "{\"id\":\"BMI\"," + "\"phenotypeGroup\":\"ANTHROPOMETRIC\"}";
return postTestEntity("/phenotypes", content);
}
@Test
public void shouldCreateEntity() throws Exception {
mockMvc.perform(post("/properties").content(
"{\"id\":\"CALL_RATE\"," +
"\"type\":\"FLOAT\"," +
"\"meaning\":\"CALL_RATE\"," +
"\"description\":\"calling rate\"}")).andExpect(
status().isCreated()).andExpect(
header().string("Location", containsString("properties/")));
public void shouldCreatePhenotype() throws Exception {
String location = postTestPhenotype();
assert location.contains("/phenotypes/");
}
@Test
public void shouldRetrieveEntity() throws Exception {
MvcResult mvcResult = mockMvc.perform(post("/properties").content(
"{\"id\":\"CALL_RATE\"," +
"\"type\":\"FLOAT\"," +
"\"meaning\":\"CALL_RATE\"," +
"\"description\":\"calling rate\"}")).andExpect(
status().isCreated()).andReturn();
public void shouldRetrievePhenotype() throws Exception {
String location = postTestPhenotype();
mockMvc.perform(get(location))
.andExpect(status().isOk())
.andExpect(jsonPath("$.phenotypeGroup").value("ANTHROPOMETRIC"));
}
@Test
public void shouldQueryPhenotype() throws Exception {
String location = postTestPhenotype();
mockMvc.perform(get("/phenotypes/search/findByPhenotypeGroup?phenotypeGroup=ANTHROPOMETRIC"))
.andExpect(status().isOk())
.andExpect(jsonPath("$..phenotypes").isArray())
.andExpect(jsonPath("$..phenotypes.length()").value(1))
.andExpect(jsonPath("$..phenotypes[0]..phenotype.href").value(location));
}
@Test
public void shouldUpdatePhenotype() throws Exception {
String location = postTestPhenotype();
mockMvc.perform(put(location)
.content("{\"id\":\"BMI\"," + "\"phenotypeGroup\":\"RENAL\"}"))
.andExpect(status().is2xxSuccessful());
mockMvc.perform(get(location))
.andExpect(status().isOk())
.andExpect(jsonPath("$.phenotypeGroup").value("RENAL"));
}
@Test
public void shouldPartiallyUpdatePhenotype() throws Exception {
String location = postTestPhenotype();
mockMvc.perform(patch(location)
.content("{\"phenotypeGroup\":\"RENAL\"}"))
.andExpect(status().is2xxSuccessful());
mockMvc.perform(get(location))
.andExpect(status().isOk())
.andExpect(jsonPath("$.phenotypeGroup").value("RENAL"));
}
@Test
public void shouldDeletePhenotype() throws Exception {
String location = postTestPhenotype();
mockMvc.perform(delete(location))
.andExpect(status().is2xxSuccessful());
mockMvc.perform(get(location))
.andExpect(status().isNotFound());
}
private String postTestProperty() throws Exception {
String content = "{\"id\":\"CALL_RATE\"," +
"\"type\":\"FLOAT\"," +
"\"meaning\":\"CALL_RATE\"," +
"\"description\":\"calling rate\"}";
return postTestEntity("/properties", content);
}
@Test
public void shouldCreateProperty() throws Exception {
String location = postTestProperty();
assert location.contains("/properties/");
}
@Test
public void shouldRetrieveProperty() throws Exception {
String location = postTestProperty();
String location = mvcResult.getResponse().getHeader("Location");
mockMvc.perform(get(location)).andExpect(status().isOk()).andExpect(
jsonPath("$.type").value("FLOAT")).andExpect(
jsonPath("$.meaning").value("CALL_RATE")).andExpect(
......@@ -88,13 +170,8 @@ public class PropertyRegistryServiceApplicationTests {
}
@Test
public void shouldQueryEntity() throws Exception {
mockMvc.perform(post("/properties").content(
"{\"id\":\"CALL_RATE\"," +
"\"type\":\"FLOAT\"," +
"\"meaning\":\"CALL_RATE\"," +
"\"description\":\"calling rate\"}")).andExpect(
status().isCreated());
public void shouldQueryProperties() throws Exception {
postTestProperty();
mockMvc.perform(
get("/properties/search/findByType?type={type}", "FLOAT")).andExpect(
......@@ -105,15 +182,8 @@ public class PropertyRegistryServiceApplicationTests {
}
@Test
public void shouldUpdateEntity() throws Exception {
MvcResult mvcResult = mockMvc.perform(post("/properties").content(
"{\"id\":\"CALL_RATE\"," +
"\"type\":\"FLOAT\"," +
"\"meaning\":\"CALL_RATE\"," +
"\"description\":\"calling rate\"}")).andExpect(
status().isCreated()).andReturn();
String location = mvcResult.getResponse().getHeader("Location");
public void shouldUpdateProperty() throws Exception {
String location = postTestProperty();
mockMvc.perform(put(location).content(
"{\"id\":\"CALL_RATE\"," +
......@@ -129,15 +199,8 @@ public class PropertyRegistryServiceApplicationTests {
}
@Test
public void shouldPartiallyUpdateEntity() throws Exception {
MvcResult mvcResult = mockMvc.perform(post("/properties").content(
"{\"id\":\"CALL_RATE\"," +
"\"type\":\"FLOAT\"," +
"\"meaning\":\"CALL_RATE\"," +
"\"description\":\"calling rate\"}")).andExpect(
status().isCreated()).andReturn();
String location = mvcResult.getResponse().getHeader("Location");
public void shouldPartiallyUpdateProperty() throws Exception {
String location = postTestProperty();
mockMvc.perform(
patch(location).content("{\"type\": \"DOUBLE\"}")).andExpect(
......@@ -150,18 +213,12 @@ public class PropertyRegistryServiceApplicationTests {
}
@Test
public void shouldDeleteEntity() throws Exception {
MvcResult mvcResult = mockMvc.perform(post("/properties").content(
"{\"id\":\"CALL_RATE\"," +
"\"type\":\"FLOAT\"," +
"\"meaning\":\"CALL_RATE\"," +
"\"description\":\"calling rate\"}")).andExpect(
status().isCreated()).andReturn();
String location = mvcResult.getResponse().getHeader("Location");
public void shouldDeleteProperty() throws Exception {
String location = postTestProperty();
mockMvc.perform(delete(location)).andExpect(status().isNoContent());
mockMvc.perform(get(location)).andExpect(status().isNotFound());
}
}
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment