2 * Copyright (c) 2010-2023 Contributors to the openHAB project
4 * See the NOTICE file(s) distributed with this work for additional
7 * This program and the accompanying materials are made available under the
8 * terms of the Eclipse Public License 2.0 which is available at
9 * http://www.eclipse.org/legal/epl-2.0
11 * SPDX-License-Identifier: EPL-2.0
13 package org.openhab.binding.ihc.internal.ws.services;
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.ArgumentMatchers.*;
17 import static org.mockito.Mockito.*;
19 import java.net.SocketTimeoutException;
21 import org.junit.jupiter.api.BeforeEach;
22 import org.junit.jupiter.api.Test;
23 import org.openhab.binding.ihc.internal.ws.ResourceFileUtils;
24 import org.openhab.binding.ihc.internal.ws.datatypes.WSLoginResult;
25 import org.openhab.binding.ihc.internal.ws.exeptions.IhcExecption;
26 import org.openhab.binding.ihc.internal.ws.http.IhcConnectionPool;
29 * Test for IHC / ELKO binding
31 * @author Pauli Anttila - Initial contribution
33 public class IhcAuthenticationServiceTest {
35 private IhcAuthenticationService ihcAuthenticationService;
36 private final String host = "1.1.1.1";
37 private final String url = "https://1.1.1.1/ws/AuthenticationService";
38 private final int timeout = 100;
41 public void setUp() throws IhcExecption, SocketTimeoutException {
42 ihcAuthenticationService = spy(new IhcAuthenticationService(host, timeout, new IhcConnectionPool()));
44 final String querySuccesfulLogin = ResourceFileUtils.getFileContent("SuccesfulLoginQuery.xml");
45 final String responseSuccesfulLogin = ResourceFileUtils.getFileContent("SuccesfulLoginResponse.xml");
47 doReturn(responseSuccesfulLogin).when(ihcAuthenticationService).sendQuery(eq(url), any(),
48 eq(querySuccesfulLogin), eq(timeout));
50 final String queryLoginFailed = ResourceFileUtils.getFileContent("LoginFailedQuery.xml");
51 final String responseLoginFailed = ResourceFileUtils.getFileContent("LoginFailedResponse.xml");
53 doReturn(responseLoginFailed).when(ihcAuthenticationService).sendQuery(eq(url), any(), eq(queryLoginFailed),
58 public void testSuccesfulLogin() throws IhcExecption {
59 final WSLoginResult result = ihcAuthenticationService.authenticate("user", "pass", "treeview");
60 assertEquals(true, result.isLoginWasSuccessful());
61 assertEquals(false, result.isLoginFailedDueToAccountInvalid());
62 assertEquals(false, result.isLoginFailedDueToConnectionRestrictions());
63 assertEquals(false, result.isLoginFailedDueToInsufficientUserRights());
67 public void testFailedLogin() throws IhcExecption {
68 final WSLoginResult result = ihcAuthenticationService.authenticate("user", "wrong", "treeview");
69 assertEquals(false, result.isLoginWasSuccessful());
70 assertEquals(true, result.isLoginFailedDueToAccountInvalid());
71 assertEquals(false, result.isLoginFailedDueToConnectionRestrictions());
72 assertEquals(false, result.isLoginFailedDueToInsufficientUserRights());