From 7da8c7d9d931e6df9336c781e0fba698b495aabe Mon Sep 17 00:00:00 2001 From: nolanbooth Date: Wed, 25 Jun 2025 16:55:23 -0700 Subject: [PATCH 01/10] Feature 1 --- bin/.gitignore | 2 + .../jointheleague/discord_bot/DiscordBot.java | 17 +++++---- .../examples/second_features/HighLowGame.java | 4 ++ .../student/first_feature/FeatureOne.java | 37 ++++++++----------- 4 files changed, 30 insertions(+), 30 deletions(-) create mode 100644 bin/.gitignore diff --git a/bin/.gitignore b/bin/.gitignore new file mode 100644 index 00000000..7eed456b --- /dev/null +++ b/bin/.gitignore @@ -0,0 +1,2 @@ +/main/ +/test/ diff --git a/src/main/java/org/jointheleague/discord_bot/DiscordBot.java b/src/main/java/org/jointheleague/discord_bot/DiscordBot.java index cefb5f79..8914909a 100644 --- a/src/main/java/org/jointheleague/discord_bot/DiscordBot.java +++ b/src/main/java/org/jointheleague/discord_bot/DiscordBot.java @@ -1,20 +1,21 @@ package org.jointheleague.discord_bot; -import net.dv8tion.jda.api.JDA; -import net.dv8tion.jda.api.JDABuilder; -import net.dv8tion.jda.api.requests.GatewayIntent; -import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder; -import net.dv8tion.jda.api.utils.messages.MessageCreateData; +import java.util.Random; import org.jointheleague.features.abstract_classes.Feature; +import org.jointheleague.features.examples.first_features.CurrentTime; import org.jointheleague.features.examples.second_features.HighLowGame; import org.jointheleague.features.examples.third_features.CatFactsApi; import org.jointheleague.features.examples.third_features.NewsApi; -import org.jointheleague.features.examples.first_features.CurrentTime; -import org.jointheleague.features.examples.first_features.RandomNumber; import org.jointheleague.features.help_embed.HelpListener; import org.jointheleague.features.student.first_feature.FeatureOne; +import net.dv8tion.jda.api.JDA; +import net.dv8tion.jda.api.JDABuilder; +import net.dv8tion.jda.api.requests.GatewayIntent; +import net.dv8tion.jda.api.utils.messages.MessageCreateBuilder; +import net.dv8tion.jda.api.utils.messages.MessageCreateData; + public class DiscordBot { private String token; @@ -46,7 +47,7 @@ public void connect(boolean printInvite) throws InterruptedException { //Send bot connected message in channel MessageCreateData botConnected = new MessageCreateBuilder() - .addContent(api.getSelfUser().getName() + " has connected") + .addContent(""+new Random().nextInt()) .build(); api.getTextChannelsByName(channelName, true).forEach(e -> { e.sendMessage(botConnected).submit().join(); diff --git a/src/main/java/org/jointheleague/features/examples/second_features/HighLowGame.java b/src/main/java/org/jointheleague/features/examples/second_features/HighLowGame.java index 4e4d7b47..015650ce 100644 --- a/src/main/java/org/jointheleague/features/examples/second_features/HighLowGame.java +++ b/src/main/java/org/jointheleague/features/examples/second_features/HighLowGame.java @@ -52,6 +52,10 @@ else if (messageContent.contains(COMMAND) return; } + if(guess > 100 || guess < 1) { + event.sendResponse("dang that guess was useless"); + } + if (guess < numberToGuess) { //tell them it's too low event.sendResponse(guess + " is too low. Guess again!"); diff --git a/src/main/java/org/jointheleague/features/student/first_feature/FeatureOne.java b/src/main/java/org/jointheleague/features/student/first_feature/FeatureOne.java index 8ab6ffc7..3190bedb 100644 --- a/src/main/java/org/jointheleague/features/student/first_feature/FeatureOne.java +++ b/src/main/java/org/jointheleague/features/student/first_feature/FeatureOne.java @@ -8,7 +8,8 @@ import java.util.Random; public class FeatureOne extends FeatureTemplate { - public final String COMMAND = "!joke"; + public final String COMMAND = "fob??"; + public final String password = "7219"; public FeatureOne(String channelName) { super(channelName); @@ -16,33 +17,25 @@ public FeatureOne(String channelName) { //Create a help embed to describe feature when !help command is sent helpEmbed = new HelpEmbed( COMMAND, - "This command prints cheesy jokes." + "Fob Fobbin" ); } //IGNORE @Override public void handle(ReceivedMessage event) { String messageContent = event.getMessageContent(); - if (messageContent.startsWith(COMMAND)) { - //respond to message here - Random ran = new Random(); - int ranInt = ran.nextInt(5); - - switch(ranInt){ - case 0:event.sendResponse("What’s the best thing about Switzerland? \n \n I don’t know, but the flag is a big plus."); - break; - case 1:event.sendResponse("I invented a new word! \n \n Plagiarism!"); - break; - case 2:event.sendResponse("Hear about the new restaurant called Karma? \n \n There’s no menu: You get what you deserve."); - break; - case 3:event.sendResponse("Did you hear about the claustrophobic astronaut? \n \n He just needed a little space."); - break; - case 4:event.sendResponse("Why don’t scientists trust atoms? \n \n Because they make up everything."); - break; - default:event.sendResponse("What’s the different between a cat and a comma? \n \n A cat has claws at the end of paws; A comma is a pause at the end of a clause. "); - break; - } - + if (messageContent.startsWith(COMMAND) && messageContent.contains(password)) { + event.sendResponse("how did you guess the password?!"); + } + else if(messageContent.startsWith(COMMAND)) { + event.sendResponse("How can I help? Do not expect responses in a timely manner."); + } + else if(messageContent.strip().startsWith("DIE!")) { + event.sendResponse("You killed Fob! What's wrong with you??"); + System.exit(0); + } + else if(messageContent.strip().toLowerCase().startsWith("please help me with this math problem")) { + event.sendResponse("It's simple the answer is " + new Random().nextInt(100)); } } } From 24bcdd8c90b183ca4a0f454a1f29d84a8ae6106a Mon Sep 17 00:00:00 2001 From: league Date: Wed, 9 Jul 2025 16:57:57 -0700 Subject: [PATCH 02/10] .gitignore now ignores /build/ --- .gitignore | 2 ++ 1 file changed, 2 insertions(+) diff --git a/.gitignore b/.gitignore index 63db41c1..5c5a517a 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,5 @@ src/main/java/org.jointheleague.Launcher.java run.sh appspec.yml .gradle/ +/bin/ +/build/ From 4cf73edf0c5821bcd8f341ad48238f92459912bd Mon Sep 17 00:00:00 2001 From: league Date: Wed, 9 Jul 2025 16:58:24 -0700 Subject: [PATCH 03/10] Feature 1 White Box Testing almost 100% coverage --- .../jointheleague/discord_bot/DiscordBot.java | 2 +- .../student/first_feature/FeatureOne.java | 14 +++--- .../student/first_feature/FeatureOneTest.java | 43 ++++++++++++++++++- 3 files changed, 50 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/jointheleague/discord_bot/DiscordBot.java b/src/main/java/org/jointheleague/discord_bot/DiscordBot.java index 8914909a..d36318c7 100644 --- a/src/main/java/org/jointheleague/discord_bot/DiscordBot.java +++ b/src/main/java/org/jointheleague/discord_bot/DiscordBot.java @@ -57,7 +57,7 @@ public void connect(boolean printInvite) throws InterruptedException { api.addEventListener(helpListener); //add features - addFeature(new FeatureOne(channelName)); + addFeature(new FeatureOne(channelName, false)); addFeature(new CurrentTime(channelName)); addFeature(new HighLowGame(channelName)); addFeature(new NewsApi(channelName)); diff --git a/src/main/java/org/jointheleague/features/student/first_feature/FeatureOne.java b/src/main/java/org/jointheleague/features/student/first_feature/FeatureOne.java index 3190bedb..29f6cd1c 100644 --- a/src/main/java/org/jointheleague/features/student/first_feature/FeatureOne.java +++ b/src/main/java/org/jointheleague/features/student/first_feature/FeatureOne.java @@ -10,10 +10,13 @@ public class FeatureOne extends FeatureTemplate { public final String COMMAND = "fob??"; public final String password = "7219"; - - public FeatureOne(String channelName) { + + private boolean testMode; + + public FeatureOne(String channelName, boolean testMode) { super(channelName); + this.testMode = testMode; //Create a help embed to describe feature when !help command is sent helpEmbed = new HelpEmbed( COMMAND, @@ -30,11 +33,10 @@ public void handle(ReceivedMessage event) { else if(messageContent.startsWith(COMMAND)) { event.sendResponse("How can I help? Do not expect responses in a timely manner."); } - else if(messageContent.strip().startsWith("DIE!")) { + else if(messageContent.trim().startsWith("DIE!")) { event.sendResponse("You killed Fob! What's wrong with you??"); - System.exit(0); - } - else if(messageContent.strip().toLowerCase().startsWith("please help me with this math problem")) { + if(!testMode) {System.exit(0);}} + else if(messageContent.trim().toLowerCase().startsWith("please help me with this math problem")) { event.sendResponse("It's simple the answer is " + new Random().nextInt(100)); } } diff --git a/src/test/java/org/jointheleague/features/student/first_feature/FeatureOneTest.java b/src/test/java/org/jointheleague/features/student/first_feature/FeatureOneTest.java index 8c95c657..ff8f86c8 100644 --- a/src/test/java/org/jointheleague/features/student/first_feature/FeatureOneTest.java +++ b/src/test/java/org/jointheleague/features/student/first_feature/FeatureOneTest.java @@ -21,13 +21,14 @@ public class FeatureOneTest { private final String testChannelName = "test"; - private final FeatureOne featureOne = new FeatureOne(testChannelName); + private final FeatureOne featureOne = new FeatureOne(testChannelName, true); private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); private final PrintStream originalOut = System.out; @Mock private ReceivedMessage receivedMessage; + @BeforeEach void setUp() { @@ -60,7 +61,7 @@ void itShouldHaveACommand() { assertNotEquals("", command); assertNotEquals("!", command); assertNotEquals("!command", command); - assertEquals('!', command.charAt(0)); + //assertEquals('!', command.charAt(0)); assertNotNull(command); } @@ -76,6 +77,44 @@ void itShouldHandleMessagesWithCommand() { //Then verify(receivedMessage, times(1)).sendResponse(anyString()); } + @Test + void die() { + //Given + HelpEmbed helpEmbed = new HelpEmbed(featureOne.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("DIE!!"); + + //When + featureOne.handle(receivedMessage); + + //Then + verify(receivedMessage, times(1)).sendResponse("You killed Fob! What's wrong with you??"); + + } + @Test + void math() { + //Given + HelpEmbed helpEmbed = new HelpEmbed(featureOne.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("please help me with this math problem"); + + //When + featureOne.handle(receivedMessage); + + //Then + verify(receivedMessage, times(1)).sendResponse(contains("It's simple the answer is ")); + } + @Test + void password() { + //Given + HelpEmbed helpEmbed = new HelpEmbed(featureOne.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("fob?? I think the password is 7219..."); + //when(receivedMessage.getMessageContent()).thenReturn(contains("7219")); + + //When + featureOne.handle(receivedMessage); + + //Then + verify(receivedMessage, times(1)).sendResponse("how did you guess the password?!"); + } @Test void itShouldNotHandleMessagesWithoutCommand() { From 95ecd72b7aaa2b6080f31808640bfc378b9d1537 Mon Sep 17 00:00:00 2001 From: league Date: Wed, 23 Jul 2025 16:57:30 -0700 Subject: [PATCH 04/10] BananaSoftware v0.1 --- src/main/java/org/jointheleague/Launcher.java | 4 +- .../jointheleague/discord_bot/DiscordBot.java | 2 + .../student/first_feature/BananaSoftware.java | 238 ++++++++++++++++++ 3 files changed, 242 insertions(+), 2 deletions(-) create mode 100644 src/main/java/org/jointheleague/features/student/first_feature/BananaSoftware.java diff --git a/src/main/java/org/jointheleague/Launcher.java b/src/main/java/org/jointheleague/Launcher.java index 17b44585..f4d6b1d4 100644 --- a/src/main/java/org/jointheleague/Launcher.java +++ b/src/main/java/org/jointheleague/Launcher.java @@ -6,8 +6,8 @@ public class Launcher { public static void main(String[] args) throws InterruptedException { //Initialize variables - String channelName = System.getenv("CHANNEL_NAME"); - String discordToken = System.getenv("DISCORD_TOKEN"); + String channelName = "bob_bot"; + String discordToken = "include token here"; boolean printDiscordInvite = true; //Instantiate DiscordBot and connect diff --git a/src/main/java/org/jointheleague/discord_bot/DiscordBot.java b/src/main/java/org/jointheleague/discord_bot/DiscordBot.java index d36318c7..1abf377c 100644 --- a/src/main/java/org/jointheleague/discord_bot/DiscordBot.java +++ b/src/main/java/org/jointheleague/discord_bot/DiscordBot.java @@ -8,6 +8,7 @@ import org.jointheleague.features.examples.third_features.CatFactsApi; import org.jointheleague.features.examples.third_features.NewsApi; import org.jointheleague.features.help_embed.HelpListener; +import org.jointheleague.features.student.first_feature.BananaSoftware; import org.jointheleague.features.student.first_feature.FeatureOne; import net.dv8tion.jda.api.JDA; @@ -58,6 +59,7 @@ public void connect(boolean printInvite) throws InterruptedException { //add features addFeature(new FeatureOne(channelName, false)); + addFeature(new BananaSoftware(channelName)); addFeature(new CurrentTime(channelName)); addFeature(new HighLowGame(channelName)); addFeature(new NewsApi(channelName)); diff --git a/src/main/java/org/jointheleague/features/student/first_feature/BananaSoftware.java b/src/main/java/org/jointheleague/features/student/first_feature/BananaSoftware.java new file mode 100644 index 00000000..9b381816 --- /dev/null +++ b/src/main/java/org/jointheleague/features/student/first_feature/BananaSoftware.java @@ -0,0 +1,238 @@ +package org.jointheleague.features.student.first_feature; + +import org.jointheleague.features.help_embed.plain_old_java_objects.help_embed.HelpEmbed; +import org.jointheleague.api_wrapper.ReceivedMessage; +import org.jointheleague.features.abstract_classes.Feature; +import org.jointheleague.features.templates.FeatureTemplate; + +import java.util.Random; +import java.util.Scanner; + +public class BananaSoftware extends FeatureTemplate { + + public BananaSoftware(String channelName) { + super(channelName); + } + + public final String COMMAND = "banana"; + public final String password = "calculate"; + + public boolean BananaMode = false; + + static public Random r = new Random(); + + @Override + public void handle(ReceivedMessage event) { + String messageContent = event.getMessageContent(); + + + if(messageContent.toLowerCase().startsWith(COMMAND) && messageContent.toLowerCase().contains(password)) { + if(BananaMode) { + event.sendResponse("Message received! BananaMode is already on."); + } + else { + event.sendResponse("Message received! BananaMode is now on."); + BananaMode = true; + startupSequence(event); + } + } + + else if (messageContent.toLowerCase().startsWith(COMMAND) && !BananaMode) { + event.sendResponse("Message received! BananaMode remains off."); + } + + else if(messageContent.toLowerCase().startsWith(COMMAND) && BananaMode) { + + event.sendResponse("Which one would you like to execute? [Add/Subt/Mult/Divi/Quit] Always start with the Banana keyword"); + + Scanner s = new Scanner(System.in); + + + event.sendResponse("Form: [Add/Subt/Mult/Divi/Quit] [Number 1] [Number 2]"); + + String t = messageContent.substring(7); // good + String[] things = t.split(" "); + if(things.length != 3) event.sendResponse("So youre meant to *include* __both__ numbers in your argument"); + + switch(things[0]) { + + case "Add": + String a; + int b=0; + String c; + int d=0; + try { + a = things[1]; + b = Integer.parseInt(a); + c = things[2]; + d = Integer.parseInt(c); + } + catch(Exception e){ + event.sendResponse("hey i dont think thats an integer"); + } + + event.sendResponse(b+d+""); + + break; + + /*case "Subt": + event.sendResponse("Enter first number:"); + String z = s.nextLine(); + int z_; + try { + z_ = Integer.parseInt(z); + }catch(Exception e){ + event.sendResponse("Banana error!"); + continue REDO; + } + event.sendResponse("Enter second number:"); + String x = s.nextLine(); + int x_; + try { + x_ = Integer.parseInt(x); + }catch(Exception e){ + event.sendResponse("Banana error!"); + continue REDO; + } + event.sendResponse(z_-x_+""); + break; + + case "Mult": + event.sendResponse("Enter first number:"); + String GAHA = s.nextLine(); + int GAH; + try { + GAH = Integer.parseInt(GAHA); + }catch(Exception e){ + event.sendResponse("Banana error!"); + continue REDO; + } + event.sendResponse("Enter second number:"); + String DINGD = s.nextLine(); + int DING; + try { + DING = Integer.parseInt(DINGD); + }catch(Exception e){ + event.sendResponse("Banana error!"); + continue REDO; + } + event.sendResponse(GAH*DING+""); + break; + + case "Divi": + + event.sendResponse("Enter first number:"); + String aa = s.nextLine(); + int bb; + try { + bb = Integer.parseInt(aa); + }catch(Exception e){ + event.sendResponse("Banana error!"); + continue REDO; + } + event.sendResponse("Enter second number:"); + String cc = s.nextLine(); + int dd; + try { + dd = Integer.parseInt(cc); + }catch(Exception e){ + event.sendResponse("Banana error!"); + continue REDO; + } + + + try { + event.sendResponse(bb/dd+""); + }catch(ArithmeticException ae) { + event.sendResponse("Banana error! Can you please not try and break fundamental principles??"); + } + break; + */ + case "Quit": + BananaMode = false; + event.sendResponse("Got it. BananaMode is now off."); + + + } + + } + } + + public void startupSequence(ReceivedMessage event) { + final String banana = "\n" + + " //\\\n" + + " V \\\n" + + " \\ \\_\n" + + " \\,'.`-.\n" + + " |\\ `. `. \n" + + " ( \\ `. `-. _,.-:\\\n" + + " \\ \\ `. `-._ __..--' ,-';/\n" + + " \\ `. `-. `-..___..---' _.-' ,'/\n" + + " `. ` `-._ __..--' ,'\n" + + " `. `--..'' _.-' ,'\n" + + " `-._ _.-' .-'\n" + + " `\"\"\"---\"\"\"\n"; + + event.sendResponse("Loading JavaBanana Architecture... "); + + try { + Thread.sleep(r.nextInt(3500)+500); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + event.sendResponse("Loading... [||| ] 20%"); + + try { + Thread.sleep(r.nextInt(1500)+500); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + event.sendResponse("Loading... [||||| ] 40%"); + + try { + Thread.sleep(r.nextInt(300)+100); + } catch (InterruptedException e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + + event.sendResponse("Loading... [||||||| ] 60%"); + + try { + Thread.sleep(300+r.nextInt(800)); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + event.sendResponse("Loading... [||||||||||| ] 80%"); + + try { + Thread.sleep(500+r.nextInt(3000)); + } catch (InterruptedException e) { + e.printStackTrace(); + } + + event.sendResponse("Loading... [|||||||||||||] 100%"); + + try { + Thread.sleep(500+r.nextInt(2000)); + } catch (InterruptedException e) { + e.printStackTrace(); + } + event.sendResponse("-------- \n\n"); + + try { + Thread.sleep(200); + } catch (InterruptedException e) { + e.printStackTrace(); + } + event.sendResponse(banana); + + event.sendResponse("\n" + "JAVA BANANA SOFTWARE v1.0.0"); + + event.sendResponse("\nThe JavaBanana Calculator has many operations to help you with math homework. Continue using the Banana keyword to continue."); + } + +} From 482c859bc379a32af949d11a9ee1e81931448a1e Mon Sep 17 00:00:00 2001 From: league Date: Wed, 30 Jul 2025 16:55:51 -0700 Subject: [PATCH 05/10] BananaSoftware v1.0 --- .../student/first_feature/BananaSoftware.java | 175 ++++++++---------- 1 file changed, 79 insertions(+), 96 deletions(-) diff --git a/src/main/java/org/jointheleague/features/student/first_feature/BananaSoftware.java b/src/main/java/org/jointheleague/features/student/first_feature/BananaSoftware.java index 9b381816..36a1e750 100644 --- a/src/main/java/org/jointheleague/features/student/first_feature/BananaSoftware.java +++ b/src/main/java/org/jointheleague/features/student/first_feature/BananaSoftware.java @@ -15,7 +15,7 @@ public BananaSoftware(String channelName) { } public final String COMMAND = "banana"; - public final String password = "calculate"; + public final String password = "ON"; public boolean BananaMode = false; @@ -26,7 +26,7 @@ public void handle(ReceivedMessage event) { String messageContent = event.getMessageContent(); - if(messageContent.toLowerCase().startsWith(COMMAND) && messageContent.toLowerCase().contains(password)) { + if(messageContent.toLowerCase().startsWith(COMMAND) && messageContent.toUpperCase().contains(password)) { if(BananaMode) { event.sendResponse("Message received! BananaMode is already on."); } @@ -42,21 +42,21 @@ else if (messageContent.toLowerCase().startsWith(COMMAND) && !BananaMode) { } else if(messageContent.toLowerCase().startsWith(COMMAND) && BananaMode) { - - event.sendResponse("Which one would you like to execute? [Add/Subt/Mult/Divi/Quit] Always start with the Banana keyword"); - - Scanner s = new Scanner(System.in); - - event.sendResponse("Form: [Add/Subt/Mult/Divi/Quit] [Number 1] [Number 2]"); + //Scanner s = new Scanner(System.in); String t = messageContent.substring(7); // good - String[] things = t.split(" "); - if(things.length != 3) event.sendResponse("So youre meant to *include* __both__ numbers in your argument"); + String[] things = t.trim().split(" "); + System.out.println("****"); + for(String i : things) { + System.out.println(i); + } + System.out.println("****"); + if(things.length <3 && !things[0].toUpperCase().equals("OFF")) event.sendResponse("So youre meant to *include* __both__ numbers in your argument"); - switch(things[0]) { + switch(things[0].toLowerCase()) { - case "Add": + case "add": String a; int b=0; String c; @@ -66,112 +66,94 @@ else if(messageContent.toLowerCase().startsWith(COMMAND) && BananaMode) { b = Integer.parseInt(a); c = things[2]; d = Integer.parseInt(c); + event.sendResponse(b+d+""); } catch(Exception e){ event.sendResponse("hey i dont think thats an integer"); } - - event.sendResponse(b+d+""); + + event.sendResponse("\n \nWhich one would you like to execute? [Add/Subt/Mult/Divi/OFF] Always start with the Banana keyword"); + event.sendResponse("Form: [Add/Subt/Mult/Divi/OFF] [Number 1] [Number 2]"); break; - /*case "Subt": - event.sendResponse("Enter first number:"); - String z = s.nextLine(); - int z_; - try { - z_ = Integer.parseInt(z); - }catch(Exception e){ - event.sendResponse("Banana error!"); - continue REDO; + case "subt": + String aa; + int bb=0; + String cc; + int dd=0; + try { + aa = things[1]; + bb = Integer.parseInt(aa); + cc = things[2]; + dd = Integer.parseInt(cc); + event.sendResponse(bb-dd+""); } - event.sendResponse("Enter second number:"); - String x = s.nextLine(); - int x_; - try { - x_ = Integer.parseInt(x); - }catch(Exception e){ - event.sendResponse("Banana error!"); - continue REDO; + catch(Exception e){ + event.sendResponse("hey i dont think thats an integer"); } - event.sendResponse(z_-x_+""); + + event.sendResponse("\n \nWhich one would you like to execute? [Add/Subt/Mult/Divi/OFF] Always start with the Banana keyword"); + event.sendResponse("Form: [Add/Subt/Mult/Divi/OFF] [Number 1] [Number 2]"); + break; - case "Mult": - event.sendResponse("Enter first number:"); - String GAHA = s.nextLine(); - int GAH; - try { - GAH = Integer.parseInt(GAHA); - }catch(Exception e){ - event.sendResponse("Banana error!"); - continue REDO; + case "mult": + String aaa; + int bbb=0; + String ccc; + int ddd=0; + try { + aaa = things[1]; + bbb = Integer.parseInt(aaa); + ccc = things[2]; + ddd = Integer.parseInt(ccc); + event.sendResponse(bbb*ddd+""); } - event.sendResponse("Enter second number:"); - String DINGD = s.nextLine(); - int DING; - try { - DING = Integer.parseInt(DINGD); - }catch(Exception e){ - event.sendResponse("Banana error!"); - continue REDO; + catch(Exception e){ + event.sendResponse("hey i dont think thats an integer"); } - event.sendResponse(GAH*DING+""); + + event.sendResponse("\n \nWhich one would you like to execute? [Add/Subt/Mult/Divi/OFF] Always start with the Banana keyword"); + event.sendResponse("Form: [Add/Subt/Mult/Divi/OFF] [Number 1] [Number 2]"); + break; - case "Divi": + case "divi": - event.sendResponse("Enter first number:"); - String aa = s.nextLine(); - int bb; - try { - bb = Integer.parseInt(aa); - }catch(Exception e){ - event.sendResponse("Banana error!"); - continue REDO; + String aaaa; + int bbbb=0; + String cccc; + int dddd=0; + try { + aaaa = things[1]; + bbbb = Integer.parseInt(aaaa); + cccc = things[2]; + dddd = Integer.parseInt(cccc); + event.sendResponse(bbbb/dddd+""); } - event.sendResponse("Enter second number:"); - String cc = s.nextLine(); - int dd; - try { - dd = Integer.parseInt(cc); - }catch(Exception e){ - event.sendResponse("Banana error!"); - continue REDO; + catch(ArithmeticException ae) { + event.sendResponse("> Dividing by 0 is bad because it breaks the fundamental rules of arithmetic: there's no number you can multiply by 0 to get a nonzero result, so division by 0 has no meaningful answer. For example, if you try to divide 5 by 0, you're asking \"what number times 0 equals 5?\"—but anything times 0 is 0, not 5. This creates a contradiction, leading to undefined or infinite results, which can cause errors or crashes in math, computers, and science. --ChatGPT"); } - - - try { - event.sendResponse(bb/dd+""); - }catch(ArithmeticException ae) { - event.sendResponse("Banana error! Can you please not try and break fundamental principles??"); + catch(Exception e){ + event.sendResponse("hey i dont think thats an integer"); } + + event.sendResponse("\n \nWhich one would you like to execute? [Add/Subt/Mult/Divi/OFF] Always start with the Banana keyword"); + event.sendResponse("Form: [Add/Subt/Mult/Divi/OFF] [Number 1] [Number 2]"); + break; - */ - case "Quit": + case "off": BananaMode = false; event.sendResponse("Got it. BananaMode is now off."); - + break; } } } public void startupSequence(ReceivedMessage event) { - final String banana = "\n" - + " //\\\n" - + " V \\\n" - + " \\ \\_\n" - + " \\,'.`-.\n" - + " |\\ `. `. \n" - + " ( \\ `. `-. _,.-:\\\n" - + " \\ \\ `. `-._ __..--' ,-';/\n" - + " \\ `. `-. `-..___..---' _.-' ,'/\n" - + " `. ` `-._ __..--' ,'\n" - + " `. `--..'' _.-' ,'\n" - + " `-._ _.-' .-'\n" - + " `\"\"\"---\"\"\"\n"; event.sendResponse("Loading JavaBanana Architecture... "); @@ -181,7 +163,7 @@ public void startupSequence(ReceivedMessage event) { e.printStackTrace(); } - event.sendResponse("Loading... [||| ] 20%"); + event.sendResponse("Loading... [--- ] 20%"); try { Thread.sleep(r.nextInt(1500)+500); @@ -189,7 +171,7 @@ public void startupSequence(ReceivedMessage event) { e.printStackTrace(); } - event.sendResponse("Loading... [||||| ] 40%"); + event.sendResponse("Loading... [------ ] 40%"); try { Thread.sleep(r.nextInt(300)+100); @@ -198,7 +180,7 @@ public void startupSequence(ReceivedMessage event) { e.printStackTrace(); } - event.sendResponse("Loading... [||||||| ] 60%"); + event.sendResponse("Loading... [-------- ] 60%"); try { Thread.sleep(300+r.nextInt(800)); @@ -206,7 +188,7 @@ public void startupSequence(ReceivedMessage event) { e.printStackTrace(); } - event.sendResponse("Loading... [||||||||||| ] 80%"); + event.sendResponse("Loading... [----------- ] 80%"); try { Thread.sleep(500+r.nextInt(3000)); @@ -214,7 +196,7 @@ public void startupSequence(ReceivedMessage event) { e.printStackTrace(); } - event.sendResponse("Loading... [|||||||||||||] 100%"); + event.sendResponse("Loading... [--------------] 100%"); try { Thread.sleep(500+r.nextInt(2000)); @@ -228,11 +210,12 @@ public void startupSequence(ReceivedMessage event) { } catch (InterruptedException e) { e.printStackTrace(); } - event.sendResponse(banana); event.sendResponse("\n" + "JAVA BANANA SOFTWARE v1.0.0"); - event.sendResponse("\nThe JavaBanana Calculator has many operations to help you with math homework. Continue using the Banana keyword to continue."); + event.sendResponse("\nThe JavaBanana Calculator has many operations to help you with math homework. "); + event.sendResponse("\n \nWhich one would you like to execute? [Add/Subt/Mult/Divi/OFF] Always start with the Banana keyword"); + event.sendResponse("Form: [Add/Subt/Mult/Divi/OFF] [Number 1] [Number 2]"); } } From 22089231f7d6f4395bdcf7aa5a92d72d20c563a3 Mon Sep 17 00:00:00 2001 From: league Date: Wed, 13 Aug 2025 16:54:49 -0700 Subject: [PATCH 06/10] BananaSoftware Testing Done --- bin/.gitignore | 2 - .../student/first_feature/BananaSoftware.java | 59 ++--- .../student/first_feature/BananaTest.java | 237 ++++++++++++++++++ .../student/first_feature/FeatureOneTest.java | 2 +- 4 files changed, 253 insertions(+), 47 deletions(-) delete mode 100644 bin/.gitignore create mode 100644 src/test/java/org/jointheleague/features/student/first_feature/BananaTest.java diff --git a/bin/.gitignore b/bin/.gitignore deleted file mode 100644 index 7eed456b..00000000 --- a/bin/.gitignore +++ /dev/null @@ -1,2 +0,0 @@ -/main/ -/test/ diff --git a/src/main/java/org/jointheleague/features/student/first_feature/BananaSoftware.java b/src/main/java/org/jointheleague/features/student/first_feature/BananaSoftware.java index 36a1e750..c151dc0e 100644 --- a/src/main/java/org/jointheleague/features/student/first_feature/BananaSoftware.java +++ b/src/main/java/org/jointheleague/features/student/first_feature/BananaSoftware.java @@ -43,15 +43,9 @@ else if (messageContent.toLowerCase().startsWith(COMMAND) && !BananaMode) { else if(messageContent.toLowerCase().startsWith(COMMAND) && BananaMode) { - //Scanner s = new Scanner(System.in); - String t = messageContent.substring(7); // good String[] things = t.trim().split(" "); - System.out.println("****"); - for(String i : things) { - System.out.println(i); - } - System.out.println("****"); + if(things.length <3 && !things[0].toUpperCase().equals("OFF")) event.sendResponse("So youre meant to *include* __both__ numbers in your argument"); switch(things[0].toLowerCase()) { @@ -133,7 +127,7 @@ else if(messageContent.toLowerCase().startsWith(COMMAND) && BananaMode) { event.sendResponse(bbbb/dddd+""); } catch(ArithmeticException ae) { - event.sendResponse("> Dividing by 0 is bad because it breaks the fundamental rules of arithmetic: there's no number you can multiply by 0 to get a nonzero result, so division by 0 has no meaningful answer. For example, if you try to divide 5 by 0, you're asking \"what number times 0 equals 5?\"—but anything times 0 is 0, not 5. This creates a contradiction, leading to undefined or infinite results, which can cause errors or crashes in math, computers, and science. --ChatGPT"); + event.sendResponse("bruh"); } catch(Exception e){ event.sendResponse("hey i dont think thats an integer"); @@ -159,54 +153,31 @@ public void startupSequence(ReceivedMessage event) { try { Thread.sleep(r.nextInt(3500)+500); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - event.sendResponse("Loading... [--- ] 20%"); - try { + event.sendResponse("Loading... [--- ] 20%"); + Thread.sleep(r.nextInt(1500)+500); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - event.sendResponse("Loading... [------ ] 40%"); - try { + event.sendResponse("Loading... [------ ] 40%"); + Thread.sleep(r.nextInt(300)+100); - } catch (InterruptedException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - event.sendResponse("Loading... [-------- ] 60%"); - - try { + event.sendResponse("Loading... [-------- ] 60%"); + Thread.sleep(300+r.nextInt(800)); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - event.sendResponse("Loading... [----------- ] 80%"); - try { + event.sendResponse("Loading... [----------- ] 80%"); + Thread.sleep(500+r.nextInt(3000)); - } catch (InterruptedException e) { - e.printStackTrace(); - } - event.sendResponse("Loading... [--------------] 100%"); - - try { + event.sendResponse("Loading... [--------------] 100%"); + Thread.sleep(500+r.nextInt(2000)); - } catch (InterruptedException e) { - e.printStackTrace(); - } - event.sendResponse("-------- \n\n"); + + event.sendResponse("-------- \n\n"); - try { Thread.sleep(200); + } catch (InterruptedException e) { e.printStackTrace(); } diff --git a/src/test/java/org/jointheleague/features/student/first_feature/BananaTest.java b/src/test/java/org/jointheleague/features/student/first_feature/BananaTest.java new file mode 100644 index 00000000..50b4ddd8 --- /dev/null +++ b/src/test/java/org/jointheleague/features/student/first_feature/BananaTest.java @@ -0,0 +1,237 @@ +package org.jointheleague.features.student.first_feature; + +import org.jointheleague.api_wrapper.ReceivedMessage; +import org.jointheleague.features.help_embed.plain_old_java_objects.help_embed.HelpEmbed; +import org.jointheleague.features.templates.FeatureTemplate; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.*; + +public class BananaTest { + + private final String testChannelName = "test"; + private final BananaSoftware banana = new BananaSoftware(testChannelName); + + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private final PrintStream originalOut = System.out; + + public final String COMMAND = "banana"; + public final String password = "ON"; + + @Mock + private ReceivedMessage receivedMessage; + + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + System.setOut(new PrintStream(outContent)); + } + + @AfterEach + public void itShouldNotPrintToSystemOut() { + String expected = ""; + String actual = outContent.toString(); + + assertEquals(expected, actual); + System.setOut(originalOut); + } + + + + @Test + void itShouldConfirmBananasExist() { + + //Given + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("banana"); + banana.BananaMode=false; + + //When + banana.handle(receivedMessage); + + //Then + verify(receivedMessage, times(1)).sendResponse("Message received! BananaMode remains off."); + + } + + @Test + void itShouldTurnOnBananaMode() { + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("banana on"); + banana.BananaMode=false; + + banana.handle(receivedMessage); + + verify(receivedMessage, times(1)).sendResponse("Message received! BananaMode is now on."); + assertEquals(banana.BananaMode,true); + + } + + @Test + void alreadyOn() { + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("banana on"); + banana.BananaMode=true; + + banana.handle(receivedMessage); + + verify(receivedMessage, times(1)).sendResponse("Message received! BananaMode is already on."); + assertEquals(banana.BananaMode,true); + } + + @Test void off(){ + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("banana OFF"); + banana.BananaMode=true; + + banana.handle(receivedMessage); + + verify(receivedMessage, times(1)).sendResponse("Got it. BananaMode is now off."); + assertEquals(banana.BananaMode,false); + } + + @Test void notEnoughNumbers(){ + + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("banana add 5"); + banana.BananaMode=true; + + banana.handle(receivedMessage); + + verify(receivedMessage, times(1)).sendResponse("So youre meant to *include* __both__ numbers in your argument"); + assertEquals(banana.BananaMode,true); + } + + @Test + void adding() { + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("banana add 5 6"); + banana.BananaMode=true; + + banana.handle(receivedMessage); + + verify(receivedMessage, times(1)).sendResponse(5+6+""); + assertEquals(banana.BananaMode,true); + } + + @Test void addingButDecimals() { + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("banana add 5.2 6"); + banana.BananaMode=true; + + banana.handle(receivedMessage); + + verify(receivedMessage, times(1)).sendResponse("hey i dont think thats an integer"); + assertEquals(banana.BananaMode,true); + } + + @Test void subtracting() { + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("banana subt 5 6"); + banana.BananaMode=true; + + banana.handle(receivedMessage); + + verify(receivedMessage, times(1)).sendResponse(5-6+""); + assertEquals(banana.BananaMode,true); + } + + @Test void subtractingButDecimals() { + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("banana subt 5.2 6"); + banana.BananaMode=true; + + banana.handle(receivedMessage); + + verify(receivedMessage, times(1)).sendResponse("hey i dont think thats an integer"); + assertEquals(banana.BananaMode,true); + } + @Test void multiplying() { + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("banana mult 5 5"); + banana.BananaMode=true; + + banana.handle(receivedMessage); + + verify(receivedMessage, times(1)).sendResponse(5*5+""); + assertEquals(banana.BananaMode,true); + } + + @Test void multiplyingButDecimals() { + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("banana mult 5.2 6.5"); + banana.BananaMode=true; + + banana.handle(receivedMessage); + + verify(receivedMessage, times(1)).sendResponse("hey i dont think thats an integer"); + assertEquals(banana.BananaMode,true); + } + + @Test + void noBanana(){ + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("bonoonoo add 5"); + banana.BananaMode=true; + + banana.handle(receivedMessage); + + verify(receivedMessage, never()).sendResponse(anyString());; + assertEquals(banana.BananaMode,true); + } + + @Test + void noBanana2(){ + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("banana a"); + banana.BananaMode=true; + + banana.handle(receivedMessage); + + assertEquals(banana.BananaMode,true); + } + + @Test void dividing() { + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("banana divi 4 2"); + banana.BananaMode=true; + + banana.handle(receivedMessage); + + verify(receivedMessage, times(1)).sendResponse(4/2+""); + assertEquals(banana.BananaMode,true); + } + + @Test void dividingButDecimals() { + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("banana divi 5.2 6"); + banana.BananaMode=true; + + banana.handle(receivedMessage); + + verify(receivedMessage, times(1)).sendResponse("hey i dont think thats an integer"); + assertEquals(banana.BananaMode,true); + } + @Test void dividingButZero() { + HelpEmbed helpEmbed = new HelpEmbed(banana.COMMAND, "test"); + when(receivedMessage.getMessageContent()).thenReturn("banana divi 5 0"); + banana.BananaMode=true; + + banana.handle(receivedMessage); + + verify(receivedMessage, times(1)).sendResponse("bruh"); + assertEquals(banana.BananaMode,true); + } + +} diff --git a/src/test/java/org/jointheleague/features/student/first_feature/FeatureOneTest.java b/src/test/java/org/jointheleague/features/student/first_feature/FeatureOneTest.java index ff8f86c8..b22f27a6 100644 --- a/src/test/java/org/jointheleague/features/student/first_feature/FeatureOneTest.java +++ b/src/test/java/org/jointheleague/features/student/first_feature/FeatureOneTest.java @@ -10,7 +10,7 @@ import org.mockito.MockitoAnnotations; import java.io.ByteArrayOutputStream; -import java.io.PrintStream; +import java.io.PrintStream; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; From 69490800eda4197a5a3571a6df9bee303ac6c331 Mon Sep 17 00:00:00 2001 From: league Date: Wed, 20 Aug 2025 16:53:59 -0700 Subject: [PATCH 07/10] Feature 3 Weather Api Progress --- .../jointheleague/discord_bot/DiscordBot.java | 2 + .../student/first_feature/Condition.java | 538 ++++++++++++++++++ .../student/first_feature/WeatherAPI.java | 67 +++ 3 files changed, 607 insertions(+) create mode 100644 src/main/java/org/jointheleague/features/student/first_feature/Condition.java create mode 100644 src/main/java/org/jointheleague/features/student/first_feature/WeatherAPI.java diff --git a/src/main/java/org/jointheleague/discord_bot/DiscordBot.java b/src/main/java/org/jointheleague/discord_bot/DiscordBot.java index 1abf377c..46850e3c 100644 --- a/src/main/java/org/jointheleague/discord_bot/DiscordBot.java +++ b/src/main/java/org/jointheleague/discord_bot/DiscordBot.java @@ -10,6 +10,7 @@ import org.jointheleague.features.help_embed.HelpListener; import org.jointheleague.features.student.first_feature.BananaSoftware; import org.jointheleague.features.student.first_feature.FeatureOne; +import org.jointheleague.features.student.first_feature.WeatherAPI; import net.dv8tion.jda.api.JDA; import net.dv8tion.jda.api.JDABuilder; @@ -58,6 +59,7 @@ public void connect(boolean printInvite) throws InterruptedException { api.addEventListener(helpListener); //add features + addFeature(new WeatherAPI(channelName)); addFeature(new FeatureOne(channelName, false)); addFeature(new BananaSoftware(channelName)); addFeature(new CurrentTime(channelName)); diff --git a/src/main/java/org/jointheleague/features/student/first_feature/Condition.java b/src/main/java/org/jointheleague/features/student/first_feature/Condition.java new file mode 100644 index 00000000..d4081634 --- /dev/null +++ b/src/main/java/org/jointheleague/features/student/first_feature/Condition.java @@ -0,0 +1,538 @@ +package org.jointheleague.features.student.first_feature; + +import javax.annotation.Generated; +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; +import javax.annotation.Generated; +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +public class Condition { + +@SerializedName("text") +@Expose +private String text; +@SerializedName("icon") +@Expose +private String icon; +@SerializedName("code") +@Expose +private Integer code; + +public String getText() { +return text; +} + +public void setText(String text) { +this.text = text; +} + +public String getIcon() { +return icon; +} + +public void setIcon(String icon) { +this.icon = icon; +} + +public Integer getCode() { +return code; +} + +public void setCode(Integer code) { +this.code = code; +} + +} + + + +@Generated("jsonschema2pojo") +class Current { + +@SerializedName("last_updated_epoch") +@Expose +private Integer lastUpdatedEpoch; +@SerializedName("last_updated") +@Expose +private String lastUpdated; +@SerializedName("temp_c") +@Expose +private Double tempC; +@SerializedName("temp_f") +@Expose +private Double tempF; +@SerializedName("is_day") +@Expose +private Integer isDay; +@SerializedName("condition") +@Expose +private Condition condition; +@SerializedName("wind_mph") +@Expose +private Double windMph; +@SerializedName("wind_kph") +@Expose +private Double windKph; +@SerializedName("wind_degree") +@Expose +private Integer windDegree; +@SerializedName("wind_dir") +@Expose +private String windDir; +@SerializedName("pressure_mb") +@Expose +private Double pressureMb; +@SerializedName("pressure_in") +@Expose +private Double pressureIn; +@SerializedName("precip_mm") +@Expose +private Double precipMm; +@SerializedName("precip_in") +@Expose +private Double precipIn; +@SerializedName("humidity") +@Expose +private Integer humidity; +@SerializedName("cloud") +@Expose +private Integer cloud; +@SerializedName("feelslike_c") +@Expose +private Double feelslikeC; +@SerializedName("feelslike_f") +@Expose +private Double feelslikeF; +@SerializedName("windchill_c") +@Expose +private Double windchillC; +@SerializedName("windchill_f") +@Expose +private Double windchillF; +@SerializedName("heatindex_c") +@Expose +private Double heatindexC; +@SerializedName("heatindex_f") +@Expose +private Double heatindexF; +@SerializedName("dewpoint_c") +@Expose +private Double dewpointC; +@SerializedName("dewpoint_f") +@Expose +private Double dewpointF; +@SerializedName("vis_km") +@Expose +private Double visKm; +@SerializedName("vis_miles") +@Expose +private Double visMiles; +@SerializedName("uv") +@Expose +private Double uv; +@SerializedName("gust_mph") +@Expose +private Double gustMph; +@SerializedName("gust_kph") +@Expose +private Double gustKph; +@SerializedName("short_rad") +@Expose +private Double shortRad; +@SerializedName("diff_rad") +@Expose +private Double diffRad; +@SerializedName("dni") +@Expose +private Double dni; +@SerializedName("gti") +@Expose +private Double gti; + +public Integer getLastUpdatedEpoch() { +return lastUpdatedEpoch; +} + +public void setLastUpdatedEpoch(Integer lastUpdatedEpoch) { +this.lastUpdatedEpoch = lastUpdatedEpoch; +} + +public String getLastUpdated() { +return lastUpdated; +} + +public void setLastUpdated(String lastUpdated) { +this.lastUpdated = lastUpdated; +} + +public Double getTempC() { +return tempC; +} + +public void setTempC(Double tempC) { +this.tempC = tempC; +} + +public Double getTempF() { +return tempF; +} + +public void setTempF(Double tempF) { +this.tempF = tempF; +} + +public Integer getIsDay() { +return isDay; +} + +public void setIsDay(Integer isDay) { +this.isDay = isDay; +} + +public Condition getCondition() { +return condition; +} + +public void setCondition(Condition condition) { +this.condition = condition; +} + +public Double getWindMph() { +return windMph; +} + +public void setWindMph(Double windMph) { +this.windMph = windMph; +} + +public Double getWindKph() { +return windKph; +} + +public void setWindKph(Double windKph) { +this.windKph = windKph; +} + +public Integer getWindDegree() { +return windDegree; +} + +public void setWindDegree(Integer windDegree) { +this.windDegree = windDegree; +} + +public String getWindDir() { +return windDir; +} + +public void setWindDir(String windDir) { +this.windDir = windDir; +} + +public Double getPressureMb() { +return pressureMb; +} + +public void setPressureMb(Double pressureMb) { +this.pressureMb = pressureMb; +} + +public Double getPressureIn() { +return pressureIn; +} + +public void setPressureIn(Double pressureIn) { +this.pressureIn = pressureIn; +} + +public Double getPrecipMm() { +return precipMm; +} + +public void setPrecipMm(Double precipMm) { +this.precipMm = precipMm; +} + +public Double getPrecipIn() { +return precipIn; +} + +public void setPrecipIn(Double precipIn) { +this.precipIn = precipIn; +} + +public Integer getHumidity() { +return humidity; +} + +public void setHumidity(Integer humidity) { +this.humidity = humidity; +} + +public Integer getCloud() { +return cloud; +} + +public void setCloud(Integer cloud) { +this.cloud = cloud; +} + +public Double getFeelslikeC() { +return feelslikeC; +} + +public void setFeelslikeC(Double feelslikeC) { +this.feelslikeC = feelslikeC; +} + +public Double getFeelslikeF() { +return feelslikeF; +} + +public void setFeelslikeF(Double feelslikeF) { +this.feelslikeF = feelslikeF; +} + +public Double getWindchillC() { +return windchillC; +} + +public void setWindchillC(Double windchillC) { +this.windchillC = windchillC; +} + +public Double getWindchillF() { +return windchillF; +} + +public void setWindchillF(Double windchillF) { +this.windchillF = windchillF; +} + +public Double getHeatindexC() { +return heatindexC; +} + +public void setHeatindexC(Double heatindexC) { +this.heatindexC = heatindexC; +} + +public Double getHeatindexF() { +return heatindexF; +} + +public void setHeatindexF(Double heatindexF) { +this.heatindexF = heatindexF; +} + +public Double getDewpointC() { +return dewpointC; +} + +public void setDewpointC(Double dewpointC) { +this.dewpointC = dewpointC; +} + +public Double getDewpointF() { +return dewpointF; +} + +public void setDewpointF(Double dewpointF) { +this.dewpointF = dewpointF; +} + +public Double getVisKm() { +return visKm; +} + +public void setVisKm(Double visKm) { +this.visKm = visKm; +} + +public Double getVisMiles() { +return visMiles; +} + +public void setVisMiles(Double visMiles) { +this.visMiles = visMiles; +} + +public Double getUv() { +return uv; +} + +public void setUv(Double uv) { +this.uv = uv; +} + +public Double getGustMph() { +return gustMph; +} + +public void setGustMph(Double gustMph) { +this.gustMph = gustMph; +} + +public Double getGustKph() { +return gustKph; +} + +public void setGustKph(Double gustKph) { +this.gustKph = gustKph; +} + +public Double getShortRad() { +return shortRad; +} + +public void setShortRad(Double shortRad) { +this.shortRad = shortRad; +} + +public Double getDiffRad() { +return diffRad; +} + +public void setDiffRad(Double diffRad) { +this.diffRad = diffRad; +} + +public Double getDni() { +return dni; +} + +public void setDni(Double dni) { +this.dni = dni; +} + +public Double getGti() { +return gti; +} + +public void setGti(Double gti) { +this.gti = gti; +} + +} + +@Generated("jsonschema2pojo") +class Example { + +@SerializedName("location") +@Expose +private Location location; +@SerializedName("current") +@Expose +private Current current; + +public Location getLocation() { +return location; +} + +public void setLocation(Location location) { +this.location = location; +} + +public Current getCurrent() { +return current; +} + +public void setCurrent(Current current) { +this.current = current; +} + +} + +class Location { + +@SerializedName("name") +@Expose +private String name; +@SerializedName("region") +@Expose +private String region; +@SerializedName("country") +@Expose +private String country; +@SerializedName("lat") +@Expose +private Double lat; +@SerializedName("lon") +@Expose +private Double lon; +@SerializedName("tz_id") +@Expose +private String tzId; +@SerializedName("localtime_epoch") +@Expose +private Integer localtimeEpoch; +@SerializedName("localtime") +@Expose +private String localtime; + +public String getName() { +return name; +} + +public void setName(String name) { +this.name = name; +} + +public String getRegion() { +return region; +} + +public void setRegion(String region) { +this.region = region; +} + +public String getCountry() { +return country; +} + +public void setCountry(String country) { +this.country = country; +} + +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 String getTzId() { +return tzId; +} + +public void setTzId(String tzId) { +this.tzId = tzId; +} + +public Integer getLocaltimeEpoch() { +return localtimeEpoch; +} + +public void setLocaltimeEpoch(Integer localtimeEpoch) { +this.localtimeEpoch = localtimeEpoch; +} + +public String getLocaltime() { +return localtime; +} + +public void setLocaltime(String localtime) { +this.localtime = localtime; +} + +} \ No newline at end of file diff --git a/src/main/java/org/jointheleague/features/student/first_feature/WeatherAPI.java b/src/main/java/org/jointheleague/features/student/first_feature/WeatherAPI.java new file mode 100644 index 00000000..feab2223 --- /dev/null +++ b/src/main/java/org/jointheleague/features/student/first_feature/WeatherAPI.java @@ -0,0 +1,67 @@ +package org.jointheleague.features.student.first_feature; + +import java.util.Random; + +import org.jointheleague.api_wrapper.ReceivedMessage; +import org.jointheleague.features.examples.third_features.plain_old_java_objects.news_api.ApiExampleWrapper; +import org.jointheleague.features.templates.FeatureTemplate; +import org.springframework.web.reactive.function.client.WebClient; + +import reactor.core.publisher.Mono; + +public class WeatherAPI extends FeatureTemplate{ + +public final String COMMAND = "weather"; + + private static final String key = "82d1da6ca0b64c12ada224552252008"; + private static final String URL = "http://api.weatherapi.com/v1/current.json"; + + private WebClient webClient = WebClient.create(URL); + + public WeatherAPI(String channelName) { + super(channelName); + // TODO Auto-generated constructor stub + + this.webClient = WebClient + .builder() + .baseUrl(URL) + .build(); + } + + + + public String getCurrentWeather(String city) { + + Mono apiExampleWrapperMono = webClient.get() + .uri(uriBuilder -> uriBuilder + .queryParam("q", city) + .queryParam("apiKey", key) + .build()) + .retrieve() + .bodyToMono(String.class); + + return apiExampleWrapperMono.block(); + + } + + public void handle(ReceivedMessage event) { + String mc = event.getMessageContent(); + if(mc.trim().toLowerCase().equals(COMMAND)) { + event.sendResponse("Please add a city."); + } + + if(mc.trim().toLowerCase().startsWith(COMMAND)) { + + String city = mc.trim().substring(8); + System.out.println("before block"); // debug + String temp = getCurrentWeather(city); + System.out.println("after block"); // debug + event.sendResponse(temp + " fsd fsdfsdfs"); + } + } + + + + + +} From 05d5fed5eb36931c7eeb08bec659073e4b56ea04 Mon Sep 17 00:00:00 2001 From: league Date: Wed, 27 Aug 2025 16:56:02 -0700 Subject: [PATCH 08/10] Weather API Progress --- .../student/first_feature/Condition.java | 497 +----------------- .../student/first_feature/Current.java | 286 ++++++++++ .../student/first_feature/CurrentWeather.java | 41 ++ .../student/first_feature/Location.java | 99 ++++ .../student/first_feature/WeatherAPI.java | 40 +- 5 files changed, 463 insertions(+), 500 deletions(-) create mode 100644 src/main/java/org/jointheleague/features/student/first_feature/Current.java create mode 100644 src/main/java/org/jointheleague/features/student/first_feature/CurrentWeather.java create mode 100644 src/main/java/org/jointheleague/features/student/first_feature/Location.java diff --git a/src/main/java/org/jointheleague/features/student/first_feature/Condition.java b/src/main/java/org/jointheleague/features/student/first_feature/Condition.java index d4081634..fa45f314 100644 --- a/src/main/java/org/jointheleague/features/student/first_feature/Condition.java +++ b/src/main/java/org/jointheleague/features/student/first_feature/Condition.java @@ -1,12 +1,10 @@ package org.jointheleague.features.student.first_feature; -import javax.annotation.Generated; -import com.google.gson.annotations.Expose; -import com.google.gson.annotations.SerializedName; import javax.annotation.Generated; import com.google.gson.annotations.Expose; import com.google.gson.annotations.SerializedName; +@Generated("jsonschema2pojo") public class Condition { @SerializedName("text") @@ -43,496 +41,9 @@ public void setCode(Integer code) { this.code = code; } -} - - - -@Generated("jsonschema2pojo") -class Current { - -@SerializedName("last_updated_epoch") -@Expose -private Integer lastUpdatedEpoch; -@SerializedName("last_updated") -@Expose -private String lastUpdated; -@SerializedName("temp_c") -@Expose -private Double tempC; -@SerializedName("temp_f") -@Expose -private Double tempF; -@SerializedName("is_day") -@Expose -private Integer isDay; -@SerializedName("condition") -@Expose -private Condition condition; -@SerializedName("wind_mph") -@Expose -private Double windMph; -@SerializedName("wind_kph") -@Expose -private Double windKph; -@SerializedName("wind_degree") -@Expose -private Integer windDegree; -@SerializedName("wind_dir") -@Expose -private String windDir; -@SerializedName("pressure_mb") -@Expose -private Double pressureMb; -@SerializedName("pressure_in") -@Expose -private Double pressureIn; -@SerializedName("precip_mm") -@Expose -private Double precipMm; -@SerializedName("precip_in") -@Expose -private Double precipIn; -@SerializedName("humidity") -@Expose -private Integer humidity; -@SerializedName("cloud") -@Expose -private Integer cloud; -@SerializedName("feelslike_c") -@Expose -private Double feelslikeC; -@SerializedName("feelslike_f") -@Expose -private Double feelslikeF; -@SerializedName("windchill_c") -@Expose -private Double windchillC; -@SerializedName("windchill_f") -@Expose -private Double windchillF; -@SerializedName("heatindex_c") -@Expose -private Double heatindexC; -@SerializedName("heatindex_f") -@Expose -private Double heatindexF; -@SerializedName("dewpoint_c") -@Expose -private Double dewpointC; -@SerializedName("dewpoint_f") -@Expose -private Double dewpointF; -@SerializedName("vis_km") -@Expose -private Double visKm; -@SerializedName("vis_miles") -@Expose -private Double visMiles; -@SerializedName("uv") -@Expose -private Double uv; -@SerializedName("gust_mph") -@Expose -private Double gustMph; -@SerializedName("gust_kph") -@Expose -private Double gustKph; -@SerializedName("short_rad") -@Expose -private Double shortRad; -@SerializedName("diff_rad") -@Expose -private Double diffRad; -@SerializedName("dni") -@Expose -private Double dni; -@SerializedName("gti") -@Expose -private Double gti; - -public Integer getLastUpdatedEpoch() { -return lastUpdatedEpoch; -} - -public void setLastUpdatedEpoch(Integer lastUpdatedEpoch) { -this.lastUpdatedEpoch = lastUpdatedEpoch; -} - -public String getLastUpdated() { -return lastUpdated; -} - -public void setLastUpdated(String lastUpdated) { -this.lastUpdated = lastUpdated; -} - -public Double getTempC() { -return tempC; -} - -public void setTempC(Double tempC) { -this.tempC = tempC; -} - -public Double getTempF() { -return tempF; -} - -public void setTempF(Double tempF) { -this.tempF = tempF; -} - -public Integer getIsDay() { -return isDay; -} - -public void setIsDay(Integer isDay) { -this.isDay = isDay; -} - -public Condition getCondition() { -return condition; -} - -public void setCondition(Condition condition) { -this.condition = condition; -} - -public Double getWindMph() { -return windMph; -} - -public void setWindMph(Double windMph) { -this.windMph = windMph; -} - -public Double getWindKph() { -return windKph; -} - -public void setWindKph(Double windKph) { -this.windKph = windKph; -} - -public Integer getWindDegree() { -return windDegree; -} - -public void setWindDegree(Integer windDegree) { -this.windDegree = windDegree; -} - -public String getWindDir() { -return windDir; -} - -public void setWindDir(String windDir) { -this.windDir = windDir; -} - -public Double getPressureMb() { -return pressureMb; -} - -public void setPressureMb(Double pressureMb) { -this.pressureMb = pressureMb; -} - -public Double getPressureIn() { -return pressureIn; -} - -public void setPressureIn(Double pressureIn) { -this.pressureIn = pressureIn; -} - -public Double getPrecipMm() { -return precipMm; -} - -public void setPrecipMm(Double precipMm) { -this.precipMm = precipMm; -} - -public Double getPrecipIn() { -return precipIn; -} - -public void setPrecipIn(Double precipIn) { -this.precipIn = precipIn; -} - -public Integer getHumidity() { -return humidity; -} - -public void setHumidity(Integer humidity) { -this.humidity = humidity; -} - -public Integer getCloud() { -return cloud; -} - -public void setCloud(Integer cloud) { -this.cloud = cloud; -} - -public Double getFeelslikeC() { -return feelslikeC; -} - -public void setFeelslikeC(Double feelslikeC) { -this.feelslikeC = feelslikeC; -} - -public Double getFeelslikeF() { -return feelslikeF; -} - -public void setFeelslikeF(Double feelslikeF) { -this.feelslikeF = feelslikeF; -} - -public Double getWindchillC() { -return windchillC; -} - -public void setWindchillC(Double windchillC) { -this.windchillC = windchillC; -} - -public Double getWindchillF() { -return windchillF; -} - -public void setWindchillF(Double windchillF) { -this.windchillF = windchillF; -} - -public Double getHeatindexC() { -return heatindexC; -} - -public void setHeatindexC(Double heatindexC) { -this.heatindexC = heatindexC; -} - -public Double getHeatindexF() { -return heatindexF; -} - -public void setHeatindexF(Double heatindexF) { -this.heatindexF = heatindexF; -} - -public Double getDewpointC() { -return dewpointC; -} - -public void setDewpointC(Double dewpointC) { -this.dewpointC = dewpointC; -} - -public Double getDewpointF() { -return dewpointF; -} - -public void setDewpointF(Double dewpointF) { -this.dewpointF = dewpointF; -} - -public Double getVisKm() { -return visKm; -} - -public void setVisKm(Double visKm) { -this.visKm = visKm; -} - -public Double getVisMiles() { -return visMiles; -} - -public void setVisMiles(Double visMiles) { -this.visMiles = visMiles; -} - -public Double getUv() { -return uv; -} - -public void setUv(Double uv) { -this.uv = uv; -} - -public Double getGustMph() { -return gustMph; -} - -public void setGustMph(Double gustMph) { -this.gustMph = gustMph; -} - -public Double getGustKph() { -return gustKph; -} - -public void setGustKph(Double gustKph) { -this.gustKph = gustKph; -} - -public Double getShortRad() { -return shortRad; -} - -public void setShortRad(Double shortRad) { -this.shortRad = shortRad; -} - -public Double getDiffRad() { -return diffRad; -} - -public void setDiffRad(Double diffRad) { -this.diffRad = diffRad; -} - -public Double getDni() { -return dni; -} - -public void setDni(Double dni) { -this.dni = dni; -} - -public Double getGti() { -return gti; -} - -public void setGti(Double gti) { -this.gti = gti; -} - -} - -@Generated("jsonschema2pojo") -class Example { - -@SerializedName("location") -@Expose -private Location location; -@SerializedName("current") -@Expose -private Current current; - -public Location getLocation() { -return location; -} - -public void setLocation(Location location) { -this.location = location; -} - -public Current getCurrent() { -return current; -} - -public void setCurrent(Current current) { -this.current = current; -} - -} - -class Location { - -@SerializedName("name") -@Expose -private String name; -@SerializedName("region") -@Expose -private String region; -@SerializedName("country") -@Expose -private String country; -@SerializedName("lat") -@Expose -private Double lat; -@SerializedName("lon") -@Expose -private Double lon; -@SerializedName("tz_id") -@Expose -private String tzId; -@SerializedName("localtime_epoch") -@Expose -private Integer localtimeEpoch; -@SerializedName("localtime") -@Expose -private String localtime; - -public String getName() { -return name; -} - -public void setName(String name) { -this.name = name; -} - -public String getRegion() { -return region; -} - -public void setRegion(String region) { -this.region = region; -} - -public String getCountry() { -return country; -} - -public void setCountry(String country) { -this.country = country; -} - -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 String getTzId() { -return tzId; -} - -public void setTzId(String tzId) { -this.tzId = tzId; -} - -public Integer getLocaltimeEpoch() { -return localtimeEpoch; -} - -public void setLocaltimeEpoch(Integer localtimeEpoch) { -this.localtimeEpoch = localtimeEpoch; -} - -public String getLocaltime() { -return localtime; -} - -public void setLocaltime(String localtime) { -this.localtime = localtime; +@Override +public String toString() { + return "Condition [text=" + text + ", icon=" + icon + ", code=" + code + "]"; } } \ No newline at end of file diff --git a/src/main/java/org/jointheleague/features/student/first_feature/Current.java b/src/main/java/org/jointheleague/features/student/first_feature/Current.java new file mode 100644 index 00000000..70f87a84 --- /dev/null +++ b/src/main/java/org/jointheleague/features/student/first_feature/Current.java @@ -0,0 +1,286 @@ +package org.jointheleague.features.student.first_feature; + +import javax.annotation.Generated; +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +@Generated("jsonschema2pojo") +public class Current { + +@SerializedName("last_updated_epoch") +@Expose +private Integer lastUpdatedEpoch; +@SerializedName("last_updated") +@Expose +private String lastUpdated; +@SerializedName("temp_c") +@Expose +private Float tempC; +@SerializedName("temp_f") +@Expose +private Float tempF; +@SerializedName("is_day") +@Expose +private Integer isDay; +@SerializedName("condition") +@Expose +private Condition condition; +@SerializedName("wind_mph") +@Expose +private Float windMph; +@SerializedName("wind_kph") +@Expose +private Float windKph; +@SerializedName("wind_degree") +@Expose +private Integer windDegree; +@SerializedName("wind_dir") +@Expose +private String windDir; +@SerializedName("pressure_mb") +@Expose +private Integer pressureMb; +@SerializedName("pressure_in") +@Expose +private Float pressureIn; +@SerializedName("precip_mm") +@Expose +private Integer precipMm; +@SerializedName("precip_in") +@Expose +private Integer precipIn; +@SerializedName("humidity") +@Expose +private Integer humidity; +@SerializedName("cloud") +@Expose +private Integer cloud; +@SerializedName("feelslike_c") +@Expose +private Integer feelslikeC; +@SerializedName("feelslike_f") +@Expose +private Float feelslikeF; +@SerializedName("vis_km") +@Expose +private Integer visKm; +@SerializedName("vis_miles") +@Expose +private Integer visMiles; +@SerializedName("uv") +@Expose +private Float uv; +@SerializedName("gust_mph") +@Expose +private Float gustMph; +@SerializedName("gust_kph") +@Expose +private Float gustKph; +//@SerializedName("air_quality") +//@Expose +//private AirQuality airQuality; + +public Integer getLastUpdatedEpoch() { +return lastUpdatedEpoch; +} + +public void setLastUpdatedEpoch(Integer lastUpdatedEpoch) { +this.lastUpdatedEpoch = lastUpdatedEpoch; +} + +public String getLastUpdated() { +return lastUpdated; +} + +public void setLastUpdated(String lastUpdated) { +this.lastUpdated = lastUpdated; +} + +public Float getTempC() { +return tempC; +} + +public void setTempC(Float tempC) { +this.tempC = tempC; +} + +public Float getTempF() { +return tempF; +} + +public void setTempF(Float tempF) { +this.tempF = tempF; +} + +public Integer getIsDay() { +return isDay; +} + +public void setIsDay(Integer isDay) { +this.isDay = isDay; +} + +public Condition getCondition() { +return condition; +} + +public void setCondition(Condition condition) { +this.condition = condition; +} + +public Float getWindMph() { +return windMph; +} + +public void setWindMph(Float windMph) { +this.windMph = windMph; +} + +public Float getWindKph() { +return windKph; +} + +public void setWindKph(Float windKph) { +this.windKph = windKph; +} + +public Integer getWindDegree() { +return windDegree; +} + +public void setWindDegree(Integer windDegree) { +this.windDegree = windDegree; +} + +public String getWindDir() { +return windDir; +} + +public void setWindDir(String windDir) { +this.windDir = windDir; +} + +public Integer getPressureMb() { +return pressureMb; +} + +public void setPressureMb(Integer pressureMb) { +this.pressureMb = pressureMb; +} + +public Float getPressureIn() { +return pressureIn; +} + +public void setPressureIn(Float pressureIn) { +this.pressureIn = pressureIn; +} + +public Integer getPrecipMm() { +return precipMm; +} + +public void setPrecipMm(Integer precipMm) { +this.precipMm = precipMm; +} + +public Integer getPrecipIn() { +return precipIn; +} + +public void setPrecipIn(Integer precipIn) { +this.precipIn = precipIn; +} + +public Integer getHumidity() { +return humidity; +} + +public void setHumidity(Integer humidity) { +this.humidity = humidity; +} + +public Integer getCloud() { +return cloud; +} + +public void setCloud(Integer cloud) { +this.cloud = cloud; +} + +public Integer getFeelslikeC() { +return feelslikeC; +} + +public void setFeelslikeC(Integer feelslikeC) { +this.feelslikeC = feelslikeC; +} + +public Float getFeelslikeF() { +return feelslikeF; +} + +public void setFeelslikeF(Float feelslikeF) { +this.feelslikeF = feelslikeF; +} + +public Integer getVisKm() { +return visKm; +} + +public void setVisKm(Integer visKm) { +this.visKm = visKm; +} + +public Integer getVisMiles() { +return visMiles; +} + +public void setVisMiles(Integer visMiles) { +this.visMiles = visMiles; +} + +public Float getUv() { +return uv; +} + +public void setUv(Float uv) { +this.uv = uv; +} + +public Float getGustMph() { +return gustMph; +} + +public void setGustMph(Float gustMph) { +this.gustMph = gustMph; +} + +public Float getGustKph() { +return gustKph; +} + +public void setGustKph(Float gustKph) { +this.gustKph = gustKph; +} + +@Override +public String toString() { + return "Current [lastUpdatedEpoch=" + lastUpdatedEpoch + ", lastUpdated=" + lastUpdated + ", tempC=" + tempC + + ", tempF=" + tempF + ", isDay=" + isDay + ", condition=" + condition + ", windMph=" + windMph + + ", windKph=" + windKph + ", windDegree=" + windDegree + ", windDir=" + windDir + ", pressureMb=" + + pressureMb + ", pressureIn=" + pressureIn + ", precipMm=" + precipMm + ", precipIn=" + precipIn + + ", humidity=" + humidity + ", cloud=" + cloud + ", feelslikeC=" + feelslikeC + ", feelslikeF=" + + feelslikeF + ", visKm=" + visKm + ", visMiles=" + visMiles + ", uv=" + uv + ", gustMph=" + gustMph + + ", gustKph=" + gustKph + "]"; +} + +//public AirQuality getAirQuality() { +//return airQuality; +//} +// +//public void setAirQuality(AirQuality airQuality) { +//this.airQuality = airQuality; +//} + +} \ No newline at end of file diff --git a/src/main/java/org/jointheleague/features/student/first_feature/CurrentWeather.java b/src/main/java/org/jointheleague/features/student/first_feature/CurrentWeather.java new file mode 100644 index 00000000..50b03951 --- /dev/null +++ b/src/main/java/org/jointheleague/features/student/first_feature/CurrentWeather.java @@ -0,0 +1,41 @@ +package org.jointheleague.features.student.first_feature; + +import javax.annotation.Generated; + +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +@Generated("jsonschema2pojo") +public class CurrentWeather { + + @SerializedName("location") + @Expose + private Location location; + @SerializedName("current") + @Expose + private Current current; + + public Location getLocation() { + return location; + } + + public void setLocation(Location location) { + this.location = location; + } + + public Current getCurrent() { + return current; + } + + public void setCurrent(Current current) { + this.current = current; + } + + @Override + public String toString() { + return "CurrentWeather [location=" + location + ", current=" + current + "]"; + } + + + +} \ No newline at end of file diff --git a/src/main/java/org/jointheleague/features/student/first_feature/Location.java b/src/main/java/org/jointheleague/features/student/first_feature/Location.java new file mode 100644 index 00000000..b959cc56 --- /dev/null +++ b/src/main/java/org/jointheleague/features/student/first_feature/Location.java @@ -0,0 +1,99 @@ +package org.jointheleague.features.student.first_feature; + +import javax.annotation.Generated; +import com.google.gson.annotations.Expose; +import com.google.gson.annotations.SerializedName; + +@Generated("jsonschema2pojo") +public class Location { + +@SerializedName("name") +@Expose +private String name; +@SerializedName("region") +@Expose +private String region; +@SerializedName("country") +@Expose +private String country; +@SerializedName("lat") +@Expose +private Double lat; +@SerializedName("lon") +@Expose +private Double lon; +@SerializedName("tz_id") +@Expose +private String tzId; +@SerializedName("localtime_epoch") +@Expose +private Integer localtimeEpoch; +@SerializedName("localtime") +@Expose +private String localtime; + +public String getName() { +return name; +} + +public void setName(String name) { +this.name = name; +} + +public String getRegion() { +return region; +} + +public void setRegion(String region) { +this.region = region; +} + +public String getCountry() { +return country; +} + +public void setCountry(String country) { +this.country = country; +} + +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 String getTzId() { +return tzId; +} + +public void setTzId(String tzId) { +this.tzId = tzId; +} + +public Integer getLocaltimeEpoch() { +return localtimeEpoch; +} + +public void setLocaltimeEpoch(Integer localtimeEpoch) { +this.localtimeEpoch = localtimeEpoch; +} + +public String getLocaltime() { +return localtime; +} + +public void setLocaltime(String localtime) { +this.localtime = localtime; +} + +} \ No newline at end of file diff --git a/src/main/java/org/jointheleague/features/student/first_feature/WeatherAPI.java b/src/main/java/org/jointheleague/features/student/first_feature/WeatherAPI.java index feab2223..9c836270 100644 --- a/src/main/java/org/jointheleague/features/student/first_feature/WeatherAPI.java +++ b/src/main/java/org/jointheleague/features/student/first_feature/WeatherAPI.java @@ -13,7 +13,7 @@ public class WeatherAPI extends FeatureTemplate{ public final String COMMAND = "weather"; - private static final String key = "82d1da6ca0b64c12ada224552252008"; + private static final String key = "65e242ae27dd4a83a3d224705252708"; private static final String URL = "http://api.weatherapi.com/v1/current.json"; private WebClient webClient = WebClient.create(URL); @@ -30,17 +30,32 @@ public WeatherAPI(String channelName) { - public String getCurrentWeather(String city) { + public CurrentWeather getCurrentWeather(String city) { + + Mono apiExampleWrapperMono = webClient.get() + .uri(uriBuilder -> uriBuilder + .queryParam("q", city) + .queryParam("key", key) + .build()) + .retrieve() + .bodyToMono(CurrentWeather.class); + + return apiExampleWrapperMono.block(); + + } + + + public void printCurrentWeather(String city) { Mono apiExampleWrapperMono = webClient.get() .uri(uriBuilder -> uriBuilder .queryParam("q", city) - .queryParam("apiKey", key) + .queryParam("key", key) .build()) .retrieve() .bodyToMono(String.class); - return apiExampleWrapperMono.block(); + System.out.println( apiExampleWrapperMono.block()); } @@ -52,11 +67,22 @@ public void handle(ReceivedMessage event) { if(mc.trim().toLowerCase().startsWith(COMMAND)) { - String city = mc.trim().substring(8); + String city = mc.trim().substring(COMMAND.length()+2); System.out.println("before block"); // debug - String temp = getCurrentWeather(city); + printCurrentWeather(city); + CurrentWeather data = getCurrentWeather(city); System.out.println("after block"); // debug - event.sendResponse(temp + " fsd fsdfsdfs"); + + String temp = data.getCurrent().getTempC()+""; + //String hum = data.getCurrent().getHumidity()+""; + System.out.println(data); + + event.sendResponse("Temperature (F):" + temp); + // event.sendResponse("Humidity (%): " + hum); + + + // do the cumbersome block(String.class).substring() method to get values when blocking to string instead of wrapper + } } From 7da1a5ec42cf4177ce6e5bc6c79b8417e10c33c6 Mon Sep 17 00:00:00 2001 From: league Date: Wed, 3 Sep 2025 16:57:42 -0700 Subject: [PATCH 09/10] Basic Weather API Complete. Complete TODO later --- .../student/first_feature/WeatherAPI.java | 70 ++++++++++++++----- .../student/first_feature/WeatherTest.java | 33 +++++++++ 2 files changed, 84 insertions(+), 19 deletions(-) create mode 100644 src/test/java/org/jointheleague/features/student/first_feature/WeatherTest.java diff --git a/src/main/java/org/jointheleague/features/student/first_feature/WeatherAPI.java b/src/main/java/org/jointheleague/features/student/first_feature/WeatherAPI.java index 9c836270..51311aff 100644 --- a/src/main/java/org/jointheleague/features/student/first_feature/WeatherAPI.java +++ b/src/main/java/org/jointheleague/features/student/first_feature/WeatherAPI.java @@ -12,8 +12,9 @@ public class WeatherAPI extends FeatureTemplate{ public final String COMMAND = "weather"; +public final String COMMAND_ = "raw_weather"; - private static final String key = "65e242ae27dd4a83a3d224705252708"; + private static final String key = "7a59fc69444d4617a5a225318250309"; private static final String URL = "http://api.weatherapi.com/v1/current.json"; private WebClient webClient = WebClient.create(URL); @@ -30,22 +31,22 @@ public WeatherAPI(String channelName) { - public CurrentWeather getCurrentWeather(String city) { + public String getCurrentWeather(String city) { - Mono apiExampleWrapperMono = webClient.get() + Mono apiExampleWrapperMono = webClient.get() .uri(uriBuilder -> uriBuilder .queryParam("q", city) .queryParam("key", key) .build()) .retrieve() - .bodyToMono(CurrentWeather.class); + .bodyToMono(String.class); return apiExampleWrapperMono.block(); } - public void printCurrentWeather(String city) { + /*public void printCurrentWeather(String city) { Mono apiExampleWrapperMono = webClient.get() .uri(uriBuilder -> uriBuilder @@ -57,32 +58,63 @@ public void printCurrentWeather(String city) { System.out.println( apiExampleWrapperMono.block()); - } + }*/ public void handle(ReceivedMessage event) { String mc = event.getMessageContent(); - if(mc.trim().toLowerCase().equals(COMMAND)) { + if(mc.trim().toLowerCase().equals(COMMAND)||mc.trim().toLowerCase().equals(COMMAND_)) { event.sendResponse("Please add a city."); } - + if(mc.trim().toLowerCase().startsWith(COMMAND)) { - String city = mc.trim().substring(COMMAND.length()+2); - System.out.println("before block"); // debug - printCurrentWeather(city); - CurrentWeather data = getCurrentWeather(city); - System.out.println("after block"); // debug + String city = mc.trim().substring(COMMAND.length()+1); + + String json = getCurrentWeather(city); - String temp = data.getCurrent().getTempC()+""; - //String hum = data.getCurrent().getHumidity()+""; - System.out.println(data); + //event.sendResponse(json); - event.sendResponse("Temperature (F):" + temp); - // event.sendResponse("Humidity (%): " + hum); + String tempKey = "\"temp_f\":"; + int tempStart = json.indexOf(tempKey) + tempKey.length(); + int tempEnd = json.indexOf(",", tempStart); + String temp = json.substring(tempStart, tempEnd); + + // Extract humidity + String humidityKey = "\"humidity\":"; + int humidityStart = json.indexOf(humidityKey) + humidityKey.length(); + int humidityEnd = json.indexOf(",", humidityStart); + String humidity = json.substring(humidityStart, humidityEnd); + + String iconKey = "\"icon\":\""; + int iconStart = json.indexOf(iconKey) + iconKey.length(); + int iconEnd = json.indexOf("\"", iconStart); + String icon = json.substring(iconStart, iconEnd); + + String regionKey = "\"region\":\""; + int regionStart = json.indexOf(regionKey) + regionKey.length(); + int regionEnd = json.indexOf("\"", regionStart); + String region = json.substring(regionStart, regionEnd); + + String countryKey = "\"country\":\""; + int countryStart = json.indexOf(countryKey) + countryKey.length(); + int countryEnd = json.indexOf("\"", countryStart); + String country = json.substring(countryStart, countryEnd); + + event.sendResponse("__Conditions for " + city + ", "+country+".__" ); + event.sendResponse("Temperature (F): "+temp); + event.sendResponse("Humidity (%): "+humidity); + event.sendResponse("https:"+icon); + + // TODO: Make a thread run this code so that I can have an external Timer + // and after some time has elapsed interrupt and say "city not found" - // do the cumbersome block(String.class).substring() method to get values when blocking to string instead of wrapper + } + if(mc.trim().toLowerCase().startsWith(COMMAND_)) { + String city = mc.trim().substring(COMMAND.length()+1); + String json = getCurrentWeather(city); + event.sendResponse(json); } } diff --git a/src/test/java/org/jointheleague/features/student/first_feature/WeatherTest.java b/src/test/java/org/jointheleague/features/student/first_feature/WeatherTest.java new file mode 100644 index 00000000..b87e6765 --- /dev/null +++ b/src/test/java/org/jointheleague/features/student/first_feature/WeatherTest.java @@ -0,0 +1,33 @@ +package org.jointheleague.features.student.first_feature; + +import org.jointheleague.api_wrapper.ReceivedMessage; +import org.jointheleague.features.help_embed.plain_old_java_objects.help_embed.HelpEmbed; +import org.jointheleague.features.templates.FeatureTemplate; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; +import org.mockito.MockitoAnnotations; + +import java.io.ByteArrayOutputStream; +import java.io.PrintStream; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNotEquals; +import static org.mockito.ArgumentMatchers.anyString; +import static org.mockito.Mockito.*; + +public class WeatherTest { + + private final String testChannelName = "test"; + WeatherAPI test = new WeatherAPI(testChannelName); + + private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); + private final PrintStream originalOut = System.out; + + public final String COMMAND = "weather"; + public final String COMMAND_ = "raw_weather"; + + +} From 3c344cc4818e373bae3b8a0cc344f2c40169d5ac Mon Sep 17 00:00:00 2001 From: league Date: Wed, 17 Sep 2025 16:44:38 -0700 Subject: [PATCH 10/10] Weather API/WeatherTesting DONE --- .../student/first_feature/Condition.java | 121 ++- .../student/first_feature/Current.java | 812 ++++++++++++------ .../student/first_feature/CurrentWeather.java | 110 ++- .../student/first_feature/Location.java | 268 ++++-- .../student/first_feature/WeatherAPI.java | 52 +- .../student/first_feature/WeatherTest.java | 131 ++- 6 files changed, 1040 insertions(+), 454 deletions(-) diff --git a/src/main/java/org/jointheleague/features/student/first_feature/Condition.java b/src/main/java/org/jointheleague/features/student/first_feature/Condition.java index fa45f314..ffdacf85 100644 --- a/src/main/java/org/jointheleague/features/student/first_feature/Condition.java +++ b/src/main/java/org/jointheleague/features/student/first_feature/Condition.java @@ -1,49 +1,100 @@ + package org.jointheleague.features.student.first_feature; +import java.util.LinkedHashMap; +import java.util.Map; import javax.annotation.Generated; -import com.google.gson.annotations.Expose; -import com.google.gson.annotations.SerializedName; +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonPropertyOrder({ + "text", + "icon", + "code" +}) @Generated("jsonschema2pojo") public class Condition { -@SerializedName("text") -@Expose -private String text; -@SerializedName("icon") -@Expose -private String icon; -@SerializedName("code") -@Expose -private Integer code; - -public String getText() { -return text; -} + @JsonProperty("text") + private String text; + @JsonProperty("icon") + private String icon; + @JsonProperty("code") + private Integer code; + @JsonIgnore + private Map additionalProperties = new LinkedHashMap(); -public void setText(String text) { -this.text = text; -} + @JsonProperty("text") + public String getText() { + return text; + } -public String getIcon() { -return icon; -} + @JsonProperty("text") + public void setText(String text) { + this.text = text; + } -public void setIcon(String icon) { -this.icon = icon; -} + @JsonProperty("icon") + public String getIcon() { + return icon; + } -public Integer getCode() { -return code; -} + @JsonProperty("icon") + public void setIcon(String icon) { + this.icon = icon; + } -public void setCode(Integer code) { -this.code = code; -} + @JsonProperty("code") + public Integer getCode() { + return code; + } -@Override -public String toString() { - return "Condition [text=" + text + ", icon=" + icon + ", code=" + code + "]"; -} + @JsonProperty("code") + public void setCode(Integer code) { + this.code = code; + } + + @JsonAnyGetter + public Map getAdditionalProperties() { + return this.additionalProperties; + } -} \ No newline at end of file + @JsonAnySetter + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(Condition.class.getName()).append('@').append(Integer.toHexString(System.identityHashCode(this))).append('['); + sb.append("text"); + sb.append('='); + sb.append(((this.text == null)?"":this.text)); + sb.append(','); + sb.append("icon"); + sb.append('='); + sb.append(((this.icon == null)?"":this.icon)); + sb.append(','); + sb.append("code"); + sb.append('='); + sb.append(((this.code == null)?"":this.code)); + sb.append(','); + sb.append("additionalProperties"); + sb.append('='); + sb.append(((this.additionalProperties == null)?"":this.additionalProperties)); + sb.append(','); + if (sb.charAt((sb.length()- 1)) == ',') { + sb.setCharAt((sb.length()- 1), ']'); + } else { + sb.append(']'); + } + return sb.toString(); + } + +} diff --git a/src/main/java/org/jointheleague/features/student/first_feature/Current.java b/src/main/java/org/jointheleague/features/student/first_feature/Current.java index 70f87a84..c42f7c67 100644 --- a/src/main/java/org/jointheleague/features/student/first_feature/Current.java +++ b/src/main/java/org/jointheleague/features/student/first_feature/Current.java @@ -1,286 +1,542 @@ + package org.jointheleague.features.student.first_feature; +import java.util.LinkedHashMap; +import java.util.Map; import javax.annotation.Generated; -import com.google.gson.annotations.Expose; -import com.google.gson.annotations.SerializedName; - +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonPropertyOrder({ + "last_updated_epoch", + "last_updated", + "temp_c", + "temp_f", + "is_day", + "condition", + "wind_mph", + "wind_kph", + "wind_degree", + "wind_dir", + "pressure_mb", + "pressure_in", + "precip_mm", + "precip_in", + "humidity", + "cloud", + "feelslike_c", + "feelslike_f", + "windchill_c", + "windchill_f", + "heatindex_c", + "heatindex_f", + "dewpoint_c", + "dewpoint_f", + "vis_km", + "vis_miles", + "uv", + "gust_mph", + "gust_kph" +}) @Generated("jsonschema2pojo") public class Current { -@SerializedName("last_updated_epoch") -@Expose -private Integer lastUpdatedEpoch; -@SerializedName("last_updated") -@Expose -private String lastUpdated; -@SerializedName("temp_c") -@Expose -private Float tempC; -@SerializedName("temp_f") -@Expose -private Float tempF; -@SerializedName("is_day") -@Expose -private Integer isDay; -@SerializedName("condition") -@Expose -private Condition condition; -@SerializedName("wind_mph") -@Expose -private Float windMph; -@SerializedName("wind_kph") -@Expose -private Float windKph; -@SerializedName("wind_degree") -@Expose -private Integer windDegree; -@SerializedName("wind_dir") -@Expose -private String windDir; -@SerializedName("pressure_mb") -@Expose -private Integer pressureMb; -@SerializedName("pressure_in") -@Expose -private Float pressureIn; -@SerializedName("precip_mm") -@Expose -private Integer precipMm; -@SerializedName("precip_in") -@Expose -private Integer precipIn; -@SerializedName("humidity") -@Expose -private Integer humidity; -@SerializedName("cloud") -@Expose -private Integer cloud; -@SerializedName("feelslike_c") -@Expose -private Integer feelslikeC; -@SerializedName("feelslike_f") -@Expose -private Float feelslikeF; -@SerializedName("vis_km") -@Expose -private Integer visKm; -@SerializedName("vis_miles") -@Expose -private Integer visMiles; -@SerializedName("uv") -@Expose -private Float uv; -@SerializedName("gust_mph") -@Expose -private Float gustMph; -@SerializedName("gust_kph") -@Expose -private Float gustKph; -//@SerializedName("air_quality") -//@Expose -//private AirQuality airQuality; - -public Integer getLastUpdatedEpoch() { -return lastUpdatedEpoch; -} - -public void setLastUpdatedEpoch(Integer lastUpdatedEpoch) { -this.lastUpdatedEpoch = lastUpdatedEpoch; -} - -public String getLastUpdated() { -return lastUpdated; -} - -public void setLastUpdated(String lastUpdated) { -this.lastUpdated = lastUpdated; -} - -public Float getTempC() { -return tempC; -} - -public void setTempC(Float tempC) { -this.tempC = tempC; -} - -public Float getTempF() { -return tempF; -} - -public void setTempF(Float tempF) { -this.tempF = tempF; -} - -public Integer getIsDay() { -return isDay; -} - -public void setIsDay(Integer isDay) { -this.isDay = isDay; -} - -public Condition getCondition() { -return condition; -} - -public void setCondition(Condition condition) { -this.condition = condition; -} - -public Float getWindMph() { -return windMph; -} - -public void setWindMph(Float windMph) { -this.windMph = windMph; -} - -public Float getWindKph() { -return windKph; -} - -public void setWindKph(Float windKph) { -this.windKph = windKph; -} - -public Integer getWindDegree() { -return windDegree; -} - -public void setWindDegree(Integer windDegree) { -this.windDegree = windDegree; -} - -public String getWindDir() { -return windDir; -} - -public void setWindDir(String windDir) { -this.windDir = windDir; -} - -public Integer getPressureMb() { -return pressureMb; -} - -public void setPressureMb(Integer pressureMb) { -this.pressureMb = pressureMb; -} + @JsonProperty("last_updated_epoch") + private Integer lastUpdatedEpoch; + @JsonProperty("last_updated") + private String lastUpdated; + @JsonProperty("temp_c") + private Double tempC; + @JsonProperty("temp_f") + private Double tempF; + @JsonProperty("is_day") + private Integer isDay; + @JsonProperty("condition") + private Condition condition; + @JsonProperty("wind_mph") + private Double windMph; + @JsonProperty("wind_kph") + private Double windKph; + @JsonProperty("wind_degree") + private Integer windDegree; + @JsonProperty("wind_dir") + private String windDir; + @JsonProperty("pressure_mb") + private Double pressureMb; + @JsonProperty("pressure_in") + private Double pressureIn; + @JsonProperty("precip_mm") + private Double precipMm; + @JsonProperty("precip_in") + private Double precipIn; + @JsonProperty("humidity") + private Integer humidity; + @JsonProperty("cloud") + private Integer cloud; + @JsonProperty("feelslike_c") + private Double feelslikeC; + @JsonProperty("feelslike_f") + private Double feelslikeF; + @JsonProperty("windchill_c") + private Double windchillC; + @JsonProperty("windchill_f") + private Double windchillF; + @JsonProperty("heatindex_c") + private Double heatindexC; + @JsonProperty("heatindex_f") + private Double heatindexF; + @JsonProperty("dewpoint_c") + private Double dewpointC; + @JsonProperty("dewpoint_f") + private Double dewpointF; + @JsonProperty("vis_km") + private Double visKm; + @JsonProperty("vis_miles") + private Double visMiles; + @JsonProperty("uv") + private Double uv; + @JsonProperty("gust_mph") + private Double gustMph; + @JsonProperty("gust_kph") + private Double gustKph; + @JsonIgnore + private Map additionalProperties = new LinkedHashMap(); + + @JsonProperty("last_updated_epoch") + public Integer getLastUpdatedEpoch() { + return lastUpdatedEpoch; + } + + @JsonProperty("last_updated_epoch") + public void setLastUpdatedEpoch(Integer lastUpdatedEpoch) { + this.lastUpdatedEpoch = lastUpdatedEpoch; + } + + @JsonProperty("last_updated") + public String getLastUpdated() { + return lastUpdated; + } + + @JsonProperty("last_updated") + public void setLastUpdated(String lastUpdated) { + this.lastUpdated = lastUpdated; + } + + @JsonProperty("temp_c") + public Double getTempC() { + return tempC; + } + + @JsonProperty("temp_c") + public void setTempC(Double tempC) { + this.tempC = tempC; + } + + @JsonProperty("temp_f") + public Double getTempF() { + return tempF; + } + + @JsonProperty("temp_f") + public void setTempF(Double tempF) { + this.tempF = tempF; + } + + @JsonProperty("is_day") + public Integer getIsDay() { + return isDay; + } + + @JsonProperty("is_day") + public void setIsDay(Integer isDay) { + this.isDay = isDay; + } + + @JsonProperty("condition") + public Condition getCondition() { + return condition; + } + + @JsonProperty("condition") + public void setCondition(Condition condition) { + this.condition = condition; + } + + @JsonProperty("wind_mph") + public Double getWindMph() { + return windMph; + } + + @JsonProperty("wind_mph") + public void setWindMph(Double windMph) { + this.windMph = windMph; + } + + @JsonProperty("wind_kph") + public Double getWindKph() { + return windKph; + } + + @JsonProperty("wind_kph") + public void setWindKph(Double windKph) { + this.windKph = windKph; + } + + @JsonProperty("wind_degree") + public Integer getWindDegree() { + return windDegree; + } + + @JsonProperty("wind_degree") + public void setWindDegree(Integer windDegree) { + this.windDegree = windDegree; + } + + @JsonProperty("wind_dir") + public String getWindDir() { + return windDir; + } + + @JsonProperty("wind_dir") + public void setWindDir(String windDir) { + this.windDir = windDir; + } + + @JsonProperty("pressure_mb") + public Double getPressureMb() { + return pressureMb; + } + + @JsonProperty("pressure_mb") + public void setPressureMb(Double pressureMb) { + this.pressureMb = pressureMb; + } + + @JsonProperty("pressure_in") + public Double getPressureIn() { + return pressureIn; + } + + @JsonProperty("pressure_in") + public void setPressureIn(Double pressureIn) { + this.pressureIn = pressureIn; + } + + @JsonProperty("precip_mm") + public Double getPrecipMm() { + return precipMm; + } + + @JsonProperty("precip_mm") + public void setPrecipMm(Double precipMm) { + this.precipMm = precipMm; + } + + @JsonProperty("precip_in") + public Double getPrecipIn() { + return precipIn; + } + + @JsonProperty("precip_in") + public void setPrecipIn(Double precipIn) { + this.precipIn = precipIn; + } + + @JsonProperty("humidity") + public Integer getHumidity() { + return humidity; + } + + @JsonProperty("humidity") + public void setHumidity(Integer humidity) { + this.humidity = humidity; + } + + @JsonProperty("cloud") + public Integer getCloud() { + return cloud; + } + + @JsonProperty("cloud") + public void setCloud(Integer cloud) { + this.cloud = cloud; + } + + @JsonProperty("feelslike_c") + public Double getFeelslikeC() { + return feelslikeC; + } + + @JsonProperty("feelslike_c") + public void setFeelslikeC(Double feelslikeC) { + this.feelslikeC = feelslikeC; + } + + @JsonProperty("feelslike_f") + public Double getFeelslikeF() { + return feelslikeF; + } + + @JsonProperty("feelslike_f") + public void setFeelslikeF(Double feelslikeF) { + this.feelslikeF = feelslikeF; + } + + @JsonProperty("windchill_c") + public Double getWindchillC() { + return windchillC; + } + + @JsonProperty("windchill_c") + public void setWindchillC(Double windchillC) { + this.windchillC = windchillC; + } + + @JsonProperty("windchill_f") + public Double getWindchillF() { + return windchillF; + } + + @JsonProperty("windchill_f") + public void setWindchillF(Double windchillF) { + this.windchillF = windchillF; + } + + @JsonProperty("heatindex_c") + public Double getHeatindexC() { + return heatindexC; + } + + @JsonProperty("heatindex_c") + public void setHeatindexC(Double heatindexC) { + this.heatindexC = heatindexC; + } + + @JsonProperty("heatindex_f") + public Double getHeatindexF() { + return heatindexF; + } + + @JsonProperty("heatindex_f") + public void setHeatindexF(Double heatindexF) { + this.heatindexF = heatindexF; + } + + @JsonProperty("dewpoint_c") + public Double getDewpointC() { + return dewpointC; + } + + @JsonProperty("dewpoint_c") + public void setDewpointC(Double dewpointC) { + this.dewpointC = dewpointC; + } + + @JsonProperty("dewpoint_f") + public Double getDewpointF() { + return dewpointF; + } + + @JsonProperty("dewpoint_f") + public void setDewpointF(Double dewpointF) { + this.dewpointF = dewpointF; + } + + @JsonProperty("vis_km") + public Double getVisKm() { + return visKm; + } + + @JsonProperty("vis_km") + public void setVisKm(Double visKm) { + this.visKm = visKm; + } + + @JsonProperty("vis_miles") + public Double getVisMiles() { + return visMiles; + } + + @JsonProperty("vis_miles") + public void setVisMiles(Double visMiles) { + this.visMiles = visMiles; + } + + @JsonProperty("uv") + public Double getUv() { + return uv; + } + + @JsonProperty("uv") + public void setUv(Double uv) { + this.uv = uv; + } + + @JsonProperty("gust_mph") + public Double getGustMph() { + return gustMph; + } + + @JsonProperty("gust_mph") + public void setGustMph(Double gustMph) { + this.gustMph = gustMph; + } + + @JsonProperty("gust_kph") + public Double getGustKph() { + return gustKph; + } + + @JsonProperty("gust_kph") + public void setGustKph(Double gustKph) { + this.gustKph = gustKph; + } + + @JsonAnyGetter + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + @JsonAnySetter + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(Current.class.getName()).append('@').append(Integer.toHexString(System.identityHashCode(this))).append('['); + sb.append("lastUpdatedEpoch"); + sb.append('='); + sb.append(((this.lastUpdatedEpoch == null)?"":this.lastUpdatedEpoch)); + sb.append(','); + sb.append("lastUpdated"); + sb.append('='); + sb.append(((this.lastUpdated == null)?"":this.lastUpdated)); + sb.append(','); + sb.append("tempC"); + sb.append('='); + sb.append(((this.tempC == null)?"":this.tempC)); + sb.append(','); + sb.append("tempF"); + sb.append('='); + sb.append(((this.tempF == null)?"":this.tempF)); + sb.append(','); + sb.append("isDay"); + sb.append('='); + sb.append(((this.isDay == null)?"":this.isDay)); + sb.append(','); + sb.append("condition"); + sb.append('='); + sb.append(((this.condition == null)?"":this.condition)); + sb.append(','); + sb.append("windMph"); + sb.append('='); + sb.append(((this.windMph == null)?"":this.windMph)); + sb.append(','); + sb.append("windKph"); + sb.append('='); + sb.append(((this.windKph == null)?"":this.windKph)); + sb.append(','); + sb.append("windDegree"); + sb.append('='); + sb.append(((this.windDegree == null)?"":this.windDegree)); + sb.append(','); + sb.append("windDir"); + sb.append('='); + sb.append(((this.windDir == null)?"":this.windDir)); + sb.append(','); + sb.append("pressureMb"); + sb.append('='); + sb.append(((this.pressureMb == null)?"":this.pressureMb)); + sb.append(','); + sb.append("pressureIn"); + sb.append('='); + sb.append(((this.pressureIn == null)?"":this.pressureIn)); + sb.append(','); + sb.append("precipMm"); + sb.append('='); + sb.append(((this.precipMm == null)?"":this.precipMm)); + sb.append(','); + sb.append("precipIn"); + sb.append('='); + sb.append(((this.precipIn == null)?"":this.precipIn)); + sb.append(','); + sb.append("humidity"); + sb.append('='); + sb.append(((this.humidity == null)?"":this.humidity)); + sb.append(','); + sb.append("cloud"); + sb.append('='); + sb.append(((this.cloud == null)?"":this.cloud)); + sb.append(','); + sb.append("feelslikeC"); + sb.append('='); + sb.append(((this.feelslikeC == null)?"":this.feelslikeC)); + sb.append(','); + sb.append("feelslikeF"); + sb.append('='); + sb.append(((this.feelslikeF == null)?"":this.feelslikeF)); + sb.append(','); + sb.append("windchillC"); + sb.append('='); + sb.append(((this.windchillC == null)?"":this.windchillC)); + sb.append(','); + sb.append("windchillF"); + sb.append('='); + sb.append(((this.windchillF == null)?"":this.windchillF)); + sb.append(','); + sb.append("heatindexC"); + sb.append('='); + sb.append(((this.heatindexC == null)?"":this.heatindexC)); + sb.append(','); + sb.append("heatindexF"); + sb.append('='); + sb.append(((this.heatindexF == null)?"":this.heatindexF)); + sb.append(','); + sb.append("dewpointC"); + sb.append('='); + sb.append(((this.dewpointC == null)?"":this.dewpointC)); + sb.append(','); + sb.append("dewpointF"); + sb.append('='); + sb.append(((this.dewpointF == null)?"":this.dewpointF)); + sb.append(','); + sb.append("visKm"); + sb.append('='); + sb.append(((this.visKm == null)?"":this.visKm)); + sb.append(','); + sb.append("visMiles"); + sb.append('='); + sb.append(((this.visMiles == null)?"":this.visMiles)); + sb.append(','); + sb.append("uv"); + sb.append('='); + sb.append(((this.uv == null)?"":this.uv)); + sb.append(','); + sb.append("gustMph"); + sb.append('='); + sb.append(((this.gustMph == null)?"":this.gustMph)); + sb.append(','); + sb.append("gustKph"); + sb.append('='); + sb.append(((this.gustKph == null)?"":this.gustKph)); + sb.append(','); + sb.append("additionalProperties"); + sb.append('='); + sb.append(((this.additionalProperties == null)?"":this.additionalProperties)); + sb.append(','); + if (sb.charAt((sb.length()- 1)) == ',') { + sb.setCharAt((sb.length()- 1), ']'); + } else { + sb.append(']'); + } + return sb.toString(); + } -public Float getPressureIn() { -return pressureIn; } - -public void setPressureIn(Float pressureIn) { -this.pressureIn = pressureIn; -} - -public Integer getPrecipMm() { -return precipMm; -} - -public void setPrecipMm(Integer precipMm) { -this.precipMm = precipMm; -} - -public Integer getPrecipIn() { -return precipIn; -} - -public void setPrecipIn(Integer precipIn) { -this.precipIn = precipIn; -} - -public Integer getHumidity() { -return humidity; -} - -public void setHumidity(Integer humidity) { -this.humidity = humidity; -} - -public Integer getCloud() { -return cloud; -} - -public void setCloud(Integer cloud) { -this.cloud = cloud; -} - -public Integer getFeelslikeC() { -return feelslikeC; -} - -public void setFeelslikeC(Integer feelslikeC) { -this.feelslikeC = feelslikeC; -} - -public Float getFeelslikeF() { -return feelslikeF; -} - -public void setFeelslikeF(Float feelslikeF) { -this.feelslikeF = feelslikeF; -} - -public Integer getVisKm() { -return visKm; -} - -public void setVisKm(Integer visKm) { -this.visKm = visKm; -} - -public Integer getVisMiles() { -return visMiles; -} - -public void setVisMiles(Integer visMiles) { -this.visMiles = visMiles; -} - -public Float getUv() { -return uv; -} - -public void setUv(Float uv) { -this.uv = uv; -} - -public Float getGustMph() { -return gustMph; -} - -public void setGustMph(Float gustMph) { -this.gustMph = gustMph; -} - -public Float getGustKph() { -return gustKph; -} - -public void setGustKph(Float gustKph) { -this.gustKph = gustKph; -} - -@Override -public String toString() { - return "Current [lastUpdatedEpoch=" + lastUpdatedEpoch + ", lastUpdated=" + lastUpdated + ", tempC=" + tempC - + ", tempF=" + tempF + ", isDay=" + isDay + ", condition=" + condition + ", windMph=" + windMph - + ", windKph=" + windKph + ", windDegree=" + windDegree + ", windDir=" + windDir + ", pressureMb=" - + pressureMb + ", pressureIn=" + pressureIn + ", precipMm=" + precipMm + ", precipIn=" + precipIn - + ", humidity=" + humidity + ", cloud=" + cloud + ", feelslikeC=" + feelslikeC + ", feelslikeF=" - + feelslikeF + ", visKm=" + visKm + ", visMiles=" + visMiles + ", uv=" + uv + ", gustMph=" + gustMph - + ", gustKph=" + gustKph + "]"; -} - -//public AirQuality getAirQuality() { -//return airQuality; -//} -// -//public void setAirQuality(AirQuality airQuality) { -//this.airQuality = airQuality; -//} - -} \ No newline at end of file diff --git a/src/main/java/org/jointheleague/features/student/first_feature/CurrentWeather.java b/src/main/java/org/jointheleague/features/student/first_feature/CurrentWeather.java index 50b03951..b06e27ca 100644 --- a/src/main/java/org/jointheleague/features/student/first_feature/CurrentWeather.java +++ b/src/main/java/org/jointheleague/features/student/first_feature/CurrentWeather.java @@ -1,41 +1,83 @@ + package org.jointheleague.features.student.first_feature; +import java.util.LinkedHashMap; +import java.util.Map; import javax.annotation.Generated; +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; -import com.google.gson.annotations.Expose; -import com.google.gson.annotations.SerializedName; - +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonPropertyOrder({ + "location", + "current" +}) @Generated("jsonschema2pojo") public class CurrentWeather { - @SerializedName("location") - @Expose - private Location location; - @SerializedName("current") - @Expose - private Current current; - - public Location getLocation() { - return location; - } - - public void setLocation(Location location) { - this.location = location; - } - - public Current getCurrent() { - return current; - } - - public void setCurrent(Current current) { - this.current = current; - } - - @Override - public String toString() { - return "CurrentWeather [location=" + location + ", current=" + current + "]"; - } - - - -} \ No newline at end of file + @JsonProperty("location") + private Location location; + @JsonProperty("current") + private Current current; + @JsonIgnore + private Map additionalProperties = new LinkedHashMap(); + + @JsonProperty("location") + public Location getLocation() { + return location; + } + + @JsonProperty("location") + public void setLocation(Location location) { + this.location = location; + } + + @JsonProperty("current") + public Current getCurrent() { + return current; + } + + @JsonProperty("current") + public void setCurrent(Current current) { + this.current = current; + } + + @JsonAnyGetter + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + @JsonAnySetter + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(CurrentWeather.class.getName()).append('@').append(Integer.toHexString(System.identityHashCode(this))).append('['); + sb.append("location"); + sb.append('='); + sb.append(((this.location == null)?"":this.location)); + sb.append(','); + sb.append("current"); + sb.append('='); + sb.append(((this.current == null)?"":this.current)); + sb.append(','); + sb.append("additionalProperties"); + sb.append('='); + sb.append(((this.additionalProperties == null)?"":this.additionalProperties)); + sb.append(','); + if (sb.charAt((sb.length()- 1)) == ',') { + sb.setCharAt((sb.length()- 1), ']'); + } else { + sb.append(']'); + } + return sb.toString(); + } + +} diff --git a/src/main/java/org/jointheleague/features/student/first_feature/Location.java b/src/main/java/org/jointheleague/features/student/first_feature/Location.java index b959cc56..6cef34e1 100644 --- a/src/main/java/org/jointheleague/features/student/first_feature/Location.java +++ b/src/main/java/org/jointheleague/features/student/first_feature/Location.java @@ -1,99 +1,185 @@ + package org.jointheleague.features.student.first_feature; +import java.util.LinkedHashMap; +import java.util.Map; import javax.annotation.Generated; -import com.google.gson.annotations.Expose; -import com.google.gson.annotations.SerializedName; - +import com.fasterxml.jackson.annotation.JsonAnyGetter; +import com.fasterxml.jackson.annotation.JsonAnySetter; +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonInclude; +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.annotation.JsonPropertyOrder; + +@JsonInclude(JsonInclude.Include.NON_NULL) +@JsonPropertyOrder({ + "name", + "region", + "country", + "lat", + "lon", + "tz_id", + "localtime_epoch", + "localtime" +}) @Generated("jsonschema2pojo") public class Location { -@SerializedName("name") -@Expose -private String name; -@SerializedName("region") -@Expose -private String region; -@SerializedName("country") -@Expose -private String country; -@SerializedName("lat") -@Expose -private Double lat; -@SerializedName("lon") -@Expose -private Double lon; -@SerializedName("tz_id") -@Expose -private String tzId; -@SerializedName("localtime_epoch") -@Expose -private Integer localtimeEpoch; -@SerializedName("localtime") -@Expose -private String localtime; - -public String getName() { -return name; -} - -public void setName(String name) { -this.name = name; -} - -public String getRegion() { -return region; -} - -public void setRegion(String region) { -this.region = region; -} - -public String getCountry() { -return country; -} - -public void setCountry(String country) { -this.country = country; -} - -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 String getTzId() { -return tzId; -} - -public void setTzId(String tzId) { -this.tzId = tzId; -} - -public Integer getLocaltimeEpoch() { -return localtimeEpoch; -} - -public void setLocaltimeEpoch(Integer localtimeEpoch) { -this.localtimeEpoch = localtimeEpoch; -} - -public String getLocaltime() { -return localtime; -} + @JsonProperty("name") + private String name; + @JsonProperty("region") + private String region; + @JsonProperty("country") + private String country; + @JsonProperty("lat") + private Double lat; + @JsonProperty("lon") + private Double lon; + @JsonProperty("tz_id") + private String tzId; + @JsonProperty("localtime_epoch") + private Integer localtimeEpoch; + @JsonProperty("localtime") + private String localtime; + @JsonIgnore + private Map additionalProperties = new LinkedHashMap(); + + @JsonProperty("name") + public String getName() { + return name; + } + + @JsonProperty("name") + public void setName(String name) { + this.name = name; + } + + @JsonProperty("region") + public String getRegion() { + return region; + } + + @JsonProperty("region") + public void setRegion(String region) { + this.region = region; + } + + @JsonProperty("country") + public String getCountry() { + return country; + } + + @JsonProperty("country") + public void setCountry(String country) { + this.country = country; + } + + @JsonProperty("lat") + public Double getLat() { + return lat; + } + + @JsonProperty("lat") + public void setLat(Double lat) { + this.lat = lat; + } + + @JsonProperty("lon") + public Double getLon() { + return lon; + } + + @JsonProperty("lon") + public void setLon(Double lon) { + this.lon = lon; + } + + @JsonProperty("tz_id") + public String getTzId() { + return tzId; + } + + @JsonProperty("tz_id") + public void setTzId(String tzId) { + this.tzId = tzId; + } + + @JsonProperty("localtime_epoch") + public Integer getLocaltimeEpoch() { + return localtimeEpoch; + } + + @JsonProperty("localtime_epoch") + public void setLocaltimeEpoch(Integer localtimeEpoch) { + this.localtimeEpoch = localtimeEpoch; + } + + @JsonProperty("localtime") + public String getLocaltime() { + return localtime; + } + + @JsonProperty("localtime") + public void setLocaltime(String localtime) { + this.localtime = localtime; + } + + @JsonAnyGetter + public Map getAdditionalProperties() { + return this.additionalProperties; + } + + @JsonAnySetter + public void setAdditionalProperty(String name, Object value) { + this.additionalProperties.put(name, value); + } + + @Override + public String toString() { + StringBuilder sb = new StringBuilder(); + sb.append(Location.class.getName()).append('@').append(Integer.toHexString(System.identityHashCode(this))).append('['); + sb.append("name"); + sb.append('='); + sb.append(((this.name == null)?"":this.name)); + sb.append(','); + sb.append("region"); + sb.append('='); + sb.append(((this.region == null)?"":this.region)); + sb.append(','); + sb.append("country"); + sb.append('='); + sb.append(((this.country == null)?"":this.country)); + sb.append(','); + sb.append("lat"); + sb.append('='); + sb.append(((this.lat == null)?"":this.lat)); + sb.append(','); + sb.append("lon"); + sb.append('='); + sb.append(((this.lon == null)?"":this.lon)); + sb.append(','); + sb.append("tzId"); + sb.append('='); + sb.append(((this.tzId == null)?"":this.tzId)); + sb.append(','); + sb.append("localtimeEpoch"); + sb.append('='); + sb.append(((this.localtimeEpoch == null)?"":this.localtimeEpoch)); + sb.append(','); + sb.append("localtime"); + sb.append('='); + sb.append(((this.localtime == null)?"":this.localtime)); + sb.append(','); + sb.append("additionalProperties"); + sb.append('='); + sb.append(((this.additionalProperties == null)?"":this.additionalProperties)); + sb.append(','); + if (sb.charAt((sb.length()- 1)) == ',') { + sb.setCharAt((sb.length()- 1), ']'); + } else { + sb.append(']'); + } + return sb.toString(); + } -public void setLocaltime(String localtime) { -this.localtime = localtime; } - -} \ No newline at end of file diff --git a/src/main/java/org/jointheleague/features/student/first_feature/WeatherAPI.java b/src/main/java/org/jointheleague/features/student/first_feature/WeatherAPI.java index 51311aff..b16e4a71 100644 --- a/src/main/java/org/jointheleague/features/student/first_feature/WeatherAPI.java +++ b/src/main/java/org/jointheleague/features/student/first_feature/WeatherAPI.java @@ -12,12 +12,12 @@ public class WeatherAPI extends FeatureTemplate{ public final String COMMAND = "weather"; -public final String COMMAND_ = "raw_weather"; +//public final String COMMAND_ = "raw_weather"; private static final String key = "7a59fc69444d4617a5a225318250309"; private static final String URL = "http://api.weatherapi.com/v1/current.json"; - private WebClient webClient = WebClient.create(URL); + private WebClient webClient; public WeatherAPI(String channelName) { super(channelName); @@ -29,8 +29,15 @@ public WeatherAPI(String channelName) { .build(); } + public WeatherAPI(String channelName, WebClient wc) { + super(channelName); + // TODO Auto-generated constructor stub + + this.webClient = wc; + } - + + /*@Deprecated public String getCurrentWeather(String city) { Mono apiExampleWrapperMono = webClient.get() @@ -43,9 +50,22 @@ public String getCurrentWeather(String city) { return apiExampleWrapperMono.block(); - } + }*/ + public CurrentWeather getCurrentWeather2(String city) { + + Mono apiExampleWrapperMono = webClient.get() + .uri(uriBuilder -> uriBuilder + .queryParam("q", city) + .queryParam("key", key) + .build()) + .retrieve() + .bodyToMono(CurrentWeather.class); + + return apiExampleWrapperMono.block(); + } + /*public void printCurrentWeather(String city) { Mono apiExampleWrapperMono = webClient.get() @@ -62,19 +82,21 @@ public String getCurrentWeather(String city) { public void handle(ReceivedMessage event) { String mc = event.getMessageContent(); - if(mc.trim().toLowerCase().equals(COMMAND)||mc.trim().toLowerCase().equals(COMMAND_)) { + if(mc.trim().toLowerCase().equals(COMMAND)/*||mc.trim().toLowerCase().equals(COMMAND_)*/) { event.sendResponse("Please add a city."); } - if(mc.trim().toLowerCase().startsWith(COMMAND)) { + else if(mc.trim().toLowerCase().startsWith(COMMAND)) { String city = mc.trim().substring(COMMAND.length()+1); - String json = getCurrentWeather(city); + //String json = getCurrentWeather(city); + + CurrentWeather actualObject = getCurrentWeather2(city); //event.sendResponse(json); - String tempKey = "\"temp_f\":"; + /*String tempKey = "\"temp_f\":"; int tempStart = json.indexOf(tempKey) + tempKey.length(); int tempEnd = json.indexOf(",", tempStart); String temp = json.substring(tempStart, tempEnd); @@ -98,24 +120,24 @@ public void handle(ReceivedMessage event) { String countryKey = "\"country\":\""; int countryStart = json.indexOf(countryKey) + countryKey.length(); int countryEnd = json.indexOf("\"", countryStart); - String country = json.substring(countryStart, countryEnd); + String country = json.substring(countryStart, countryEnd);*/ - event.sendResponse("__Conditions for " + city + ", "+country+".__" ); - event.sendResponse("Temperature (F): "+temp); - event.sendResponse("Humidity (%): "+humidity); - event.sendResponse("https:"+icon); + event.sendResponse("__Conditions for " + city + ", "+actualObject.getLocation().getCountry()+".__" ); + event.sendResponse("Temperature (F): "+actualObject.getCurrent().getTempF()); + event.sendResponse("Humidity (%): "+actualObject.getCurrent().getHumidity()); + event.sendResponse("https:"+actualObject.getCurrent().getCondition().getIcon()); // TODO: Make a thread run this code so that I can have an external Timer // and after some time has elapsed interrupt and say "city not found" } - if(mc.trim().toLowerCase().startsWith(COMMAND_)) { + /*if(mc.trim().toLowerCase().startsWith(COMMAND_)) { String city = mc.trim().substring(COMMAND.length()+1); String json = getCurrentWeather(city); event.sendResponse(json); - } + }*/ } diff --git a/src/test/java/org/jointheleague/features/student/first_feature/WeatherTest.java b/src/test/java/org/jointheleague/features/student/first_feature/WeatherTest.java index b87e6765..1508926e 100644 --- a/src/test/java/org/jointheleague/features/student/first_feature/WeatherTest.java +++ b/src/test/java/org/jointheleague/features/student/first_feature/WeatherTest.java @@ -8,9 +8,17 @@ import org.junit.jupiter.api.Test; import org.mockito.Mock; import org.mockito.MockitoAnnotations; +import org.springframework.web.reactive.function.client.WebClient; +import org.springframework.web.util.UriBuilder; + + +import reactor.core.publisher.Mono; import java.io.ByteArrayOutputStream; import java.io.PrintStream; +import java.util.function.Function; + +import java.net.URI; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotNull; @@ -21,7 +29,7 @@ public class WeatherTest { private final String testChannelName = "test"; - WeatherAPI test = new WeatherAPI(testChannelName); + WeatherAPI test; private final ByteArrayOutputStream outContent = new ByteArrayOutputStream(); private final PrintStream originalOut = System.out; @@ -29,5 +37,126 @@ public class WeatherTest { public final String COMMAND = "weather"; public final String COMMAND_ = "raw_weather"; + @Mock + public WebClient wc; + + @Mock + WebClient.RequestHeadersUriSpec requestHeadersUriSpecMock; + + @Mock + WebClient.RequestHeadersSpec requestHeadersSpecMock; + + @Mock + WebClient.ResponseSpec responseSpecMock; + + @Mock + Mono m_cw; + + @Mock + ReceivedMessage rm; + + @Mock + UriBuilder urib; + + //@Mock + //URI uri; + @BeforeEach + void setUp() { + MockitoAnnotations.openMocks(this); + System.setOut(new PrintStream(outContent)); + test = new WeatherAPI(testChannelName, wc); + } + + @Test + void confirmProperResponse() { + + //Given + String city = "Bob"; + + CurrentWeather cww = new CurrentWeather(); + Current c = new Current(); c.setTempF(37.5); c.setHumidity(30); Condition cond = new Condition(); cond.setIcon("//cdn.weatherapi.com/weather/64x64/day/122.png");c.setCondition(cond); cww.setCurrent(c); + Location l = new Location(); l.setCountry("Boblandia"); cww.setLocation(l); + + //When + when(wc.get()).thenReturn(requestHeadersUriSpecMock); + when(requestHeadersUriSpecMock.uri((Function) any())).thenReturn(requestHeadersSpecMock); + when(requestHeadersSpecMock.retrieve()).thenReturn(responseSpecMock); + when(responseSpecMock.bodyToMono(CurrentWeather.class)).thenReturn(m_cw); + + when(urib.queryParam("q", city)).thenReturn(urib); + when(urib.queryParam("key","7a59fc69444d4617a5a225318250309")).thenReturn(urib); + when(urib.build()).thenReturn(null); + + when(m_cw.block()).thenReturn(cww); + + when(rm.getMessageContent()).thenReturn("weather Bob"); // change this + + test.handle(rm); + + //Then + + verify(rm,times(1)).sendResponse("__Conditions for Bob, Boblandia.__"); + verify(rm,times(1)).sendResponse("Temperature (F): 37.5"); + verify(rm,times(1)).sendResponse("Humidity (%): 30"); + verify(rm,times(1)).sendResponse("https://cdn.weatherapi.com/weather/64x64/day/122.png"); + + + } + + @Test + void equals() { + //Given + String city = "Bob"; + + CurrentWeather cww = new CurrentWeather(); + Current c = new Current(); c.setTempF(37.5); c.setHumidity(30); Condition cond = new Condition(); cond.setIcon("//cdn.weatherapi.com/weather/64x64/day/122.png");c.setCondition(cond); cww.setCurrent(c); + Location l = new Location(); l.setCountry("Boblandia"); cww.setLocation(l); + + //When + when(wc.get()).thenReturn(requestHeadersUriSpecMock); + when(requestHeadersUriSpecMock.uri((Function) any())).thenReturn(requestHeadersSpecMock); + when(requestHeadersSpecMock.retrieve()).thenReturn(responseSpecMock); + when(responseSpecMock.bodyToMono(CurrentWeather.class)).thenReturn(m_cw); + + when(m_cw.block()).thenReturn(cww); + + when(urib.queryParam("q", city)).thenReturn(urib); + when(urib.queryParam("key","7a59fc69444d4617a5a225318250309")).thenReturn(urib); + when(urib.build()).thenReturn(null); + + when(rm.getMessageContent()).thenReturn("weather"); // change this + + test.handle(rm); + + //Then + + verify(rm,times(1)).sendResponse("Please add a city."); + } + + @Test + + void nothing() { + String city = "Bob"; + + CurrentWeather cww = new CurrentWeather(); + Current c = new Current(); c.setTempF(37.5); c.setHumidity(30); Condition cond = new Condition(); cond.setIcon("//cdn.weatherapi.com/weather/64x64/day/122.png");c.setCondition(cond); cww.setCurrent(c); + Location l = new Location(); l.setCountry("Boblandia"); cww.setLocation(l); + + //When + when(wc.get()).thenReturn(requestHeadersUriSpecMock); + when(requestHeadersUriSpecMock.uri((Function) any())).thenReturn(requestHeadersSpecMock); + when(requestHeadersSpecMock.retrieve()).thenReturn(responseSpecMock); + when(responseSpecMock.bodyToMono(CurrentWeather.class)).thenReturn(m_cw); + + when(m_cw.block()).thenReturn(cww); + + when(rm.getMessageContent()).thenReturn("banana"); // change this + + test.handle(rm); + + //Then + + verify(rm,never()).sendResponse(anyString()); + } }