Handled invalid city ID.
This commit is contained in:
parent
106accdcf5
commit
b8539a7295
2 changed files with 19 additions and 6 deletions
|
@ -44,6 +44,7 @@ import net.thauvin.erik.mobibot.msg.ErrorMessage;
|
||||||
import net.thauvin.erik.mobibot.msg.Message;
|
import net.thauvin.erik.mobibot.msg.Message;
|
||||||
import net.thauvin.erik.mobibot.msg.NoticeMessage;
|
import net.thauvin.erik.mobibot.msg.NoticeMessage;
|
||||||
import net.thauvin.erik.mobibot.msg.PublicMessage;
|
import net.thauvin.erik.mobibot.msg.PublicMessage;
|
||||||
|
import okhttp3.HttpUrl;
|
||||||
import org.jibble.pircbot.Colors;
|
import org.jibble.pircbot.Colors;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
@ -167,10 +168,20 @@ public class Weather2 extends ThreadedModule {
|
||||||
messages.add(new NoticeMessage(condition.toString()));
|
messages.add(new NoticeMessage(condition.toString()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
messages.add(new NoticeMessage("https://openweathermap.org/city/"
|
|
||||||
+ cwd.getCityId(), Colors.GREEN));
|
|
||||||
}
|
|
||||||
|
|
||||||
|
if (cwd.getCityId() != null) {
|
||||||
|
if (cwd.getCityId() > 0) {
|
||||||
|
messages.add(new NoticeMessage("https://openweathermap.org/city/" + cwd.getCityId(),
|
||||||
|
Colors.GREEN));
|
||||||
|
} else {
|
||||||
|
final HttpUrl url =
|
||||||
|
HttpUrl.parse("https://openweathermap.org/find").newBuilder().addQueryParameter(
|
||||||
|
"q", city).build();
|
||||||
|
messages.add(
|
||||||
|
new NoticeMessage(url.toString(), Colors.GREEN));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (APIException | NullPointerException e) {
|
} catch (APIException | NullPointerException e) {
|
||||||
throw new ModuleException("getWeather(" + query + ')', "Unable to perform weather lookup.", e);
|
throw new ModuleException("getWeather(" + query + ')', "Unable to perform weather lookup.", e);
|
||||||
}
|
}
|
||||||
|
|
|
@ -51,11 +51,13 @@ public class Weather2Test extends LocalProperties {
|
||||||
@SuppressFBWarnings("PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS")
|
@SuppressFBWarnings("PRMC_POSSIBLY_REDUNDANT_METHOD_CALLS")
|
||||||
@Test
|
@Test
|
||||||
public void testWeather() throws ModuleException {
|
public void testWeather() throws ModuleException {
|
||||||
List<Message> messages = Weather2.getWeather("98204",
|
List<Message> messages = Weather2.getWeather("98204", LocalProperties.getProperty(Weather2.OWM_API_KEY_PROP));
|
||||||
LocalProperties.getProperty(Weather2.OWM_API_KEY_PROP));
|
|
||||||
assertThat(messages.get(0).getMessage()).as("is Everett").contains("Everett");
|
assertThat(messages.get(0).getMessage()).as("is Everett").contains("Everett");
|
||||||
|
assertThat(messages.get(messages.size() - 1).getMessage()).as("is City Search").endsWith("98204");
|
||||||
|
|
||||||
messages = Weather2.getWeather("London, UK", LocalProperties.getProperty(Weather2.OWM_API_KEY_PROP));
|
messages = Weather2.getWeather("London, UK", LocalProperties.getProperty(Weather2.OWM_API_KEY_PROP));
|
||||||
assertThat(messages.get(0).getMessage()).as("is UK").contains("UK");
|
assertThat(messages.get(0).getMessage()).as("is UK").contains("UK");
|
||||||
|
assertThat(messages.get(messages.size() - 1).getMessage()).as("is City Code").endsWith("4298960");
|
||||||
|
|
||||||
try {
|
try {
|
||||||
Weather2.getWeather("test", "");
|
Weather2.getWeather("test", "");
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue