FormTest.java

  1. /**
  2.  *
  3.  * Copyright 2004 Jive Software, 2017-2020 Florian Schmaus.
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *     http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17. package org.jivesoftware.smackx.xdata;

  18. import static org.junit.jupiter.api.Assertions.assertEquals;
  19. import static org.junit.jupiter.api.Assertions.assertNotNull;
  20. import static org.junit.jupiter.api.Assertions.assertThrows;

  21. import org.jivesoftware.smack.SmackException.NotConnectedException;
  22. import org.jivesoftware.smack.StanzaCollector;
  23. import org.jivesoftware.smack.filter.ThreadFilter;
  24. import org.jivesoftware.smack.packet.Message;
  25. import org.jivesoftware.smack.packet.StanzaBuilder;

  26. import org.jivesoftware.smackx.xdata.form.FillableForm;
  27. import org.jivesoftware.smackx.xdata.form.Form;
  28. import org.jivesoftware.smackx.xdata.packet.DataForm;

  29. import org.igniterealtime.smack.inttest.AbstractSmackIntegrationTest;
  30. import org.igniterealtime.smack.inttest.SmackIntegrationTestEnvironment;
  31. import org.igniterealtime.smack.inttest.annotations.SmackIntegrationTest;

  32. /**
  33.  * Tests the DataForms extensions.
  34.  *
  35.  * @author Gaston Dombiak
  36.  */
  37. public class FormTest extends AbstractSmackIntegrationTest {

  38.     public FormTest(SmackIntegrationTestEnvironment environment) {
  39.         super(environment);
  40.     }

  41.     /**
  42.      * 1. Create a form to fill out and send it to the other user
  43.      * 2. Retrieve the form to fill out, complete it and return it to the requestor
  44.      * 3. Retrieve the completed form and check that everything is OK
  45.      *
  46.      * @throws InterruptedException if the calling thread was interrupted.
  47.      * @throws NotConnectedException if the XMPP connection is not connected.
  48.      */
  49.     @SuppressWarnings("deprecation")
  50.     @SmackIntegrationTest
  51.     public void testFilloutForm() throws NotConnectedException, InterruptedException {
  52.         DataForm.Builder formToSend = DataForm.builder(DataForm.Type.form);
  53.         formToSend.setInstructions(
  54.             "Fill out this form to report your case.\nThe case will be created automatically.");
  55.         formToSend.setTitle("Case configurations");
  56.         formToSend.setFormType("https://igniterealtime.org/projects/smack/sinttest/form-test/1");
  57.         // Add a hidden variable
  58.         FormField field = FormField.hiddenBuilder("hidden_var")
  59.                         .setValue("Some value for the hidden variable")
  60.                         .build();
  61.         formToSend.addField(field);
  62.         // Add a fixed variable
  63.         field = FormField.fixedBuilder()
  64.                         .setValue("Section 1: Case description")
  65.                         .build();
  66.         formToSend.addField(field);
  67.         // Add a text-single variable
  68.         field = FormField.textSingleBuilder("name")
  69.                         .setLabel("Enter a name for the case")
  70.                         .build();
  71.         formToSend.addField(field);
  72.         // Add a text-multi variable
  73.         field = FormField.textMultiBuilder("description")
  74.                         .setLabel("Enter a description")
  75.                         .build();
  76.         formToSend.addField(field);
  77.         // Add a boolean variable
  78.         field = FormField.booleanBuilder("time")
  79.                         .setLabel("Is this your first case?")
  80.                         .build();
  81.         formToSend.addField(field);
  82.         // Add a text variable where an int value is expected
  83.         field = FormField.textSingleBuilder("age")
  84.                         .setLabel("How old are you?")
  85.                         .build();
  86.         formToSend.addField(field);

  87.         // Create the chats between the two participants
  88.         org.jivesoftware.smack.chat.Chat chat = org.jivesoftware.smack.chat.ChatManager.getInstanceFor(conOne).createChat(conTwo.getUser(), null);
  89.         StanzaCollector collector = conOne.createStanzaCollector(
  90.                 new ThreadFilter(chat.getThreadID()));
  91.         StanzaCollector collector2 = conTwo.createStanzaCollector(
  92.                 new ThreadFilter(chat.getThreadID()));

  93.         Message msg = StanzaBuilder.buildMessage()
  94.                 .setBody("To enter a case please fill out this form and send it back to me")
  95.                 .addExtension(formToSend.build())
  96.                 .build();

  97.         try {
  98.             // Send the message with the form to fill out
  99.             chat.sendMessage(msg);

  100.             // Get the message with the form to fill out
  101.             Message msg2 = collector2.nextResult();
  102.             assertNotNull(msg2, "Message not found");
  103.             // Retrieve the form to fill out
  104.             Form formToRespond = Form.from(msg2);
  105.             assertNotNull(formToRespond);
  106.             assertNotNull(formToRespond.getField("name"));
  107.             assertNotNull(formToRespond.getField("description"));
  108.             // Obtain the form to send with the replies
  109.             final FillableForm completedForm = formToRespond.getFillableForm();
  110.             assertNotNull(completedForm.getField("hidden_var"));
  111.             // Check that a field of type String does not accept booleans
  112.             assertThrows(IllegalArgumentException.class, () -> completedForm.setAnswer("name", true));
  113.             completedForm.setAnswer("name", "Credit card number invalid");
  114.             completedForm.setAnswer(
  115.                 "description",
  116.                 "The ATM says that my credit card number is invalid. What's going on?");
  117.             completedForm.setAnswer("time", true);
  118.             completedForm.setAnswer("age", 20);
  119.             // Create a new message to send with the completed form
  120.             msg2 = StanzaBuilder.buildMessage()
  121.                     .to(conOne.getUser().asBareJid())
  122.                     .setThread(msg.getThread())
  123.                     .ofType(Message.Type.chat)
  124.                     .setBody("To enter a case please fill out this form and send it back to me")
  125.                     // Add the completed form to the message
  126.                     .addExtension(completedForm.getDataFormToSubmit())
  127.                     .build();
  128.             // Send the message with the completed form
  129.             conTwo.sendStanza(msg2);

  130.             // Get the message with the completed form
  131.             Message msg3 = collector.nextResult();
  132.             assertNotNull(msg3, "Message not found");
  133.             // Retrieve the completed form
  134.             final DataForm completedForm2 = DataForm.from(msg3);
  135.             assertNotNull(completedForm2);
  136.             assertNotNull(completedForm2.getField("name"));
  137.             assertNotNull(completedForm2.getField("description"));
  138.             assertEquals(
  139.                  "Credit card number invalid",
  140.                  completedForm2.getField("name").getValues().get(0).toString()
  141.                 );
  142.             assertNotNull(completedForm2.getField("time"));
  143.             assertNotNull(completedForm2.getField("age"));
  144.             assertEquals("20", completedForm2.getField("age").getValues().get(0).toString(), "The age is bad");

  145.         }
  146.         finally {
  147.             collector.cancel();
  148.             collector2.cancel();
  149.         }
  150.     }

  151. }