current_weather and weather_forecast method tests successfull

api_setup
Selim Eser 2024-06-09 04:45:31 +02:00
parent 42e7cc5daa
commit 3f3fcaefbd
2 changed files with 40 additions and 8 deletions

View File

@ -12,13 +12,17 @@ import org.json.JSONObject;
public class System { public class System {
private User current_user; private User current_user = new User();
private String api_key; private String api_key;
public System(String api_key) { public System(String api_key) {
this.api_key = api_key; this.api_key = api_key;
} }
public void set_current_user_zip(String zip){
this.current_user.setZip(Integer.parseInt(zip));
}
public HashSet<User> get_all_user(){ public HashSet<User> get_all_user(){
return new HashSet<User>(); return new HashSet<User>();
} }
@ -54,19 +58,19 @@ public class System {
public String current_weather(){ public String current_weather(){
String weather = ""; String weather = "";
double temperature = 1; double temperature = 1;
try{ try{
HttpClient http_client = HttpClient.newHttpClient(); HttpClient http_client = HttpClient.newHttpClient();
HttpRequest get_request = HttpRequest.newBuilder() HttpRequest get_request = HttpRequest.newBuilder()
.uri(new URI("https://api.openweathermap.org/data/2.5/weather?zip="+current_user.getZip()+",de&appid="+api_key+"&units=metric&lang=de)")) .uri(new URI("https://api.openweathermap.org/data/2.5/weather?zip="+current_user.getZip()+",de&appid="+api_key+"&units=metric&lang=de"))
.GET() .GET()
.build(); .build();
HttpResponse get_response = http_client.send(get_request, BodyHandlers.ofString()); HttpResponse get_response = http_client.send(get_request, BodyHandlers.ofString());
JSONObject json = new JSONObject(get_response.body()); JSONObject json = new JSONObject(((String)get_response.body()).substring(0,((String) get_response.body()).length()));
weather = json.getJSONArray("weather").getJSONObject(0).getString("description"); weather = json.getJSONArray("weather").getJSONObject(0).getString("description");
temperature = json.getJSONObject("main").getDouble("temp"); temperature = json.getJSONObject("main").getDouble("temp");
} catch (Exception e){} } catch (Exception e){}
@ -116,13 +120,13 @@ public class System {
HttpClient http_client = HttpClient.newHttpClient(); HttpClient http_client = HttpClient.newHttpClient();
HttpRequest get_request = HttpRequest.newBuilder() HttpRequest get_request = HttpRequest.newBuilder()
.uri(new URI("https://api.openweathermap.org/data/2.5/forecast?zip="+destination_zip+",de&appid="+api_key+"&units=metric&lang=de)")) .uri(new URI("https://api.openweathermap.org/data/2.5/forecast?zip="+destination_zip+",de&appid="+api_key+"&units=metric&lang=de"))
.GET() .GET()
.build(); .build();
HttpResponse get_response = http_client.send(get_request, BodyHandlers.ofString()); HttpResponse get_response = http_client.send(get_request, BodyHandlers.ofString());
JSONObject json = new JSONObject(get_response.body()); JSONObject json = new JSONObject(((String)get_response.body()).substring(0,((String) get_response.body()).length()));
weather_day_1 = json.getJSONArray("list").getJSONObject(11).getJSONArray("weather").getJSONObject(0).getString("description"); weather_day_1 = json.getJSONArray("list").getJSONObject(11).getJSONArray("weather").getJSONObject(0).getString("description");
weather_day_2 = json.getJSONArray("list").getJSONObject(19).getJSONArray("weather").getJSONObject(0).getString("description"); weather_day_2 = json.getJSONArray("list").getJSONObject(19).getJSONArray("weather").getJSONObject(0).getString("description");
weather_day_3 = json.getJSONArray("list").getJSONObject(27).getJSONArray("weather").getJSONObject(0).getString("description"); weather_day_3 = json.getJSONArray("list").getJSONObject(27).getJSONArray("weather").getJSONObject(0).getString("description");

View File

@ -0,0 +1,28 @@
package de.hs_mannheim.domain;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import org.junit.jupiter.api.Test;
public class SystemTest {
@Test
public void weather_forecast(){
System current_system = new System("35a75437476f12302f72e55d368485db");
assertNotEquals("Es ist ein Fehler aufgetreten!",current_system.weather_forecast("68161"));
}
@Test
public void current_weather(){
System current_system = new System("35a75437476f12302f72e55d368485db");
current_system.set_current_user_zip("68161");
assertNotEquals("Es ist ein Fehler aufgetreten!",current_system.current_weather());
}
}