Commit 3c83fa7f authored by Maxym Borodenko's avatar Maxym Borodenko

Added ClimateDataController v1

parent 422a67ba
/*
* Copyright 2019 Global Crop Diversity Trust
*
* 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 org.genesys2.server.api.v1;
import io.swagger.annotations.Api;
import org.genesys.worldclim.WorldClimUtil;
import org.genesys2.server.api.ApiBaseController;
import org.genesys2.server.exception.NotFoundElement;
import org.genesys2.server.model.impl.TileClimate;
import org.genesys2.server.service.ClimateDataService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController("climateApi1")
@PreAuthorize("isAuthenticated()")
@RequestMapping(ClimateDataController.API_BASE)
@Api(tags = { "climate" })
public class ClimateDataController {
/** The Constant API_BASE. */
public static final String API_BASE = ApiBaseController.APIv1_BASE + "/climate";
/** The Constant LOG. */
private static final Logger LOG = LoggerFactory.getLogger(ClimateDataController.class);
@Autowired
private ClimateDataService climateDataService;
@GetMapping("/current/{latitude},{longitude}")
public TileClimate getCurrentClimate(@PathVariable("latitude") double latitude, @PathVariable("longitude") double longitude) {
Long tileIndex = WorldClimUtil.getWorldclim25Tile(longitude, latitude);
if (tileIndex == null) {
throw new NotFoundElement("No current climate data for specified (lat, lon)");
}
return climateDataService.climateForTile(tileIndex);
}
@GetMapping("/past/{latitude},{longitude}")
public TileClimate getPastClimate(@PathVariable("latitude") double latitude, @PathVariable("longitude") double longitude) {
throw new NotFoundElement("Past climate data not available");
}
@GetMapping("/future/{latitude},{longitude}")
public TileClimate getFutureClimate(@PathVariable("latitude") double latitude, @PathVariable("longitude") double longitude) {
throw new NotFoundElement("Future climate data not available");
}
}
/*
* Copyright 2019 Global Crop Diversity Trust
*
* 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 org.genesys.test.server.api.v1;
import static org.hamcrest.Matchers.*;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import org.genesys.test.base.AbstractApiTest;
import org.genesys.worldclim.WorldClimUtil;
import org.genesys2.server.api.v1.ClimateDataController;
import org.genesys2.server.model.impl.TileClimate;
import org.genesys2.server.persistence.TileClimateRepository;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.transaction.annotation.Transactional;
/**
* @author Maxym Borodenko
*/
public class ClimateDataControllerTest extends AbstractApiTest {
private static final Double LONGITUDE = 32.85;
private static final Double LATITUDE = 39.61666667;
private static final Long TILE_INDEX = WorldClimUtil.getWorldclim25Tile(LONGITUDE, LATITUDE);
@Autowired
private TileClimateRepository tileClimateRepository;
@After
@Override
@Transactional
public void cleanup() throws Exception {
tileClimateRepository.deleteAll();
super.cleanup();
}
@Before
@Override
@Transactional
public void beforeTest() throws Exception {
super.beforeTest();
tileClimateRepository.save(new TileClimate(TILE_INDEX));
}
@Test
public void getCurrentClimateTest() throws Exception {
/*@formatter:off*/
mockMvc
.perform(get(ClimateDataController.API_BASE.concat("/current/{latitude},{longitude}"), LATITUDE, LONGITUDE)
.contentType(MediaType.APPLICATION_JSON))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8))
.andExpect(jsonPath("$.tileIndex", is(TILE_INDEX.intValue())));
/*@formatter:on*/
}
@Test
public void getPastClimateTestFail() throws Exception {
/*@formatter:off*/
mockMvc
.perform(get(ClimateDataController.API_BASE.concat("/past/{latitude},{longitude}"), LATITUDE, LONGITUDE)
.contentType(MediaType.APPLICATION_JSON))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isNotFound())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
/*@formatter:on*/
}
@Test
public void getFutureClimateTestFail() throws Exception {
/*@formatter:off*/
mockMvc
.perform(get(ClimateDataController.API_BASE.concat("/future/{latitude},{longitude}"), LATITUDE, LONGITUDE)
.contentType(MediaType.APPLICATION_JSON))
// .andDo(MockMvcResultHandlers.print())
.andExpect(status().isNotFound())
.andExpect(content().contentType(MediaType.APPLICATION_JSON_UTF8));
/*@formatter:on*/
}
}
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