Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.85 KB | None | 0 0
  1. /*
  2.   The MIT License (MIT)
  3.  
  4.   Copyright (c) 2017 JBug:Brasil <contato@jbugbrasil.com.br>
  5.  
  6.   Permission is hereby granted, free of charge, to any person obtaining a copy of
  7.   this software and associated documentation files (the "Software"), to deal in
  8.   the Software without restriction, including without limitation the rights to
  9.   use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
  10.   the Software, and to permit persons to whom the Software is furnished to do so,
  11.   subject to the following conditions:
  12.  
  13.   The above copyright notice and this permission notice shall be included in all
  14.   copies or substantial portions of the Software.
  15.  
  16.   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  17.   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
  18.   FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
  19.   COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
  20.   IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  21.   CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22.  */
  23.  
  24. package br.com.jbugbrasil.bot.plugin.welcome.test;
  25.  
  26. import br.com.jbugbrasil.bot.api.object.MessageUpdate;
  27. import br.com.jbugbrasil.bot.api.object.TelegramResponse;
  28. import br.com.jbugbrasil.bot.plugin.welcome.WelcomeMessagePlugin;
  29. import com.fasterxml.jackson.core.type.TypeReference;
  30. import com.fasterxml.jackson.databind.ObjectMapper;
  31. import org.junit.Assert;
  32. import org.junit.Test;
  33.  
  34. import java.io.IOException;
  35. import java.lang.reflect.Field;
  36. import java.lang.reflect.Modifier;
  37. import java.util.ArrayList;
  38.  
  39. public class WelcomeMessageTest {
  40.  
  41.     private String newMember = "{" +
  42.             "\"ok\": true," +
  43.             "\"result\": [" +
  44.             "{" +
  45.             "\"update_id\": 311105828," +
  46.             "\"message\": {" +
  47.             "\"message_id\": 87073," +
  48.             "\"from\": {" +
  49.             "\"id\": 14289485," +
  50.             "\"is_bot\": false," +
  51.             "\"first_name\": \"Filippe\"," +
  52.             "\"last_name\": \"Spolti\"," +
  53.             "\"username\": \"fspolti\"," +
  54.             "\"language_code\": \"en\"" +
  55.             "}," +
  56.             "\"chat\": {" +
  57.             "\"id\": -234234212," +
  58.             "\"title\": \"JBug Brasil\"," +
  59.             "\"type\": \"group\"," +
  60.             "\"all_members_are_administrators\": true" +
  61.             "}," +
  62.             "\"date\": 1513173785," +
  63.             "\"new_chat_participant\": {" +
  64.             "\"id\": 199267392," +
  65.             "\"is_bot\": false," +
  66.             "\"first_name\": \"Mario\"," +
  67.             "\"last_name\": \"Oliveira\"," +
  68.             "\"username\": \"moliveira\"" +
  69.             "}," +
  70.             "\"new_chat_member\": {" +
  71.             "\"id\": 199267392," +
  72.             "\"is_bot\": false," +
  73.             "\"first_name\": \"Mario\"," +
  74.             "\"last_name\": \"Oliveira\"," +
  75.             "\"username\": \"moliveira\"" +
  76.             "}," +
  77.             "\"new_chat_members\": [" +
  78.             "{" +
  79.             "\"id\": 199267392," +
  80.             "\"is_bot\": false," +
  81.             "\"first_name\": \"Mario\"," +
  82.             "\"last_name\": \"Oliveira\"," +
  83.             "\"username\": \"moliveira\"" +
  84.             "}]}}]}";
  85.  
  86.     private String leftMember = "{" +
  87.             "\"ok\": true," +
  88.             "\"result\": [" +
  89.             "{" +
  90.             "\"update_id\": 311105829," +
  91.             "\"message\": {" +
  92.             "\"message_id\": 87074," +
  93.             "\"from\": {" +
  94.             "\"id\": 14289485," +
  95.             "\"is_bot\": false," +
  96.             "\"first_name\": \"Filippe\"," +
  97.             "\"last_name\": \"Spolti\"," +
  98.             "\"username\": \"fspolti\"," +
  99.             "\"language_code\": \"en\"" +
  100.             "}," +
  101.             "\"chat\": {" +
  102.             "\"id\": -234234212," +
  103.             "\"title\": \"JBug Brasil\"," +
  104.             "\"type\": \"group\"," +
  105.             "\"all_members_are_administrators\": true" +
  106.             "}," +
  107.             "\"date\": 1513173793," +
  108.             "\"left_chat_participant\": {" +
  109.             "\"id\": 199267392," +
  110.             "\"is_bot\": false," +
  111.             "\"first_name\": \"Mario\"," +
  112.             "\"last_name\": \"Oliveira\"," +
  113.             "\"username\": \"moliveira\"" +
  114.             "}," +
  115.             "\"left_chat_member\": {" +
  116.             "\"id\": 199267392," +
  117.             "\"is_bot\": false," +
  118.             "\"first_name\": \"Mario\"," +
  119.             "\"last_name\": \"Oliveira\"," +
  120.             "\"username\": \"moliveira\"" +
  121.             "}}}]}";
  122.  
  123.     @Test
  124.     public void testWelcomeMessage() throws IOException, NoSuchFieldException, IllegalAccessException {
  125.         WelcomeMessagePlugin welcome = new WelcomeMessagePlugin();
  126.         Assert.assertEquals(String.format(getMessage(welcome, "WELCOME_MESSAGE"), "Mario", "JBug Brasil"), welcome.process(processUpdates(newMember).getResult().get(0)));
  127.     }
  128.  
  129.     @Test
  130.     public void testGoodbyeMessage() throws IOException, NoSuchFieldException, IllegalAccessException {
  131.         WelcomeMessagePlugin left = new WelcomeMessagePlugin();
  132.         Assert.assertEquals(String.format(getMessage(left, "GOODBYE_MESSAGE"), "Mario"), left.process(processUpdates(leftMember).getResult().get(0)));
  133.     }
  134.  
  135.     private String getMessage(Object target, String fieldName) throws NoSuchFieldException, IllegalAccessException {
  136.         Field field = WelcomeMessagePlugin.class.getDeclaredField(fieldName);
  137.         field.setAccessible(true);
  138.         return field.get(target).toString();
  139.     }
  140.  
  141.     private TelegramResponse<ArrayList<MessageUpdate>> processUpdates(String update) throws IOException {
  142.         ObjectMapper objectMapper = new ObjectMapper();
  143.         return objectMapper.readValue(update, new TypeReference<TelegramResponse<ArrayList<MessageUpdate>>>() {
  144.         });
  145.     }
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement