diff --git a/README.md b/README.md index bbe19e3..12415aa 100644 --- a/README.md +++ b/README.md @@ -1,22 +1,24 @@ -[![Published on Vaadin Directory](https://img.shields.io/badge/Vaadin%20Directory-published-00b4f0.svg)](https://vaadin.com/directory/component/template-addon) -[![Stars on vaadin.com/directory](https://img.shields.io/vaadin-directory/star/app-layout-addon.svg)](https://vaadin.com/directory/component/template-addon) -[![Build Status](https://jenkins.flowingcode.com/job/template-addon/badge/icon)](https://jenkins.flowingcode.com/job/template-addon) +[![Published on Vaadin Directory](https://img.shields.io/badge/Vaadin%20Directory-published-00b4f0.svg)](https://vaadin.com/directory/component/chat-assistant) +[![Stars on vaadin.com/directory](https://img.shields.io/vaadin-directory/star/app-layout-addon.svg)](https://vaadin.com/directory/component/chat-assistant) +[![Build Status](https://jenkins.flowingcode.com/job/ChatAssistant-addon/badge/icon)](https://jenkins.flowingcode.com/job/ChatAssistant-addon) -# Template Add-on +# Chat Assistant Add-on -This is a template project for building new Vaadin 24 add-ons +Vaadin Add-on that displays a chat assistant floating window using [wc-chatbot](https://github.com/yishiashia/wc-chatbot). ## Features -* List the features of your add-on in here +* Messages can be sent by the user or programmatically. +* Listen for new messages written by the user. +* Toggle the chat window on/off. ## Online demo -[Online demo here](http://addonsv24.flowingcode.com/template) +[Online demo here](http://addonsv24.flowingcode.com/chat-assistant) ## Download release -[Available in Vaadin Directory](https://vaadin.com/directory/component/template-addon) +[Available in Vaadin Directory](https://vaadin.com/directory/component/chat-assistant) ### Maven install @@ -25,7 +27,7 @@ Add the following dependencies in your pom.xml file: ```xml org.vaadin.addons.flowingcode - template-addon + chat-assistant-addon X.Y.Z ``` @@ -49,7 +51,7 @@ To see the demo, navigate to http://localhost:8080/ ## Release notes -See [here](https://github.com/FlowingCode/TemplateAddon/releases) +See [here](https://github.com/FlowingCode/ChatAssistant/releases) ## Issue tracking @@ -74,13 +76,30 @@ Then, follow these steps for creating a contibution: This add-on is distributed under Apache License 2.0. For license terms, see LICENSE.txt. -TEMPLATE_ADDON is written by Flowing Code S.A. +Chat Assistant Add-on is written by Flowing Code S.A. # Developer Guide ## Getting started -Add your code samples in this section +Simple example showing the basic options: + + ChatAssistant chatAssistant = new ChatAssistant(); + TextArea message = new TextArea(); + message.setLabel("Enter a message from the assistant"); + message.setSizeFull(); + + Button chat = new Button("Chat"); + chat.addClickListener(ev->{ + Message m = new Message(message.getValue(),false,false,0,false,new Sender("Assistant","1","https://ui-avatars.com/api/?name=Bot")); + chatAssistant.sendMessage(m); + message.clear(); + }); + chatAssistant.sendMessage(new Message("Hello, I am here to assist you",false,false,0,false,new Sender("Assistant","1","https://ui-avatars.com/api/?name=Bot"))); + chatAssistant.toggle(); + chatAssistant.addChatSentListener(ev->{ + Notification.show(ev.getMessage()); + }); ## Special configuration when using Spring diff --git a/pom.xml b/pom.xml index b7e1368..0e5141b 100644 --- a/pom.xml +++ b/pom.xml @@ -5,10 +5,10 @@ 4.0.0 org.vaadin.addons.flowingcode - template-addon + chat-assistant-addon 1.0.0-SNAPSHOT - Template Add-on - Template Add-on for Vaadin Flow + Chat Assistant Add-on + Chat Assistant Add-on for Vaadin Flow 24.1.2 @@ -19,7 +19,7 @@ UTF-8 ${project.basedir}/drivers 11.0.12 - 3.6.0 + 3.7.1 true @@ -123,6 +123,12 @@ com.vaadin vaadin-core + + org.projectlombok + lombok + provided + 1.18.28 + org.slf4j slf4j-simple @@ -154,7 +160,7 @@ io.github.bonigarcia webdrivermanager - 5.1.1 + 5.4.1 test @@ -173,14 +179,6 @@ maven-deploy-plugin 2.8.2 - - org.apache.maven.plugins - maven-surefire-plugin - 2.22.1 - - false - - @@ -242,7 +240,6 @@ src/test/resources/META-INF/resources - src/main/resources/META-INF/resources diff --git a/src/main/java/com/flowingcode/vaadin/addons/chatassistant/ChatAssistant.java b/src/main/java/com/flowingcode/vaadin/addons/chatassistant/ChatAssistant.java new file mode 100644 index 0000000..6ab0457 --- /dev/null +++ b/src/main/java/com/flowingcode/vaadin/addons/chatassistant/ChatAssistant.java @@ -0,0 +1,112 @@ +/*- + * #%L + * Chat Assistant Add-on + * %% + * Copyright (C) 2023 Flowing Code + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ + +package com.flowingcode.vaadin.addons.chatassistant; + +import com.vaadin.flow.component.ComponentEvent; +import com.vaadin.flow.component.ComponentEventListener; +import com.vaadin.flow.component.DomEvent; +import com.vaadin.flow.component.EventData; +import com.vaadin.flow.component.Tag; +import com.vaadin.flow.component.dependency.CssImport; +import com.vaadin.flow.component.dependency.JsModule; +import com.vaadin.flow.component.dependency.NpmPackage; +import com.vaadin.flow.component.html.Div; +import com.vaadin.flow.shared.Registration; + +/** + * Component that allows to create a floating chat button that will open a chat window that can be + * used to provide a chat assistant feature. + * + * @author mmlopez + */ +@SuppressWarnings("serial") +@NpmPackage(value = "wc-chatbot", version = "0.1.1") +@JsModule("wc-chatbot/dist/wc-chatbot.js") +@CssImport("./styles/chat-assistant-styles.css") +@Tag("chat-bot") +public class ChatAssistant extends Div { + + /** + * Sends a message represented by the string message programmatically to the component, with + * default settings. + * + * @param message + */ + public void sendMessage(String message) { + sendMessage(Message.builder().content(message).build()); + } + + /** Shows or hides the chat window */ + public void toggle() { + getElement().executeJs("setTimeout(() => {this.toggle();})"); + } + + /** + * Sends a message to the component, by using the supplied Message object. + * + * @param message + */ + public void sendMessage(Message message) { + getElement() + .executeJs( + "setTimeout(() => {debugger;this.sendMessage(null, JSON.parse($0));})", message.getJsonObject().toJson()); + } + + /** + * Adds a listener that will be notified when the ChatSentEvent is fired, allowing to react when a + * message is sent by the user or programmatically. + * + * @param listener + * @return Registration object to allow removing the listener + */ + public Registration addChatSentListener(ComponentEventListener listener) { + return addListener(ChatSentEvent.class, listener); + } + + /** + * Event that represents a chat being sent to the component. + * + * @author mmlopez + */ + @DomEvent("sent") + public static class ChatSentEvent extends ComponentEvent { + private final String message; + private boolean right; + + public ChatSentEvent( + ChatAssistant source, + boolean fromClient, + @EventData("event.detail.message.message") String message, + @EventData("event.detail.message.right") boolean right) { + super(source, fromClient); + this.message = message.replaceAll("^<[^>]+>|<[^>]+>$", ""); + this.right = right; + } + + public String getMessage() { + return message; + } + + public boolean isRight() { + return right; + } + } +} diff --git a/src/main/java/com/flowingcode/vaadin/addons/chatassistant/Message.java b/src/main/java/com/flowingcode/vaadin/addons/chatassistant/Message.java new file mode 100644 index 0000000..d632a74 --- /dev/null +++ b/src/main/java/com/flowingcode/vaadin/addons/chatassistant/Message.java @@ -0,0 +1,67 @@ +/*- + * #%L + * Chat Assistant Add-on + * %% + * Copyright (C) 2023 Flowing Code + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package com.flowingcode.vaadin.addons.chatassistant; + +import java.util.Optional; +import elemental.json.Json; +import elemental.json.JsonObject; +import lombok.Builder; +import lombok.Getter; +import lombok.Setter; + +/** + * Class that represents a chat message. It contains several configurations to control the + * appearance of the message, such as: + * + *
    + *
  • continued: when true, the message is continued with the previous one. + *
  • right: when true, the message is aligned to the right. + *
  • delay: it can show some delay before being sent. + *
  • loading: it can show a loading image before showing the message. + *
  • sender: it allows to specify the sender. + *
+ * + * @author mmlopez + */ +@Getter +@Setter +@Builder +public class Message { + + private String content; + private boolean continued; + private boolean right; + @Builder.Default + private Integer delay = 0; + private boolean loading; + private Sender sender; + + public JsonObject getJsonObject() { + JsonObject result = Json.createObject(); + result.put("message", content); + result.put("continued", continued); + result.put("right", right); + Optional.ofNullable(delay).ifPresent(value->result.put("delay", delay)); + result.put("loading", loading); + Optional.ofNullable(sender).ifPresent(value->result.put("sender", sender.getJsonObject())); + return result; + } + +} diff --git a/src/main/java/com/flowingcode/vaadin/addons/chatassistant/Sender.java b/src/main/java/com/flowingcode/vaadin/addons/chatassistant/Sender.java new file mode 100644 index 0000000..bd206c2 --- /dev/null +++ b/src/main/java/com/flowingcode/vaadin/addons/chatassistant/Sender.java @@ -0,0 +1,58 @@ +/*- + * #%L + * Chat Assistant Add-on + * %% + * Copyright (C) 2023 Flowing Code + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package com.flowingcode.vaadin.addons.chatassistant; + +import java.util.Optional; +import elemental.json.Json; +import elemental.json.JsonObject; +import elemental.json.JsonValue; +import lombok.Builder; +import lombok.Getter; +import lombok.Setter; + +/** + * Class that represents a chat message sender: + * + *
    + *
  • name: The name of the sender. + *
  • id: a special id to identify the sender. + *
  • avatar: an image that represents the sender. + *
+ * + * @author mmlopez + */ +@Getter +@Setter +@Builder +public class Sender { + + private String name; + private String id; + private String avatar; + + public JsonValue getJsonObject() { + JsonObject result = Json.createObject(); + Optional.ofNullable(name).ifPresent(value->result.put("name", name)); + Optional.ofNullable(id).ifPresent(value->result.put("id", id)); + Optional.ofNullable(avatar).ifPresent(value->result.put("avatar", avatar)); + return result; + } + +} diff --git a/src/main/resources/META-INF/frontend/styles/chat-assistant-styles.css b/src/main/resources/META-INF/frontend/styles/chat-assistant-styles.css new file mode 100644 index 0000000..5d85bc4 --- /dev/null +++ b/src/main/resources/META-INF/frontend/styles/chat-assistant-styles.css @@ -0,0 +1,36 @@ +/*- + * #%L + * Chat Assistant Add-on + * %% + * Copyright (C) 2023 Flowing Code + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ + chat-bot { + --chatbot-avatar-bg-color: var(--lumo-primary-color-10pct); + --chatbot-avatar-margin: 10%; + --chatbot-header-bg-color: var(--lumo-primary-color-50pct); + --chatbot-header-title-color: var(--lumo-header-text-color); + --chatbot-body-bg-color: var(--lumo-primary-color-10pct); + --chatbot-send-button-color: var(--lumo-primary-color); + } + + chat-bot::part(chat-bubble) { + --chat-bubble-color: var(--lumo-success-color); + --chat-bubble-right-color: white; + --chat-bubble-font-color: white; + --chat-bubble-font-right-color: var(--lumo-contrast-90pct); + --chat-bubble-avatar-color: var(--lumo-secondary-color); + --chat-bubble-delay: 0.2s; + } diff --git a/src/main/resources/META-INF/frontend/styles/static_addon_styles b/src/main/resources/META-INF/frontend/styles/static_addon_styles deleted file mode 100644 index c2a6ed1..0000000 --- a/src/main/resources/META-INF/frontend/styles/static_addon_styles +++ /dev/null @@ -1 +0,0 @@ -Place add-on shareable styles in this folder \ No newline at end of file diff --git a/src/main/resources/META-INF/resources/static_addon_resources b/src/main/resources/META-INF/resources/static_addon_resources deleted file mode 100644 index 70832cc..0000000 --- a/src/main/resources/META-INF/resources/static_addon_resources +++ /dev/null @@ -1 +0,0 @@ -Place static add-on resources in this folder \ No newline at end of file diff --git a/src/test/java/com/flowingcode/vaadin/addons/DemoLayout.java b/src/test/java/com/flowingcode/vaadin/addons/DemoLayout.java index 70abf98..2ee7785 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/DemoLayout.java +++ b/src/test/java/com/flowingcode/vaadin/addons/DemoLayout.java @@ -1,15 +1,15 @@ /*- * #%L - * Template Add-on + * Chat Assistant Add-on * %% * Copyright (C) 2023 Flowing Code * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. diff --git a/src/test/java/com/flowingcode/vaadin/addons/chatassistant/ChatAssistantDemo.java b/src/test/java/com/flowingcode/vaadin/addons/chatassistant/ChatAssistantDemo.java new file mode 100644 index 0000000..9107955 --- /dev/null +++ b/src/test/java/com/flowingcode/vaadin/addons/chatassistant/ChatAssistantDemo.java @@ -0,0 +1,79 @@ +/*- + * #%L + * Chat Assistant Add-on + * %% + * Copyright (C) 2023 Flowing Code + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package com.flowingcode.vaadin.addons.chatassistant; + +import com.flowingcode.vaadin.addons.chatassistant.ChatAssistant; +import com.flowingcode.vaadin.addons.chatassistant.Message; +import com.flowingcode.vaadin.addons.chatassistant.Sender; +import com.flowingcode.vaadin.addons.demo.DemoSource; +import com.vaadin.flow.component.button.Button; +import com.vaadin.flow.component.dependency.CssImport; +import com.vaadin.flow.component.notification.Notification; +import com.vaadin.flow.component.orderedlayout.VerticalLayout; +import com.vaadin.flow.component.textfield.TextArea; +import com.vaadin.flow.router.PageTitle; +import com.vaadin.flow.router.Route; + +@DemoSource +@PageTitle("Chat Assistant Add-on Demo") +@SuppressWarnings("serial") +@Route(value = "chat-assistant/basic-demo", layout = ChatAssistantDemoView.class) +@CssImport("./styles/chat-assistant-styles-demo.css") +public class ChatAssistantDemo extends VerticalLayout { + + public ChatAssistantDemo() { + ChatAssistant chatAssistant = new ChatAssistant(); + TextArea message = new TextArea(); + message.setLabel("Enter a message from the assistant"); + message.setSizeFull(); + + Button chat = new Button("Chat"); + chat.addClickListener( + ev -> { + Message m = + new Message( + message.getValue(), + false, + false, + 0, + false, + new Sender("Assistant", "1", "chatbot.png")); + chatAssistant.sendMessage(m); + message.clear(); + }); + chatAssistant.sendMessage( + new Message( + "Hello, I am here to assist you", + false, + false, + 0, + false, + new Sender("Assistant", "1", "chatbot.png"))); + chatAssistant.toggle(); + chatAssistant.addChatSentListener( + ev -> { + if (ev.isRight()) { + Notification.show(ev.getMessage()); + } + }); + + add(message, chat, chatAssistant); + } +} diff --git a/src/test/java/com/flowingcode/vaadin/addons/template/TemplateDemoView.java b/src/test/java/com/flowingcode/vaadin/addons/chatassistant/ChatAssistantDemoView.java similarity index 76% rename from src/test/java/com/flowingcode/vaadin/addons/template/TemplateDemoView.java rename to src/test/java/com/flowingcode/vaadin/addons/chatassistant/ChatAssistantDemoView.java index 1c3aecd..207104a 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/template/TemplateDemoView.java +++ b/src/test/java/com/flowingcode/vaadin/addons/chatassistant/ChatAssistantDemoView.java @@ -1,6 +1,6 @@ /*- * #%L - * Template Add-on + * Chat Assistant Add-on * %% * Copyright (C) 2023 Flowing Code * %% @@ -17,7 +17,7 @@ * limitations under the License. * #L% */ -package com.flowingcode.vaadin.addons.template; +package com.flowingcode.vaadin.addons.chatassistant; import com.flowingcode.vaadin.addons.DemoLayout; import com.flowingcode.vaadin.addons.GithubLink; @@ -27,12 +27,12 @@ @SuppressWarnings("serial") @ParentLayout(DemoLayout.class) -@Route("template") -@GithubLink("https://github.com/FlowingCode/AddonStarter24") -public class TemplateDemoView extends TabbedDemo { +@Route("chat-assistant") +@GithubLink("https://github.com/FlowingCode/ChatAssistant") +public class ChatAssistantDemoView extends TabbedDemo { - public TemplateDemoView() { - addDemo(TemplateDemo.class); + public ChatAssistantDemoView() { + addDemo(ChatAssistantDemo.class); setSizeFull(); } } diff --git a/src/test/java/com/flowingcode/vaadin/addons/template/DemoView.java b/src/test/java/com/flowingcode/vaadin/addons/chatassistant/DemoView.java similarity index 88% rename from src/test/java/com/flowingcode/vaadin/addons/template/DemoView.java rename to src/test/java/com/flowingcode/vaadin/addons/chatassistant/DemoView.java index 951bdb7..17157c3 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/template/DemoView.java +++ b/src/test/java/com/flowingcode/vaadin/addons/chatassistant/DemoView.java @@ -1,15 +1,15 @@ /*- * #%L - * Template Add-on + * Chat Assistant Add-on * %% * Copyright (C) 2023 Flowing Code * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -18,7 +18,7 @@ * #L% */ -package com.flowingcode.vaadin.addons.template; +package com.flowingcode.vaadin.addons.chatassistant; import com.vaadin.flow.component.orderedlayout.VerticalLayout; import com.vaadin.flow.router.BeforeEnterEvent; @@ -31,6 +31,6 @@ public class DemoView extends VerticalLayout implements BeforeEnterObserver { @Override public void beforeEnter(BeforeEnterEvent event) { - event.forwardTo(TemplateDemoView.class); + event.forwardTo(ChatAssistantDemoView.class); } } diff --git a/src/test/java/com/flowingcode/vaadin/addons/template/it/AbstractViewTest.java b/src/test/java/com/flowingcode/vaadin/addons/chatassistant/it/AbstractViewTest.java similarity index 93% rename from src/test/java/com/flowingcode/vaadin/addons/template/it/AbstractViewTest.java rename to src/test/java/com/flowingcode/vaadin/addons/chatassistant/it/AbstractViewTest.java index 87ff695..1cbb554 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/template/it/AbstractViewTest.java +++ b/src/test/java/com/flowingcode/vaadin/addons/chatassistant/it/AbstractViewTest.java @@ -1,15 +1,15 @@ /*- * #%L - * Template Add-on + * Chat Assistant Add-on * %% * Copyright (C) 2023 Flowing Code * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -18,7 +18,7 @@ * #L% */ -package com.flowingcode.vaadin.addons.template.it; +package com.flowingcode.vaadin.addons.chatassistant.it; import com.vaadin.testbench.ScreenshotOnFailureRule; import com.vaadin.testbench.TestBench; @@ -47,8 +47,7 @@ public abstract class AbstractViewTest extends ParallelTest { private final String route; - @Rule - public ScreenshotOnFailureRule rule = new ScreenshotOnFailureRule(this, true); + @Rule public ScreenshotOnFailureRule rule = new ScreenshotOnFailureRule(this, true); public AbstractViewTest() { this(""); @@ -87,8 +86,7 @@ private static String getURL(String route) { private static final String USE_HUB_PROPERTY = "test.use.hub"; /** - * Returns whether we are using a test hub. This means that the starter is - * running tests in + * Returns whether we are using a test hub. This means that the starter is running tests in * Vaadin's CI environment, and uses TestBench to connect to the testing hub. * * @return whether we are using a test hub diff --git a/src/test/java/com/flowingcode/vaadin/addons/chatassistant/it/BasicIT.java b/src/test/java/com/flowingcode/vaadin/addons/chatassistant/it/BasicIT.java new file mode 100644 index 0000000..70a3ea4 --- /dev/null +++ b/src/test/java/com/flowingcode/vaadin/addons/chatassistant/it/BasicIT.java @@ -0,0 +1,57 @@ +/*- + * #%L + * Chat Assistant Add-on + * %% + * Copyright (C) 2023 Flowing Code + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package com.flowingcode.vaadin.addons.chatassistant.it; + +import com.flowingcode.vaadin.addons.chatassistant.it.po.ChatAssistantElement; +import com.flowingcode.vaadin.addons.chatassistant.it.po.ChatBubbleElement; +import com.vaadin.flow.component.button.testbench.ButtonElement; +import com.vaadin.flow.component.html.testbench.NativeButtonElement; +import com.vaadin.flow.component.html.testbench.ParagraphElement; +import com.vaadin.flow.component.notification.testbench.NotificationElement; +import com.vaadin.flow.component.textfield.testbench.TextAreaElement; +import com.vaadin.testbench.TestBenchElement; +import org.junit.Assert; +import org.junit.Test; + +public class BasicIT extends AbstractViewTest { + + @Test + public void sendMessageFromUser() { + ChatAssistantElement element = $(ChatAssistantElement.class).first(); + TestBenchElement input = element.$(TestBenchElement.class).id("msg-input"); + input.sendKeys("hello"); + TestBenchElement button = element.$(NativeButtonElement.class).get(1); + button.click(); + String notificationMessage = $(NotificationElement.class).waitForFirst().getText(); + Assert.assertEquals("hello", notificationMessage); + } + + @Test + public void sendMessageFromAssistant() { + ChatAssistantElement element = $(ChatAssistantElement.class).first(); + TextAreaElement ta = $(TextAreaElement.class).first(); + ta.setValue("What can I do for you?"); + ButtonElement chatButton = $(ButtonElement.class).first(); + chatButton.click(); + ChatBubbleElement cb = element.$(ChatBubbleElement.class).get(1); + String chat = cb.getText(); + Assert.assertEquals("What can I do for you?", chat); + } +} diff --git a/src/test/java/com/flowingcode/vaadin/addons/template/it/ViewIT.java b/src/test/java/com/flowingcode/vaadin/addons/chatassistant/it/ViewIT.java similarity index 54% rename from src/test/java/com/flowingcode/vaadin/addons/template/it/ViewIT.java rename to src/test/java/com/flowingcode/vaadin/addons/chatassistant/it/ViewIT.java index 7e4a499..e191e13 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/template/it/ViewIT.java +++ b/src/test/java/com/flowingcode/vaadin/addons/chatassistant/it/ViewIT.java @@ -1,15 +1,15 @@ /*- * #%L - * Template Add-on + * Chat Assistant Add-on * %% * Copyright (C) 2023 Flowing Code * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -18,7 +18,7 @@ * #L% */ -package com.flowingcode.vaadin.addons.template.it; +package com.flowingcode.vaadin.addons.chatassistant.it; import static org.hamcrest.Matchers.is; import static org.hamcrest.Matchers.not; @@ -32,32 +32,33 @@ public class ViewIT extends AbstractViewTest { - private Matcher hasBeenUpgradedToCustomElement = new TypeSafeDiagnosingMatcher() { - - @Override - public void describeTo(Description description) { - description.appendText("a custom element"); - } - - @Override - protected boolean matchesSafely(TestBenchElement item, Description mismatchDescription) { - String script = "let s=arguments[0].shadowRoot; return !!(s&&s.childElementCount)"; - if (!item.getTagName().contains("-")) { - return true; - } - if ((Boolean) item.getCommandExecutor().executeScript(script, item)) { - return true; - } else { - mismatchDescription.appendText(item.getTagName() + " "); - mismatchDescription.appendDescriptionOf(is(not(this))); - return false; - } - } - }; + private Matcher hasBeenUpgradedToCustomElement = + new TypeSafeDiagnosingMatcher() { + + @Override + public void describeTo(Description description) { + description.appendText("a custom element"); + } + + @Override + protected boolean matchesSafely(TestBenchElement item, Description mismatchDescription) { + String script = "let s=arguments[0].shadowRoot; return !!(s&&s.childElementCount)"; + if (!item.getTagName().contains("-")) { + return true; + } + if ((Boolean) item.getCommandExecutor().executeScript(script, item)) { + return true; + } else { + mismatchDescription.appendText(item.getTagName() + " "); + mismatchDescription.appendDescriptionOf(is(not(this))); + return false; + } + } + }; @Test public void componentWorks() { - TestBenchElement element = $("paper-input").first(); + TestBenchElement element = $("chat-bot").first(); assertThat(element, hasBeenUpgradedToCustomElement); } } diff --git a/src/main/java/com/flowingcode/vaadin/addons/template/TemplateAddon.java b/src/test/java/com/flowingcode/vaadin/addons/chatassistant/it/po/ChatAssistantElement.java similarity index 57% rename from src/main/java/com/flowingcode/vaadin/addons/template/TemplateAddon.java rename to src/test/java/com/flowingcode/vaadin/addons/chatassistant/it/po/ChatAssistantElement.java index 5870ed5..fd9e07b 100644 --- a/src/main/java/com/flowingcode/vaadin/addons/template/TemplateAddon.java +++ b/src/test/java/com/flowingcode/vaadin/addons/chatassistant/it/po/ChatAssistantElement.java @@ -1,6 +1,6 @@ /*- * #%L - * Template Add-on + * Chat Assistant Add-on * %% * Copyright (C) 2023 Flowing Code * %% @@ -17,16 +17,10 @@ * limitations under the License. * #L% */ +package com.flowingcode.vaadin.addons.chatassistant.it.po; -package com.flowingcode.vaadin.addons.template; +import com.vaadin.testbench.TestBenchElement; +import com.vaadin.testbench.elementsbase.Element; -import com.vaadin.flow.component.Tag; -import com.vaadin.flow.component.dependency.JsModule; -import com.vaadin.flow.component.dependency.NpmPackage; -import com.vaadin.flow.component.html.Div; - -@SuppressWarnings("serial") -@NpmPackage(value = "@polymer/paper-input", version = "3.2.1") -@JsModule("@polymer/paper-input/paper-input.js") -@Tag("paper-input") -public class TemplateAddon extends Div {} +@Element("chat-bot") +public class ChatAssistantElement extends TestBenchElement {} diff --git a/src/test/java/com/flowingcode/vaadin/addons/chatassistant/it/po/ChatBubbleElement.java b/src/test/java/com/flowingcode/vaadin/addons/chatassistant/it/po/ChatBubbleElement.java new file mode 100644 index 0000000..134fab4 --- /dev/null +++ b/src/test/java/com/flowingcode/vaadin/addons/chatassistant/it/po/ChatBubbleElement.java @@ -0,0 +1,26 @@ +/*- + * #%L + * Chat Assistant Add-on + * %% + * Copyright (C) 2023 Flowing Code + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ +package com.flowingcode.vaadin.addons.chatassistant.it.po; + +import com.vaadin.testbench.TestBenchElement; +import com.vaadin.testbench.elementsbase.Element; + +@Element("chat-bubble") +public class ChatBubbleElement extends TestBenchElement {} diff --git a/src/test/java/com/flowingcode/vaadin/addons/template/test/SerializationTest.java b/src/test/java/com/flowingcode/vaadin/addons/chatassistant/test/SerializationTest.java similarity index 81% rename from src/test/java/com/flowingcode/vaadin/addons/template/test/SerializationTest.java rename to src/test/java/com/flowingcode/vaadin/addons/chatassistant/test/SerializationTest.java index 6c226a2..48f73d6 100644 --- a/src/test/java/com/flowingcode/vaadin/addons/template/test/SerializationTest.java +++ b/src/test/java/com/flowingcode/vaadin/addons/chatassistant/test/SerializationTest.java @@ -1,15 +1,15 @@ /*- * #%L - * Template Add-on + * Chat Assistant Add-on * %% * Copyright (C) 2023 Flowing Code * %% * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at - * + * * http://www.apache.org/licenses/LICENSE-2.0 - * + * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. @@ -17,14 +17,14 @@ * limitations under the License. * #L% */ -package com.flowingcode.vaadin.addons.template.test; +package com.flowingcode.vaadin.addons.chatassistant.test; +import com.flowingcode.vaadin.addons.chatassistant.ChatAssistant; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.ObjectInputStream; import java.io.ObjectOutputStream; -import com.flowingcode.vaadin.addons.template.TemplateAddon; import org.junit.Assert; import org.junit.Test; @@ -35,7 +35,8 @@ private void testSerializationOf(Object obj) throws IOException, ClassNotFoundEx try (ObjectOutputStream oos = new ObjectOutputStream(baos)) { oos.writeObject(obj); } - try (ObjectInputStream in = new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))) { + try (ObjectInputStream in = + new ObjectInputStream(new ByteArrayInputStream(baos.toByteArray()))) { obj.getClass().cast(in.readObject()); } } @@ -43,7 +44,7 @@ private void testSerializationOf(Object obj) throws IOException, ClassNotFoundEx @Test public void testSerialization() throws ClassNotFoundException, IOException { try { - testSerializationOf(new TemplateAddon()); + testSerializationOf(new ChatAssistant()); } catch (Exception e) { Assert.fail("Problem while testing serialization: " + e.getMessage()); } diff --git a/src/test/java/com/flowingcode/vaadin/addons/template/TemplateDemo.java b/src/test/java/com/flowingcode/vaadin/addons/template/TemplateDemo.java deleted file mode 100644 index 5f6e6ee..0000000 --- a/src/test/java/com/flowingcode/vaadin/addons/template/TemplateDemo.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.flowingcode.vaadin.addons.template; - -import com.flowingcode.vaadin.addons.demo.DemoSource; -import com.vaadin.flow.component.html.Div; -import com.vaadin.flow.router.PageTitle; -import com.vaadin.flow.router.Route; - -@DemoSource -@PageTitle("Template Add-on Demo") -@SuppressWarnings("serial") -@Route(value = "demo", layout = TemplateDemoView.class) -public class TemplateDemo extends Div { - - public TemplateDemo() { - add(new TemplateAddon()); - } -} diff --git a/src/test/resources/META-INF/frontend/styles/chat-assistant-styles-demo.css b/src/test/resources/META-INF/frontend/styles/chat-assistant-styles-demo.css new file mode 100644 index 0000000..d69ef15 --- /dev/null +++ b/src/test/resources/META-INF/frontend/styles/chat-assistant-styles-demo.css @@ -0,0 +1,23 @@ +/*- + * #%L + * Chat Assistant Add-on + * %% + * Copyright (C) 2023 Flowing Code + * %% + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * #L% + */ + chat-bot { + --chatbot-avatar-img: url(chatbot.png); + --chatbot-avatar-bg-color: white; + } diff --git a/src/test/resources/META-INF/frontend/styles/shared-styles.css b/src/test/resources/META-INF/frontend/styles/shared-styles.css deleted file mode 100644 index 6680e2d..0000000 --- a/src/test/resources/META-INF/frontend/styles/shared-styles.css +++ /dev/null @@ -1 +0,0 @@ -/*Demo styles*/ \ No newline at end of file diff --git a/src/test/resources/META-INF/resources/chatbot.png b/src/test/resources/META-INF/resources/chatbot.png new file mode 100644 index 0000000..c4bf3df Binary files /dev/null and b/src/test/resources/META-INF/resources/chatbot.png differ diff --git a/src/test/resources/META-INF/resources/static_addon_test_resources b/src/test/resources/META-INF/resources/static_addon_test_resources deleted file mode 100644 index b68f527..0000000 --- a/src/test/resources/META-INF/resources/static_addon_test_resources +++ /dev/null @@ -1 +0,0 @@ -Place static addon test resources in this folder \ No newline at end of file