Refactor Android Aitt class and test cases
authorChanhee Lee <ch2102.lee@samsung.com>
Fri, 16 Dec 2022 07:29:09 +0000 (16:29 +0900)
committerChanhee Lee <ch2102.lee@samsung.com>
Thu, 9 Mar 2023 02:14:59 +0000 (11:14 +0900)
[Problem] There are some code/comments which need to be simplified.
[Solution] Do refactoring by drawing common code as functions,
           correcting comments and so on.

android/aitt/src/main/java/com/samsung/android/aitt/Aitt.java
android/aitt/src/test/java/com/samsung/android/aitt/AittUnitTest.java
android/modules/tcp/src/androidTest/java/com/samsung/android/modules/tcp/TCPInstrumentedTest.java

index 6df0c09..7ed8f04 100644 (file)
@@ -51,6 +51,7 @@ public class Aitt {
 
     private static final String TAG = "AITT_ANDROID";
     private static final String INVALID_TOPIC = "Invalid topic";
+    private static final String HOST_STRING = "host";
 
     private final Map<String, HostTable> publishTable = new HashMap<>();
     private final Map<String, Pair<Protocol, Object>> subscribeMap = new HashMap<>();
@@ -95,11 +96,10 @@ public class Aitt {
         }
 
         public static Protocol fromInt(long value) {
-            for (Protocol type : values()) {
-                if (type.getValue() == value) {
+            for (Protocol type : values())
+                if (type.getValue() == value)
                     return type;
-                }
-            }
+
             return null;
         }
     }
