]> git.basschouten.com Git - openhab-addons.git/blob
4e900093cf4110b38b6cae15b9ead17ac6c9be45
[openhab-addons.git] /
1 /**
2  * Copyright (c) 2010-2023 Contributors to the openHAB project
3  *
4  * See the NOTICE file(s) distributed with this work for additional
5  * information.
6  *
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
10  *
11  * SPDX-License-Identifier: EPL-2.0
12  */
13 package org.openhab.binding.ihc.internal.ws.services;
14
15 import static org.junit.jupiter.api.Assertions.*;
16 import static org.mockito.ArgumentMatchers.*;
17 import static org.mockito.Mockito.*;
18
19 import java.net.SocketTimeoutException;
20
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;
27
28 /**
29  * Test for IHC / ELKO binding
30  *
31  * @author Pauli Anttila - Initial contribution
32  */
33 public class IhcAuthenticationServiceTest {
34
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;
39
40     @BeforeEach
41     public void setUp() throws IhcExecption, SocketTimeoutException {
42         ihcAuthenticationService = spy(new IhcAuthenticationService(host, timeout, new IhcConnectionPool()));
43
44         final String querySuccesfulLogin = ResourceFileUtils.getFileContent("SuccesfulLoginQuery.xml");
45         final String responseSuccesfulLogin = ResourceFileUtils.getFileContent("SuccesfulLoginResponse.xml");
46
47         doReturn(responseSuccesfulLogin).when(ihcAuthenticationService).sendQuery(eq(url), any(),
48                 eq(querySuccesfulLogin), eq(timeout));
49
50         final String queryLoginFailed = ResourceFileUtils.getFileContent("LoginFailedQuery.xml");
51         final String responseLoginFailed = ResourceFileUtils.getFileContent("LoginFailedResponse.xml");
52
53         doReturn(responseLoginFailed).when(ihcAuthenticationService).sendQuery(eq(url), any(), eq(queryLoginFailed),
54                 eq(timeout));
55     }
56
57     @Test
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());
64     }
65
66     @Test
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());
73     }
74 }