Repository: EricssonResearch/arms

Issue:
Refactor of SocketClient.get_config_and_connect 
By now the method SocketClient.get_config_and_connect only returns one value, which equals True if the settings to the client could be found and False if not.

It seems necessary that this function returns two values [*1, *2], because usually this function is only called to connect with the respective server and overall a better feedback is needed:
- *1: True, if a setting entry belonging to the client (client_name) could be found and no data type violation occurred. False, if not.
- *2: True, if a connection could be made. False, if not.

Comments:

Patch:
diff --git a/arms/misc/socket.py b/arms/misc/socket.py
index 0ca3fd3..a04eb19 100644
--- a/arms/misc/socket.py
+++ b/arms/misc/socket.py
@@ -25,9 +25,10 @@ class SocketClient:
         server.
 
         Return:
-            - True, if a setting entry belonging to the client (client_name)
-            could be found and no data type violation occurred.
-            - False, if not.
+			- [#1, #2]
+				#1: True, if a setting entry belonging to the client (client_name)
+					could be found and no data type violation occurred. False, if not.
+				#2: True, if a connection could be made. False, if not.
         """
         try:
             c = config.var.data['socket'][self.name]
@@ -38,11 +39,11 @@ class SocketClient:
                              'socket {} in the configuration file ('
                              'arms/config/config.json). Hence, no '
                              'connection could be made.'.format(self.name))
-            return False
+            return [False, False]
 
         if isinstance(host, str) and isinstance(port, int):
             ok = self.connect(host, port)
-            return ok
+            return [True, ok]
         else:
             log.socket.error('Data type violation. The host number has to be '
                              'provided as a string and the port number as an '
@@ -50,7 +51,7 @@ class SocketClient:
                              'arms/config/config.json). In consequence, the '
                              'socket {} could not connect to the server.'
                              .format(self.name))
-            return False
+            return [False, False]
 
     def connect(self, host, port):
         """Establishes a connection to a server.
diff --git a/arms/units/abb.py b/arms/units/abb.py
index 5d39fa1..76c3cb0 100644
--- a/arms/units/abb.py
+++ b/arms/units/abb.py
@@ -23,6 +23,11 @@ class ABB:
     def __exit__(self, exc_type, exc_val, exc_tb):
         self.close()
 
+    def connect(self):
+        """Connects to the socket server."""
+        ok, connected = self.client.get_config_and_connect()
+        return ok, connected
+
     def close(self):
         """Close connection."""
         case = 0


Test patch:
diff --git a/tests/unit/test_socket.py b/tests/unit/test_socket.py
index a1a9b3c..a56d6e7 100644
--- a/tests/unit/test_socket.py
+++ b/tests/unit/test_socket.py
@@ -39,11 +39,12 @@ def test_get_config_and_connect():
     """
     client = SocketClient("test_socket")
     data = {"socket": {"test_socket": {"host": host, "port": port}}}
-    client.connect = mock.MagicMock()
+    mock_connect = mock.MagicMock()
+    client.connect = mock_connect
 
     with mock.patch.object(config.var, 'data', data):
         client.get_config_and_connect()
-        client.connect.assert_called_with(host, port)
+        mock_connect.assert_called_with(host, port)
 
 
 config_none = None
@@ -65,16 +66,17 @@ def test_get_config_and_connect_fail(config_data):
     """WHEN the function 'get_config_and_connect' is called, IF the config
     entry containing the host name and port number to the specified client
     (client_name) is of the wrong format or does not exist, THEN no attempt
-    to connect with the respective server shall be made and the boolean value
-    False shall be returned.
+    to connect with the respective server shall be made and the boolean values
+    [False, False] shall be returned.
     """
     client = SocketClient("test_socket")
-    client.connect = mock.MagicMock()
+    mock_connect = mock.MagicMock()
+    client.connect = mock_connect
 
     with mock.patch.object(config.var, 'data', config_data):
         ok = client.get_config_and_connect()
-        client.connect.assert_not_called()
-        assert ok is False
+        mock_connect.assert_not_called()
+        assert ok == [False, False]
 
 
 def test_connect():
@@ -185,7 +187,7 @@ class SocketServer:
         """Closes the socket and the connection to the client."""
         if self.conn is not None:
             self.conn.close()
-            
+
         if self.sock is not None:
             self.sock.close()