@@ -155,20 +155,18 @@ public class Aitt {
      * @param clearSession "clear" the current session when the client disconnects
      */
     public Aitt(Context appContext, String id, String ip, boolean clearSession) throws InstantiationException {
-        if (appContext == null) {
+        if (appContext == null)
             throw new IllegalArgumentException("Invalid appContext");
-        }
-        if (id == null || id.isEmpty()) {
+        if (id == null || id.isEmpty())
             throw new IllegalArgumentException("Invalid id");
-        }
-        if (ip == null || ip.isEmpty()) {
+        if (ip == null || ip.isEmpty())
             throw new IllegalArgumentException("Invalid ip");
-        }
+
         mJniInterface = new JniInterface();
         instance = mJniInterface.init(id, ip, clearSession);
-        if (instance == 0L) {
+        if (instance == 0L)
             throw new InstantiationException("Failed to instantiate native instance");
-        }
+
         this.ip = ip;
         this.appContext = appContext;
     }
@@ -179,9 +177,9 @@ public class Aitt {
      * @param callback ConnectionCallback to which status should be updated
      */
     public void setConnectionCallback(ConnectionCallback callback) {
-        if (callback == null) {
+        if (callback == null)
             throw new IllegalArgumentException("Invalid callback");
-        }
+
         connectionCallback = callback;
         mJniInterface.setConnectionCallback(this::connectionStatusCallback);
     }
@@ -202,9 +200,9 @@ public class Aitt {
      * @param port     Broker port number to which, device has to connect
      */
     public void connect(@Nullable String brokerIp, int port) {
-        if (brokerIp == null || brokerIp.isEmpty()) {
+        if (brokerIp == null || brokerIp.isEmpty())
             brokerIp = Definitions.AITT_LOCALHOST;
-        }
+
         mJniInterface.connect(brokerIp, port);
         //Subscribe to java discovery topic
         mJniInterface.subscribe(Definitions.JAVA_SPECIFIC_DISCOVERY_TOPIC, this::messageCallback, Protocol.MQTT.getValue(), QoS.EXACTLY_ONCE.ordinal());
@@ -236,7 +234,7 @@ public class Aitt {
     }
 
     /**
-     * Method to publish message to a specific topic
+     * Method to publish message to a specific topic with a given protocol
      *
      * @param topic    String to which message needs to be published
      * @param message  Byte message that needs to be published
@@ -248,7 +246,7 @@ public class Aitt {
     }
 
     /**
-     * Method to publish message to a specific topic
+     * Method to publish message to a specific topic with a given protocol and qos
      *
      * @param topic    String to which message needs to be published
      * @param message  Byte message that needs to be published
@@ -261,7 +259,7 @@ public class Aitt {
     }
 
     /**
-     * Method to publish message to a specific topic
+     * Method to publish message to a specific topic with a given protocol, qos, and retain
      *
      * @param topic    String to which message needs to be published
      * @param message  Byte message that needs to be published
@@ -275,7 +273,7 @@ public class Aitt {
     }
 
     /**
-     * Method to publish message to a specific topic
+     * Method to publish message to a specific topic with protocols, qos, and retain
      *
      * @param topic     String to which message needs to be published
      * @param message   Byte message that needs to be published
@@ -284,33 +282,34 @@ public class Aitt {
      * @param retain    Boolean to decide whether or not the message should be retained by the broker
      */
     public void publish(String topic, byte[] message, EnumSet<Protocol> protocols, QoS qos, boolean retain) {
-
         checkParams(topic, protocols);
-        int jniProtocols = 0;
 
+        int jniProtocols = 0;
         for (Protocol p : protocols) {
-            if (!classifyProtocol(p)) {
+            if (isUsingNativePubSub(p)) {
                 jniProtocols += p.getValue();
                 protocols.remove(p);
             }
         }
 
-        if (jniProtocols > 0) {
+        if (jniProtocols > 0)
             mJniInterface.publish(topic, message, message.length, jniProtocols, qos.ordinal(), retain);
-        }
 
+        publishTransportProtocols(topic, message, protocols);
+    }
+
+    /**
+     * Method to publish message to a specific topic with transport protocols
+     *
+     * @param topic     String to which message needs to be published
+     * @param message   Byte message that needs to be published
+     * @param protocols Protocols to be used to publish message
+     */
+    private void publishTransportProtocols(String topic, byte[] message, EnumSet<Protocol> protocols) {
         for (Protocol protocol : protocols) {
             try {
                 synchronized (this) {
-                    if (!publishTable.containsKey(topic)) {
-                        Log.e(TAG, "Invalid publish request over unsubscribed topic");
-                        return;
-                    }
-                    HostTable hostTable = publishTable.get(topic);
-                    if (hostTable == null) {
-                        Log.d(TAG, "Host table for topic [" + topic + "] is null.");
-                        continue;
-                    }
+                    HostTable hostTable = getHostTable(topic);
                     for (String hostIp : hostTable.hostMap.keySet()) {
                         PortTable portTable = hostTable.hostMap.get(hostIp);
                         if (portTable == null) {
@@ -329,11 +328,27 @@ public class Aitt {
                     }
                 }
             } catch (Exception e) {
-                Log.e(TAG, "Error during publish", e);
+                Log.e(TAG, "Error during publishing transport protocols", e);
             }
         }
     }
 
+    /**
+     * Method to get a host table related to a specific topic
+     *
+     * @param topic String to which message needs to be published
+     */
+    private HostTable getHostTable(String topic) {
+        if (!publishTable.containsKey(topic))
+            throw new IllegalArgumentException("Invalid publish request over an unsubscribed topic");
+
+        HostTable hostTable = publishTable.get(topic);
+        if (hostTable == null)
+            throw new IllegalArgumentException("Host table for topic [" + topic + "] is null.");
+
+        return hostTable;
+    }
+
     // TODO: Update publish with proper stream interface.
     public boolean publish(AittStream stream, String topic, byte[] message, Protocol protocol) {
         if (stream == null) {
@@ -343,15 +358,7 @@ public class Aitt {
 
         try {
             synchronized (this) {
-                if (!publishTable.containsKey(topic)) {
-                    Log.e(TAG, "Invalid publish request over unsubscribed topic");
-                    return false;
-                }
-                HostTable hostTable = publishTable.get(topic);
-                if (hostTable == null) {
-                    Log.d(TAG, "Host table for topic [" + topic + "] is null.");
-                    return false;
-                }
+                HostTable hostTable = getHostTable(topic);
                 for (String hostIp : hostTable.hostMap.keySet()) {
                     PortTable portTable = hostTable.hostMap.get(hostIp);
                     if (portTable == null) {
@@ -411,10 +418,10 @@ public class Aitt {
      * Method to differentiate android specific protocol
      *
      * @param protocols Protocol to be classified
-     * @return true if the protocol is specific to android implementation
+     * @return true if the protocol is using native publish/subscribe
      */
-    private boolean classifyProtocol(Protocol protocols) {
-        return protocols.equals(Protocol.WEBRTC) || protocols.equals(Protocol.IPC) || protocols.equals(Protocol.RTSP);
+    private boolean isUsingNativePubSub(Protocol protocols) {
+        return protocols.equals(Protocol.MQTT) || protocols.equals(Protocol.TCP) || protocols.equals(Protocol.TCP_SECURE);
     }
 
     /**
@@ -458,21 +465,19 @@ public class Aitt {
      *
      * @param topic     String to which applications can subscribe, to receive data
      * @param callback  Callback object specific to a subscribe call
-     * @param protocols Protocol supported by application, invoking subscribe
+     * @param protocols Protocols supported by application, invoking subscribe
      * @param qos       QoS at which the message should be delivered
      */
     public void subscribe(String topic, SubscribeCallback callback, EnumSet<Protocol> protocols, QoS qos) {
-
         checkParams(topic, protocols);
 
-        if (callback == null) {
+        if (callback == null)
             throw new IllegalArgumentException("Invalid callback");
-        }
 
         int jniProtocols = 0;
 
         for (Protocol p : protocols) {
-            if (!classifyProtocol(p)) {
+            if (isUsingNativePubSub(p)) {
                 jniProtocols += p.getValue();
                 protocols.remove(p);
             }
@@ -515,17 +520,15 @@ public class Aitt {
     /**
      * Method to verify Aitt pub & sub parameters
      *
-     * @param topic     String to which applications can subscribe, to receive data
-     * @param protocols Protocol supported by application, invoking subscribe
+     * @param topic     String to which applications can publish or subscribe data
+     * @param protocols Protocols which indicate the way to publish or subscribe data
      */
     private void checkParams(String topic, EnumSet<Protocol> protocols) {
-        if (topic == null || topic.isEmpty()) {
+        if (topic == null || topic.isEmpty())
             throw new IllegalArgumentException(INVALID_TOPIC);
-        }
 
-        if (protocols.isEmpty()) {
+        if (protocols.isEmpty())
             throw new IllegalArgumentException("Invalid protocols");
-        }
     }
 
     /**
@@ -539,11 +542,8 @@ public class Aitt {
             try {
                 ArrayList<SubscribeCallback> cbList = subscribeCallbacks.get(topic);
 
-                if (cbList != null) {
-                    // check whether the list already contains same callback
-                    if (!cbList.contains(callback)) {
-                        cbList.add(callback);
-                    }
+                if (cbList != null && !cbList.contains(callback)) {
+                    cbList.add(callback);
                 } else {
                     cbList = new ArrayList<>();
                     cbList.add(callback);
@@ -561,9 +561,8 @@ public class Aitt {
      * @param topic String topic to which application had subscribed
      */
     public void unsubscribe(String topic) {
-        if (topic == null || topic.isEmpty()) {
+        if (topic == null || topic.isEmpty())
             throw new IllegalArgumentException(INVALID_TOPIC);
-        }
 
         boolean isRemoved = false;
         try {
@@ -578,15 +577,13 @@ public class Aitt {
             }
 
             if (!isRemoved) {
-                Long paittSubId = null;
+                Long pAittSubId = null;
                 synchronized (this) {
-                    if (aittSubId.containsKey(topic)) {
-                        paittSubId = aittSubId.get(topic);
-                    }
-                }
-                if (paittSubId != null) {
-                    mJniInterface.unsubscribe(topic, paittSubId);
+                    if (aittSubId.containsKey(topic))
+                        pAittSubId = aittSubId.get(topic);
                 }
+                if (pAittSubId != null)
+                    mJniInterface.unsubscribe(topic, pAittSubId);
             }
 
             synchronized (this) {
@@ -681,15 +678,14 @@ public class Aitt {
         try {
             ByteBuffer buffer = ByteBuffer.wrap(payload);
             FlexBuffers.Map map = FlexBuffers.getRoot(buffer).asMap();
-            String host = map.get("host").asString();
+            String host = map.get(HOST_STRING).asString();
             String status = map.get(Definitions.STATUS).asString();
             if (status != null && status.compareTo(Definitions.WILL_LEAVE_NETWORK) == 0) {
                 synchronized (this) {
                     for (Map.Entry<String, HostTable> entry : publishTable.entrySet()) {
                         HostTable hostTable = entry.getValue();
-                        if (hostTable != null) {
+                        if (hostTable != null)
                             hostTable.hostMap.remove(host);
-                        }
                     }
                 }
                 return;
@@ -698,9 +694,8 @@ public class Aitt {
             FlexBuffers.KeyVector topics = map.keys();
             for (int i = 0; i < topics.size(); i++) {
                 String _topic = topics.get(i).toString();
-                if (_topic.compareTo("host") == 0 || _topic.compareTo(Definitions.STATUS) == 0) {
+                if (HOST_STRING.compareTo(_topic) == 0 || Definitions.STATUS.compareTo(_topic) == 0)
                     continue;
-                }
 
                 FlexBuffers.Map _map = map.get(_topic).asMap();
                 int port = _map.get("port").asInt();
@@ -719,7 +714,7 @@ public class Aitt {
      * @param topic    The topic to which, other parties have subscribed to
      * @param host     String which specifies a particular host
      * @param port     Port of the party which subscribed to given topic
-     * @param protocol protocol supported by the party which subscribed to given topic
+     * @param protocol Protocol supported by the party which subscribed to given topic
      */
     private void updatePublishTable(String topic, String host, int port, Protocol protocol) {
         synchronized (this) {
@@ -755,7 +750,7 @@ public class Aitt {
             }
 
             // TODO: Handle multiple ports with the same topic.
-            if (portTable.portMap.isEmpty() == false) {
+            if (!portTable.portMap.isEmpty()) {
                 StringBuilder portLists = new StringBuilder();
                 for (int p : portTable.portMap.keySet()) {
                     portLists.append(p);
@@ -780,12 +775,12 @@ public class Aitt {
             String topic = message.getTopic();
             synchronized (this) {
                 ArrayList<SubscribeCallback> cbList = subscribeCallbacks.get(topic);
-
-                if (cbList != null) {
-                    for (int i = 0; i < cbList.size(); i++) {
-                        cbList.get(i).onMessageReceived(message);
-                    }
+                if (cbList == null) {
+                    Log.d(TAG, "Subscribe callback list is null.");
+                    return;
                 }
+
+                cbList.forEach(subscribeCallback -> subscribeCallback.onMessageReceived(message));
             }
         } catch (Exception e) {
             Log.e(TAG, "Error during messageReceived", e);
@@ -808,24 +803,36 @@ public class Aitt {
         }
     }
 
+    /**
+     * Method that receives message from JNI layer for topics other than discovery topics
+     *
+     * @param protocol The data received from JNI layer to be sent to application layer
+     * @param topic The data received from JNI layer to be sent to application layer
+     * @param streamRole The data received from JNI layer to be sent to application layer
+     */
     public AittStream createStream(Protocol protocol, String topic, AittStream.StreamRole streamRole) {
         ModuleHandler moduleHandler = createModuleHandler(protocol);
-        if (moduleHandler != null && protocol == Protocol.WEBRTC) {
-            WebRTCStream webRTCStream = (WebRTCStream) ((WebRTCHandler) moduleHandler).newStreamModule(protocol, topic, streamRole, appContext);
-            if (webRTCStream != null && streamRole == AittStream.StreamRole.SUBSCRIBER) {
-                webRTCStream.setSelfIP(ip);
-                webRTCStream.setJNIInterface(mJniInterface);
-            }
-
-            return webRTCStream;
+        if (moduleHandler == null) {
+            Log.e(TAG, "Fail to create a module handler.");
+            return null;
         }
-        if (moduleHandler != null && protocol == Protocol.RTSP) {
-            RTSPStream rtspStream = (RTSPStream) ((RTSPHandler) moduleHandler).newStreamModule(protocol, topic, streamRole, appContext);
-            if (rtspStream != null) {
-                rtspStream.setJNIInterface(mJniInterface);
-            }
 
-            return rtspStream;
+        switch (protocol) {
+            case WEBRTC:
+                WebRTCStream webRTCStream = (WebRTCStream) ((WebRTCHandler) moduleHandler).newStreamModule(protocol, topic, streamRole, appContext);
+                if (webRTCStream != null && streamRole == AittStream.StreamRole.SUBSCRIBER) {
+                    webRTCStream.setSelfIP(ip);
+                    webRTCStream.setJNIInterface(mJniInterface);
+                }
+                return webRTCStream;
+            case RTSP:
+                RTSPStream rtspStream = (RTSPStream) ((RTSPHandler) moduleHandler).newStreamModule(protocol, topic, streamRole, appContext);
+                if (rtspStream != null)
+                    rtspStream.setJNIInterface(mJniInterface);
+                return rtspStream;
+            default:
+                Log.d(TAG, "Not supported yet.");
+                break;
         }
 
         return null;
index b876bda..45fbc10 100644 (file)
@@ -251,7 +251,6 @@ public class AittUnitTest {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
 
-            assertNotNull("Aitt Instance not null", aitt);
             aitt.connect(brokerIp, port);
 
             aitt.close();
@@ -266,7 +265,6 @@ public class AittUnitTest {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
 
-            assertNotNull("Aitt Instance not null", aitt);
             aitt.connect(null);
 
             aitt.close();
@@ -280,8 +278,6 @@ public class AittUnitTest {
         try {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
-
-            assertNotNull("Aitt Instance not null", aitt);
             aitt.connect(brokerIp, port);
 
             aitt.disconnect();
@@ -295,8 +291,6 @@ public class AittUnitTest {
         try {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
-
-            assertNotNull("Aitt Instance not null", aitt);
             aitt.connect(brokerIp, port);
 
             byte[] payload = message.getBytes();
@@ -314,9 +308,9 @@ public class AittUnitTest {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
             aitt.connect(brokerIp, port);
+
             String _topic = "";
             byte[] payload = message.getBytes();
-
             assertThrows(IllegalArgumentException.class, () -> aitt.publish(_topic, payload));
 
             aitt.disconnect();
@@ -330,8 +324,6 @@ public class AittUnitTest {
         try {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
-
-            assertNotNull("Aitt Instance not null", aitt);
             aitt.connect(brokerIp, port);
 
             byte[] payload = message.getBytes();
@@ -349,9 +341,9 @@ public class AittUnitTest {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
             aitt.connect(brokerIp, port);
+
             String _topic = "";
             byte[] payload = message.getBytes();
-
             assertThrows(IllegalArgumentException.class, () -> aitt.publish(_topic, payload, Aitt.Protocol.WEBRTC, Aitt.QoS.AT_MOST_ONCE, false));
 
             aitt.disconnect();
@@ -365,8 +357,6 @@ public class AittUnitTest {
         try {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
-
-            assertNotNull("Aitt Instance not null", aitt);
             aitt.connect(brokerIp, port);
 
             byte[] payload = message.getBytes();
@@ -384,9 +374,9 @@ public class AittUnitTest {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
             aitt.connect(brokerIp, port);
+
             String _topic = "";
             byte[] payload = message.getBytes();
-
             assertThrows(IllegalArgumentException.class, () -> aitt.publish(_topic, payload, Aitt.Protocol.IPC, Aitt.QoS.AT_MOST_ONCE, false));
 
             aitt.disconnect();
@@ -400,8 +390,6 @@ public class AittUnitTest {
         try {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
-
-            assertNotNull("Aitt Instance not null", aitt);
             aitt.connect(brokerIp, port);
 
             byte[] payload = message.getBytes();
@@ -419,9 +407,9 @@ public class AittUnitTest {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
             aitt.connect(brokerIp, port);
+
             String _topic = "";
             byte[] payload = message.getBytes();
-
             assertThrows(IllegalArgumentException.class, () -> aitt.publish(_topic, payload, Aitt.Protocol.TCP, Aitt.QoS.AT_LEAST_ONCE, false));
 
             aitt.disconnect();
@@ -435,8 +423,6 @@ public class AittUnitTest {
         try {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
-
-            assertNotNull("Aitt Instance not null", aitt);
             aitt.connect(brokerIp, port);
 
             byte[] payload = message.getBytes();
@@ -454,13 +440,11 @@ public class AittUnitTest {
         try {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
-
-            assertNotNull("Aitt Instance not null", aitt);
             aitt.connect(brokerIp, port);
+
             String _topic = "";
             byte[] payload = message.getBytes();
             EnumSet<Aitt.Protocol> protocols = EnumSet.of(Aitt.Protocol.MQTT, Aitt.Protocol.TCP);
-
             assertThrows(IllegalArgumentException.class, () -> aitt.publish(_topic, payload, protocols, Aitt.QoS.AT_MOST_ONCE, false));
 
             aitt.disconnect();
@@ -475,9 +459,9 @@ public class AittUnitTest {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
             aitt.connect(brokerIp, port);
+
             byte[] payload = message.getBytes();
             EnumSet<Aitt.Protocol> protocols = EnumSet.noneOf(Aitt.Protocol.class);
-
             assertThrows(IllegalArgumentException.class, () -> aitt.publish(topic, payload, protocols, Aitt.QoS.AT_MOST_ONCE, false));
 
             aitt.disconnect();
@@ -491,8 +475,6 @@ public class AittUnitTest {
         try {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
-
-            assertNotNull("Aitt Instance not null", aitt);
             aitt.connect(brokerIp, port);
 
             aitt.subscribe(topic, message -> {
@@ -512,7 +494,6 @@ public class AittUnitTest {
             aitt.connect(brokerIp, port);
 
             String _topic = "";
-
             assertThrows(IllegalArgumentException.class, () -> aitt.subscribe(_topic, message -> {
             }));
 
@@ -527,7 +508,6 @@ public class AittUnitTest {
         try {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
-
             aitt.connect(brokerIp, port);
 
             assertThrows(IllegalArgumentException.class, () -> aitt.subscribe(topic, null));
@@ -543,8 +523,6 @@ public class AittUnitTest {
         try {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
-
-            assertNotNull("Aitt Instance not null", aitt);
             aitt.connect(brokerIp, port);
 
             aitt.subscribe(topic, message -> {
@@ -564,7 +542,6 @@ public class AittUnitTest {
             aitt.connect(brokerIp, port);
 
             String _topic = "";
-
             assertThrows(IllegalArgumentException.class, () -> aitt.subscribe(_topic, message -> {
             }, Aitt.Protocol.TCP));
 
@@ -579,7 +556,6 @@ public class AittUnitTest {
         try {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
-
             aitt.connect(brokerIp, port);
 
             assertThrows(IllegalArgumentException.class, () -> aitt.subscribe(topic, null, Aitt.Protocol.TCP));
@@ -595,8 +571,6 @@ public class AittUnitTest {
         try {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
-
-            assertNotNull("Aitt Instance not null", aitt);
             aitt.connect(brokerIp, port);
 
             aitt.subscribe(topic, message -> {
@@ -616,7 +590,6 @@ public class AittUnitTest {
             aitt.connect(brokerIp, port);
 
             String _topic = "";
-
             assertThrows(IllegalArgumentException.class, () -> aitt.subscribe(_topic, message -> {
             }, Aitt.Protocol.WEBRTC, Aitt.QoS.AT_MOST_ONCE));
 
@@ -631,7 +604,6 @@ public class AittUnitTest {
         try {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
-
             aitt.connect(brokerIp, port);
 
             assertThrows(IllegalArgumentException.class, () -> aitt.subscribe(topic, null, Aitt.Protocol.WEBRTC, Aitt.QoS.AT_MOST_ONCE));
@@ -647,8 +619,6 @@ public class AittUnitTest {
         try {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
-
-            assertNotNull("Aitt Instance not null", aitt);
             aitt.connect(brokerIp, port);
 
             aitt.subscribe(topic, message -> {
@@ -668,7 +638,6 @@ public class AittUnitTest {
             aitt.connect(brokerIp, port);
 
             String _topic = "";
-
             assertThrows(IllegalArgumentException.class, () -> aitt.subscribe(_topic, message -> {
             }, Aitt.Protocol.IPC, Aitt.QoS.AT_MOST_ONCE));
 
@@ -683,8 +652,6 @@ public class AittUnitTest {
         try {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
-
-            assertNotNull("Aitt Instance not null", aitt);
             aitt.connect(brokerIp, port);
 
             aitt.subscribe(topic, message -> {
@@ -704,7 +671,6 @@ public class AittUnitTest {
             aitt.connect(brokerIp, port);
 
             String _topic = "";
-
             assertThrows(IllegalArgumentException.class, () -> aitt.subscribe(_topic, message -> {
             }, Aitt.Protocol.TCP, Aitt.QoS.AT_MOST_ONCE));
 
@@ -719,7 +685,6 @@ public class AittUnitTest {
         try {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
-
             aitt.connect(brokerIp, port);
 
             assertThrows(IllegalArgumentException.class, () -> aitt.subscribe(topic, null, Aitt.Protocol.TCP, Aitt.QoS.AT_MOST_ONCE));
@@ -735,10 +700,9 @@ public class AittUnitTest {
         try {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
-
             aitt.connect(brokerIp, port);
-            EnumSet<Aitt.Protocol> protocols = EnumSet.noneOf(Aitt.Protocol.class);
 
+            EnumSet<Aitt.Protocol> protocols = EnumSet.noneOf(Aitt.Protocol.class);
             assertThrows(IllegalArgumentException.class, () -> aitt.subscribe(topic, message -> {
             }, protocols, Aitt.QoS.AT_MOST_ONCE));
 
@@ -753,8 +717,6 @@ public class AittUnitTest {
         try {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
-
-            assertNotNull("Aitt Instance not null", aitt);
             aitt.connect(brokerIp, port);
 
             EnumSet<Aitt.Protocol> protocols = EnumSet.of(Aitt.Protocol.MQTT, Aitt.Protocol.TCP);
@@ -790,8 +752,8 @@ public class AittUnitTest {
         try {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
-
             aitt.connect(brokerIp, port);
+
             EnumSet<Aitt.Protocol> protocols = EnumSet.of(Aitt.Protocol.MQTT, Aitt.Protocol.TCP);
             assertThrows(IllegalArgumentException.class, () -> aitt.subscribe(topic, null, protocols, Aitt.QoS.AT_MOST_ONCE));
 
@@ -806,10 +768,7 @@ public class AittUnitTest {
         try {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
-
-            assertNotNull("Aitt Instance not null", aitt);
             aitt.connect(brokerIp, port);
-
             aitt.subscribe(topic, message -> {
             });
 
@@ -825,10 +784,9 @@ public class AittUnitTest {
         try {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
-
             aitt.connect(brokerIp, port);
-            String _topic = "";
 
+            String _topic = "";
             assertThrows(IllegalArgumentException.class, () -> aitt.unsubscribe(_topic));
 
             aitt.disconnect();
@@ -843,7 +801,6 @@ public class AittUnitTest {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
 
-            assertNotNull("Aitt Instance not null", aitt);
             aitt.setConnectionCallback(new Aitt.ConnectionCallback() {
                 @Override
                 public void onConnected() {
@@ -857,8 +814,8 @@ public class AittUnitTest {
                 public void onConnectionFailed() {
                 }
             });
-            aitt.connect(brokerIp, port);
 
+            aitt.connect(brokerIp, port);
             aitt.disconnect();
         } catch (Exception e) {
             fail("Failed testSetConnectionCallback " + e);
@@ -885,17 +842,14 @@ public class AittUnitTest {
         try {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
-
-            assertNotNull("Aitt Instance not null", aitt);
             aitt.connect(brokerIp, port);
 
             Aitt.SubscribeCallback callback1 = message -> {
             };
+            aitt.subscribe(topic, callback1);
 
             Aitt.SubscribeCallback callback2 = message -> {
             };
-
-            aitt.subscribe(topic, callback1);
             aitt.subscribe(topic, callback2);
 
             aitt.disconnect();
@@ -910,8 +864,6 @@ public class AittUnitTest {
         try {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
-
-            assertNotNull("Aitt Instance not null", aitt);
             aitt.connect(brokerIp, port);
 
             int counter = 1;
@@ -923,7 +875,7 @@ public class AittUnitTest {
 
             aitt.disconnect();
         } catch (Exception e) {
-            fail("Failed testDiscoveryMessageCallback " + e);
+            fail("Failed testDiscoveryMessageCallbackConnected " + e);
         }
     }
 
@@ -932,8 +884,6 @@ public class AittUnitTest {
         try {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
-
-            assertNotNull("Aitt Instance not null", aitt);
             aitt.connect(brokerIp, port);
 
             int counter = 1;
@@ -943,9 +893,10 @@ public class AittUnitTest {
             counter = 6;
             byte[] disconnectMessage = createDiscoveryMessage(counter);
             messageCallbackMethod.invoke(aitt, Definitions.JAVA_SPECIFIC_DISCOVERY_TOPIC, disconnectMessage);
+
             aitt.disconnect();
         } catch (Exception e) {
-            fail("Failed testDiscoveryMessageCallback " + e);
+            fail("Failed testDiscoveryMessageCallbackDisconnected " + e);
         }
     }
 
@@ -954,8 +905,6 @@ public class AittUnitTest {
         try {
             shadowJniInterface.setInitReturn(true);
             Aitt aitt = new Aitt(appContext, aittId);
-
-            assertNotNull("Aitt Instance not null", aitt);
             aitt.connect(brokerIp, port);
 
             byte[] discoveryMessage = new byte[0];
@@ -978,12 +927,11 @@ public class AittUnitTest {
                 String receivedTopic = aittMessage.getTopic();
                 assertEquals("Received topic and subscribed topic are equal", receivedTopic, topic);
             });
-
             messageCallbackMethod.invoke(aitt, topic, message.getBytes(StandardCharsets.UTF_8));
 
             aitt.disconnect();
         } catch (Exception e) {
-            fail("Failed testSubscribeCallback " + e);
+            fail("Failed testSubscribeCallbackVerifyTopic " + e);
         }
     }
 
@@ -998,12 +946,11 @@ public class AittUnitTest {
                 String receivedMessage = new String(aittMessage.getPayload(), StandardCharsets.UTF_8);
                 assertEquals("Received message and sent message are equal", message, receivedMessage);
             });
-
             messageCallbackMethod.invoke(aitt, topic, message.getBytes(StandardCharsets.UTF_8));
 
             aitt.disconnect();
         } catch (Exception e) {
-            fail("Failed testSubscribeCallback " + e);
+            fail("Failed testSubscribeCallbackVerifyPayload " + e);
         }
     }
 
index fe0b715..b0a6ee8 100644 (file)
@@ -34,15 +34,22 @@ public class TCPInstrumentedTest {
 
     private static final String TAG = "AITT-ANDROID";
     private static final String AITT_ID = "AITT_ANDROID";
-    private static final String BROKER_IP = "192.168.1.59"; // TODO: Replace with 'localhost' value.
     private static final int PORT = 1883;
     private static final String TEST_TOPIC = "android/test/tcp";
     private static final String TEST_MESSAGE = "This is a test message for TCP protocol.";
     private static final String ERROR_MESSAGE_AITT_NULL = "An AITT instance is null.";
 
+    private static String brokerIp;
+
     @BeforeClass
     public static void initialize() {
         appContext = InstrumentationRegistry.getInstrumentation().getTargetContext();
+        // IMPORTANT NOTE: Should give test arguments as follows.
+        // if using Android studio: Run -> Edit Configurations -> Find 'Instrumentation arguments'
+        //                         -> press '...' button -> add the name as "brokerIp" and the value
+        //                         (Broker WiFi IP) of broker argument
+        // if using gradlew commands: Add "-e brokerIp [Broker WiFi IP]"
+        brokerIp = InstrumentationRegistry.getArguments().getString("brokerIp");
     }
 
     @Test
@@ -50,12 +57,12 @@ public class TCPInstrumentedTest {
         try {
             Aitt aitt = new Aitt(appContext, AITT_ID);
             assertNotNull(ERROR_MESSAGE_AITT_NULL, aitt);
-            aitt.connect(BROKER_IP, PORT);
+            aitt.connect(brokerIp, PORT);
 
             byte[] payload = TEST_MESSAGE.getBytes();
             aitt.publish(TEST_TOPIC, payload, Aitt.Protocol.TCP, Aitt.QoS.AT_LEAST_ONCE, false);
         } catch (Exception e) {
-            fail("Failed to execute testPublishWithTCP_P, (" + e + ")");
+            fail("Failed to execute testPublishWithTCP, (" + e + ")");
         }
     }
 
@@ -64,7 +71,7 @@ public class TCPInstrumentedTest {
         try {
             Aitt aitt = new Aitt(appContext, AITT_ID);
             assertNotNull(ERROR_MESSAGE_AITT_NULL, aitt);
-            aitt.connect(BROKER_IP, PORT);
+            aitt.connect(brokerIp, PORT);
 
             String _topic = "";
             byte[] payload = TEST_MESSAGE.getBytes();
@@ -73,7 +80,7 @@ public class TCPInstrumentedTest {
 
             aitt.disconnect();
         } catch (Exception e) {
-            fail("Failed testPublishWithTCPInvalidTopic_N, (" + e + ")");
+            fail("Failed testPublishWithTCPInvalidTopic, (" + e + ")");
         }
     }
 
@@ -82,13 +89,13 @@ public class TCPInstrumentedTest {
         try {
             Aitt aitt = new Aitt(appContext, AITT_ID);
             assertNotNull(ERROR_MESSAGE_AITT_NULL, aitt);
-            aitt.connect(BROKER_IP, PORT);
+            aitt.connect(brokerIp, PORT);
 
             aitt.subscribe(TEST_TOPIC, message -> Log.i(TAG, "A subscription callback is called."), Aitt.Protocol.TCP, Aitt.QoS.AT_LEAST_ONCE);
 
             aitt.disconnect();
         } catch (Exception e) {
-            fail("Failed to execute testSubscribeWithTCP_P, (" + e + ")");
+            fail("Failed to execute testSubscribeWithTCP, (" + e + ")");
         }
     }
 
@@ -97,7 +104,7 @@ public class TCPInstrumentedTest {
         try {
             Aitt aitt = new Aitt(appContext, AITT_ID);
             assertNotNull(ERROR_MESSAGE_AITT_NULL, aitt);
-            aitt.connect(BROKER_IP, PORT);
+            aitt.connect(brokerIp, PORT);
 
             Aitt.SubscribeCallback callback1 = message -> {
             };
@@ -110,7 +117,7 @@ public class TCPInstrumentedTest {
 
             aitt.disconnect();
         } catch (Exception e) {
-            fail("Failed testSubscribeWithTCPMultipleCallbacks_P, (" + e + ")");
+            fail("Failed testSubscribeWithTCPMultipleCallbacks, (" + e + ")");
         }
     }
 
@@ -119,7 +126,7 @@ public class TCPInstrumentedTest {
         try {
             Aitt aitt = new Aitt(appContext, AITT_ID);
             assertNotNull(ERROR_MESSAGE_AITT_NULL, aitt);
-            aitt.connect(BROKER_IP, PORT);
+            aitt.connect(brokerIp, PORT);
 
             String _topic = "";
             assertThrows(IllegalArgumentException.class, () -> aitt.subscribe(_topic, message -> {
@@ -127,7 +134,7 @@ public class TCPInstrumentedTest {
 
             aitt.disconnect();
         } catch (Exception e) {
-            fail("Failed testSubscribeWithTCPInvalidTopic_N, (" + e + ")");
+            fail("Failed testSubscribeWithTCPInvalidTopic, (" + e + ")");
         }
     }
 
@@ -136,14 +143,14 @@ public class TCPInstrumentedTest {
         try {
             Aitt aitt = new Aitt(appContext, AITT_ID);
             assertNotNull(ERROR_MESSAGE_AITT_NULL, aitt);
-            aitt.connect(BROKER_IP, PORT);
+            aitt.connect(brokerIp, PORT);
 
             String _topic = "topic";
             assertThrows(IllegalArgumentException.class, () -> aitt.subscribe(_topic, null, Aitt.Protocol.TCP, Aitt.QoS.AT_LEAST_ONCE));
 
             aitt.disconnect();
         } catch (Exception e) {
-            fail("Failed testSubscribeWithTCPInvalidCallback_N, (" + e + ")");
+            fail("Failed testSubscribeWithTCPInvalidCallback, (" + e + ")");
         }
     }
 
@@ -152,7 +159,7 @@ public class TCPInstrumentedTest {
         try {
             Aitt aitt = new Aitt(appContext, AITT_ID);
             assertNotNull(ERROR_MESSAGE_AITT_NULL, aitt);
-            aitt.connect(BROKER_IP, PORT);
+            aitt.connect(brokerIp, PORT);
             aitt.subscribe(TEST_TOPIC, message -> {
             }, Aitt.Protocol.TCP, Aitt.QoS.AT_LEAST_ONCE);
 
@@ -160,7 +167,7 @@ public class TCPInstrumentedTest {
 
             aitt.disconnect();
         } catch (Exception e) {
-            fail("Failed testUnsubscribe_P, (" + e + ")");
+            fail("Failed testUnsubscribe, (" + e + ")");
         }
     }
 
@@ -170,7 +177,7 @@ public class TCPInstrumentedTest {
             String wifiIp = wifiIpAddress();
             Aitt aitt = new Aitt(appContext, AITT_ID, wifiIp, false);
             assertNotNull(ERROR_MESSAGE_AITT_NULL, aitt);
-            aitt.connect(BROKER_IP, PORT);
+            aitt.connect(brokerIp, PORT);
 
             AtomicBoolean message_received = new AtomicBoolean(false);
             byte[] payload = TEST_MESSAGE.getBytes();
@@ -194,7 +201,7 @@ public class TCPInstrumentedTest {
 
             Assert.assertTrue(message_received.get());
         } catch (Exception e) {
-            fail("Failed to execute testPublishSubscribeWithTCP_P, (" + e + ")");
+            fail("Failed to execute testPublishSubscribeWithTCP, (" + e + ")");
         }
     }