Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

api #18

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open

api #18

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package com.indramakers.example.measuresms.api.clients;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import org.springframework.web.util.UriComponentsBuilder;

@Component
public class StormGlass {

@Autowired
private RestTemplate restTemplate;

public StormGlassWeather getWeather(Double lat, Double lon) {
HttpHeaders headers = new HttpHeaders();
headers.set("Authorization", "ad86e998-e23c-11ec-ab6b-0242ac130002-ad86ea10-e23c-11ec-ab6b-0242ac130002");
HttpEntity<String> entity = new HttpEntity<>(null, headers);

UriComponentsBuilder uri = UriComponentsBuilder.fromUriString("https://api.stormglass.io/v2/weather/point")
.queryParam("lat", lat.toString())
.queryParam("lng", lon.toString())
.queryParam("params", "airTemperature");

System.out.println(uri.toUriString());
ResponseEntity<StormGlassWeather> result = restTemplate.exchange(
uri.toUriString(),
HttpMethod.GET,
entity,
StormGlassWeather.class);

return result.getBody();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.indramakers.example.measuresms.api.clients;

import com.fasterxml.jackson.annotation.JsonProperty;

import java.util.List;


public class StormGlassWeather {
public static class AirTemperature {
private String dwd;

public AirTemperature() {
}

public AirTemperature(String dwd) {
this.dwd = dwd;
}

public String getDwd() {
return dwd;
}

public void setDwd(String dwd) {
this.dwd = dwd;
}
}
public static class Hours {
private String time;
private AirTemperature airTemperature;

public Hours(String time, AirTemperature airTemperature) {
this.time = time;
this.airTemperature = airTemperature;
}

public Hours() {
}

public String getTime() {
return time;
}

public void setTime(String time) {
this.time = time;
}

public AirTemperature getAirTemperature() {
return airTemperature;
}

public void setAirTemperature(AirTemperature airTemperature) {
this.airTemperature = airTemperature;
}
}

@JsonProperty("hours")
private List<Hours> data;

public StormGlassWeather() {
}

public StormGlassWeather(List<Hours> data) {
this.data = data;
}

public List<Hours> getData() {
return data;
}

public void setData(List<Hours> data) {
this.data = data;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.indramakers.example.measuresms.api.model;

public class Weather {

private Double lat;
private Double lon;
private Double temperature;

public Weather() {
}

public Weather(Double lat, Double lon, Double temperature) {
this.lat = lat;
this.lon = lon;
this.temperature = temperature;
}

public Double getLat() {
return lat;
}

public void setLat(Double lat) {
this.lat = lat;
}

public Double getLon() {
return lon;
}

public void setLon(Double lon) {
this.lon = lon;
}

public Double getTemperature() {
return temperature;
}

public void setTemperature(Double temperature) {
this.temperature = temperature;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.indramakers.example.measuresms.api.services;

import com.indramakers.example.measuresms.api.clients.StormGlass;
import com.indramakers.example.measuresms.api.clients.StormGlassWeather;
import com.indramakers.example.measuresms.api.model.Weather;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class GetWeatherService {

@Autowired
private StormGlass weatherClient;

public Weather getWeather(Double lat, Double lon) {
StormGlassWeather weather = weatherClient.getWeather(lat, lon);
StormGlassWeather.AirTemperature airTemperature = weather.getData().get(0).getAirTemperature();

return new Weather(lat, lon, Double.valueOf(airTemperature.getDwd()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package com.indramakers.example.measuresms.config;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;

@Configuration
public class RestTemplateConfig {

@Autowired
private RestTemplateBuilder restTemplateBuilder;

@Bean
public RestTemplate buildRestemplate() {
return restTemplateBuilder.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import com.indramakers.example.measuresms.model.requests.MeasureValueRequest;
import com.indramakers.example.measuresms.model.responses.ListMEasuresResponses;
import com.indramakers.example.measuresms.model.responses.MeasureSummaryResponse;
import com.indramakers.example.measuresms.services.ClimaService;
import com.indramakers.example.measuresms.services.DeviceService;
import com.indramakers.example.measuresms.services.MeasureService;
import org.springframework.beans.factory.annotation.Autowired;
Expand All @@ -26,6 +27,9 @@ public class DeviceController {
@Autowired
private MeasureService measureService;

@Autowired
private ClimaService climaService;

/**
* URL /devices
*
Expand Down Expand Up @@ -92,4 +96,10 @@ public MeasureSummaryResponse getAllSummary() {
return measureService.getAllSummary();
}


@GetMapping("/clima")
public String isCaliente(@RequestParam Double lat, @RequestParam Double lon) {
return climaService.isColdOrHot(lat, lon);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ public ErrorResponse handleNotFoundException(MethodArgumentNotValidException exc
@ResponseBody
@ExceptionHandler(Exception.class)
public ErrorResponse handleException(Exception exception) {
exception.printStackTrace();
return new ErrorResponse("500", exception.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package com.indramakers.example.measuresms.services;

import com.indramakers.example.measuresms.api.model.Weather;
import com.indramakers.example.measuresms.api.services.GetWeatherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class ClimaService {

@Autowired
private GetWeatherService weatherService;

public String isColdOrHot(Double lat, Double lon) {

Weather clima = weatherService.getWeather(lat, lon);

if (clima.getTemperature()>25) return "calido";
else return "frio";
}
}
4 changes: 2 additions & 2 deletions src/main/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
spring.datasource.url=jdbc:postgresql://${DB_HOST:localhost}:${DB_PORT:5432}/${DB_NAME:test}
spring.datasource.url=jdbc:postgresql://${DB_HOST:localhost}:${DB_PORT:5433}/${DB_NAME:test}
#spring.datasource.url=${DB_URI:jdbc:postgresql://localhost:432/test}
spring.datasource.username=${DB_USER:postgres}
spring.datasource.password=${DB_PASS:postgres}
spring.datasource.driverClassName=org.postgresql.Driver

#URL BASe del servicio
server.servlet.context-path=/api/measures-ms
server.port=${PORT:8080}
server.port=${PORT:8081}
spring.flyway.enabled=true
spring.flyway.locations=classpath:/db/migration
spring.mvc.pathmatch.matching-strategy=ant-path-matcher
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,34 @@

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.indramakers.example.measuresms.api.clients.StormGlassWeather;
import com.indramakers.example.measuresms.config.Routes;
import com.indramakers.example.measuresms.model.entities.Device;
import com.indramakers.example.measuresms.model.entities.Measure;
import com.indramakers.example.measuresms.model.responses.ErrorResponse;
import com.indramakers.example.measuresms.repositories.IDevicesRepository;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.jdbc.Sql;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockHttpServletRequestBuilder;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.web.client.RestTemplate;

import javax.transaction.Transactional;
import java.util.List;
Expand All @@ -43,6 +52,9 @@ public class DeviceControllerTest {
@Autowired
private JdbcTemplate jdbcTemplate;

@MockBean
private RestTemplate restTemplate;

@Test
public void createDeviceHappyPath() throws Exception {
MockHttpServletRequestBuilder request = MockMvcRequestBuilders
Expand Down Expand Up @@ -170,5 +182,53 @@ public void addMeasureToDeviceWhenDeviceisNotFound() throws Exception {
}


@Test
public void testCAlor() throws Exception {
StormGlassWeather mockedResponse = new StormGlassWeather(List.of(new StormGlassWeather.Hours("2020-01-01", new StormGlassWeather.AirTemperature("30"))));
ResponseEntity res = new ResponseEntity<StormGlassWeather>(mockedResponse, HttpStatus.OK);

Mockito.when(restTemplate.exchange(
Mockito.anyString(),
Mockito.any(HttpMethod.class),
Mockito.any(),
Mockito.<Class<StormGlassWeather>>any()))
.thenReturn(res);

MockHttpServletRequestBuilder request = MockMvcRequestBuilders
.get("/devices/clima?lat=58.7984&lon=17.8081")
.contentType(MediaType.APPLICATION_JSON);

MockHttpServletResponse response = mockMvc.perform(request).andReturn().getResponse();
//------------ las verificaciones--------------------
Assertions.assertEquals(200, response.getStatus());

String nodes = (response.getContentAsString());
Assertions.assertEquals("calido", nodes);
}

@Test
public void testFrio() throws Exception {
StormGlassWeather mockedResponse = new StormGlassWeather(List.of(new StormGlassWeather.Hours("2020-01-01",
new StormGlassWeather.AirTemperature("10"))));
ResponseEntity res = new ResponseEntity<StormGlassWeather>(mockedResponse, HttpStatus.OK);

Mockito.when(restTemplate.exchange(
Mockito.anyString(),
Mockito.any(HttpMethod.class),
Mockito.any(),
Mockito.<Class<StormGlassWeather>>any()))
.thenReturn(res);

MockHttpServletRequestBuilder request = MockMvcRequestBuilders
.get("/devices/clima?lat=58.7984&lon=17.8081")
.contentType(MediaType.APPLICATION_JSON);

MockHttpServletResponse response = mockMvc.perform(request).andReturn().getResponse();
//------------ las verificaciones--------------------
Assertions.assertEquals(200, response.getStatus());

String nodes = (response.getContentAsString());
Assertions.assertEquals("frio", nodes);
}

}
3 changes: 2 additions & 1 deletion src/test/resources/application.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
spring.datasource.url=jdbc:postgresql://localhost:5432/test
spring.datasource.url=jdbc:postgresql://localhost:5433/test
spring.datasource.username=postgres
spring.datasource.password=postgres
spring.datasource.driverClassName=org.postgresql.Driver
Expand All @@ -8,3 +8,4 @@ server.servlet.context-path=/api/measures-ms
server.port=8081
spring.flyway.enabled=true
spring.flyway.locations=classpath:/db/migration
spring.mvc.pathmatch.matching-strategy=ant-path-matcher