<commit_before>/**
 *****************************************************************************
 Copyright (c) 2016 IBM Corporation and other Contributors.
 All rights reserved. This program and the accompanying materials
 are made available under the terms of the Eclipse Public License v1.0
 which accompanies this distribution, and is available at
 http://www.eclipse.org/legal/epl-v10.html
 Contributors:
 <NAME> - Initial Contribution
 *****************************************************************************
 *
 */
package com.ibm.iotf.client.gateway;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Properties;
import java.util.logging.Level;

import org.eclipse.paho.client.mqttv3.MqttException;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.*;

import com.google.gson.JsonObject;
import com.ibm.iotf.client.IoTFCReSTException;
import com.ibm.iotf.client.api.APIClient;
import com.ibm.iotf.client.app.ApplicationClient;
import com.ibm.iotf.test.common.TestEnv;
import com.ibm.iotf.util.LoggerUtility;

/**
 * This test verifies that the device receives the command published by the application
 * successfully.
 *
 */
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class GatewayCommandSubscriptionTest {
	
	private static final String CLASS_NAME = GatewayCommandSubscriptionTest.class.getName();
	private static final String APP_ID = "GwCmdSubApp1";
	
	private final static String DEVICE_TYPE = "SubType1";
	private final static String DEVICE_ID_PREFIX = "SubDev";
	private final static String GW_DEVICE_TYPE = "GwCmdSubType1";
	private final static String GW_DEVICE_ID_PREFIX = "GwCmdSubDev";

	private static APIClient apiClient = null;
	private static ApplicationClient mqttAppClient = null;
	
	private static int testNum = 1;
	private final static int totalTests = 10;
	
	private static HashMap<Integer,TestHelper> testMap = new HashMap<Integer,TestHelper>();
	
	private synchronized int getTestNumber() {
		int number = testNum;
		testNum++;
		return number;
	}
	
	private static void deleteDevice(String devType, String devId) {
		final String METHOD = "deleteDevices";
		boolean exist = false;
		try {
			exist = apiClient.isDeviceExist(devType, devId);
		} catch (IoTFCReSTException e1) {
			e1.printStackTrace();
		}
		
		if (exist) {
			try {
				apiClient.deleteDevice(devType, devId);
				LoggerUtility.info(CLASS_NAME, METHOD, "Device ID " + devId + " has been deleted.");
			} catch (IoTFCReSTException e) {
				e.printStackTrace();
			}
		}			
	}
	
	@BeforeClass
	public static void oneTimeSetup() {
		
		final String METHOD = "oneTimeSetup";
		
		Properties appProps = TestEnv.getAppProperties(APP_ID, false, DEVICE_TYPE, null);
		
		try {
			apiClient = new APIClient(appProps);
		} catch (KeyManagementException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}

		boolean exist = false;
		try {
			exist = apiClient.isDeviceTypeExist(GW_DEVICE_TYPE);
			
		} catch (IoTFCReSTException e1) {
			e1.printStackTrace();
		}
		
		if (!exist) {
			JsonObject jsonGW = new JsonObject();
			jsonGW.addProperty("id", GW_DEVICE_TYPE);
			try {
				apiClient.addGatewayDeviceType(jsonGW);
				LoggerUtility.info(CLASS_NAME, METHOD, "Gateway device type " + GW_DEVICE_TYPE + " has been created.");
			} catch (IoTFCReSTException e) {
				e.printStackTrace();
			}
		}

		try {
			exist = apiClient.isDeviceTypeExist(DEVICE_TYPE);
		} catch (IoTFCReSTException e) {
			e.printStackTrace();
		}
		
		if (!exist) {
			try {
				apiClient.addDeviceType(DEVICE_TYPE, null, null, null);
				LoggerUtility.info(CLASS_NAME, METHOD, "Device type " + DEVICE_TYPE + " has been created.");
			} catch (IoTFCReSTException e) {
				e.printStackTrace();
			}
		}
		
		for (int i=1; i<= totalTests; i++) {			
			String devId = new String(DEVICE_ID_PREFIX + "_" + i);
			String gwDevId = new String(GW_DEVICE_ID_PREFIX + "_" + i);
			
			deleteDevice(DEVICE_TYPE, devId);
			deleteDevice(GW_DEVICE_TYPE, gwDevId);
			
			// Register gateway device
			try {
				apiClient.registerDevice(GW_DEVICE_TYPE, gwDevId, TestEnv.getGatewayToken(), null, null, null);
				LoggerUtility.info(CLASS_NAME, METHOD, "Gateway device " + gwDevId + " has been created.");
			} catch (IoTFCReSTException e) {
				e.printStackTrace();
			}
			
			// Register device under gateway
			try {
				apiClient.registerDeviceUnderGateway(DEVICE_TYPE, devId, GW_DEVICE_TYPE, gwDevId);
				LoggerUtility.info(CLASS_NAME, METHOD, "Device " + devId + " has been created.");
			} catch (IoTFCReSTException e) {
				e.printStackTrace();
			}
			
			Integer iTest = new Integer(i);
			TestHelper testHelper = null;
			try {
				testHelper = new TestHelper(GW_DEVICE_TYPE, gwDevId, DEVICE_TYPE, devId);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			testMap.put(iTest, testHelper);
		}
		
		for (int i=1; i <= totalTests; i++) {
			Integer iTest = new Integer(i);
			TestHelper testHelper = testMap.get(iTest);
			if (testHelper != null) {
				try {
					exist = apiClient.isDeviceExist(testHelper.getAttachedDeviceType(), testHelper.getAttachedDeviceId());
					LoggerUtility.info(CLASS_NAME, METHOD, "Device " + testHelper.getAttachedDeviceId() + " does exist.");
					exist = apiClient.isDeviceExist(testHelper.getGatewayDeviceType(), testHelper.getGatewayDeviceId());
					LoggerUtility.info(CLASS_NAME, METHOD, "Gateway device " + testHelper.getGatewayDeviceId() + " does exist.");
					
					JsonObject jsonProps = apiClient.getAccessControlProperties(testHelper.getGatewayDeviceId(), null);
					if (jsonProps != null) {
						LoggerUtility.info(CLASS_NAME, METHOD, testHelper.getGatewayDeviceId() + " access control : " 
								+ jsonProps.getAsString());
					}
					
				} catch (IoTFCReSTException e) { 
					e.printStackTrace();
				}
			}
		}
		LoggerUtility.info(CLASS_NAME, METHOD, METHOD + " setup is complete");
		
	}
	
	@AfterClass
	public static void oneTimeTearDown() {
		final String METHOD = "oneTimeTearDown";<commit_msg>Update junit tests 32<commit_after>/**
 *****************************************************************************
 Copyright (c) 2016 IBM Corporation and other Contributors.
 All rights reserved. This program and the accompanying materials
 are made available under the terms of the Eclipse Public License v1.0
 which accompanies this distribution, and is available at
 http://www.eclipse.org/legal/epl-v10.html
 Contributors:
 <NAME> - Initial Contribution
 *****************************************************************************
 *
 */
package com.ibm.iotf.client.gateway;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.UnsupportedEncodingException;
import java.security.KeyManagementException;
import java.security.NoSuchAlgorithmException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Properties;
import java.util.logging.Level;

import org.eclipse.paho.client.mqttv3.MqttException;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.*;

import com.google.gson.JsonObject;
import com.ibm.iotf.client.IoTFCReSTException;
import com.ibm.iotf.client.api.APIClient;
import com.ibm.iotf.client.app.ApplicationClient;
import com.ibm.iotf.test.common.TestEnv;
import com.ibm.iotf.util.LoggerUtility;

/**
 * This test verifies that the device receives the command published by the application
 * successfully.
 *
 */
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class GatewayCommandSubscriptionTest {
	
	private static final String CLASS_NAME = GatewayCommandSubscriptionTest.class.getName();
	private static final String APP_ID = "GwCmdSubApp1";
	
	private final static String DEVICE_TYPE = "SubType1";
	private final static String DEVICE_ID_PREFIX = "SubDev";
	private final static String GW_DEVICE_TYPE = "GwCmdSubType1";
	private final static String GW_DEVICE_ID_PREFIX = "GwCmdSubDev";

	private static APIClient apiClient = null;
	private static ApplicationClient mqttAppClient = null;
	
	private static int testNum = 1;
	private final static int totalTests = 10;
	
	private static HashMap<Integer,TestHelper> testMap = new HashMap<Integer,TestHelper>();
	
	private synchronized int getTestNumber() {
		int number = testNum;
		testNum++;
		return number;
	}
	
	private static void deleteDevice(String devType, String devId) {
		final String METHOD = "deleteDevices";
		boolean exist = false;
		try {
			exist = apiClient.isDeviceExist(devType, devId);
		} catch (IoTFCReSTException e1) {
			e1.printStackTrace();
		}
		
		if (exist) {
			try {
				apiClient.deleteDevice(devType, devId);
				LoggerUtility.info(CLASS_NAME, METHOD, "Device ID " + devId + " has been deleted.");
			} catch (IoTFCReSTException e) {
				e.printStackTrace();
			}
		}			
	}
	
	@BeforeClass
	public static void oneTimeSetup() {
		
		final String METHOD = "oneTimeSetup";
		
		Properties appProps = TestEnv.getAppProperties(APP_ID, false, DEVICE_TYPE, null);
		
		try {
			apiClient = new APIClient(appProps);
		} catch (KeyManagementException e) {
			e.printStackTrace();
		} catch (NoSuchAlgorithmException e) {
			e.printStackTrace();
		}

		boolean exist = false;
		try {
			exist = apiClient.isDeviceTypeExist(GW_DEVICE_TYPE);
			
		} catch (IoTFCReSTException e1) {
			e1.printStackTrace();
		}
		
		if (!exist) {
			JsonObject jsonGW = new JsonObject();
			jsonGW.addProperty("id", GW_DEVICE_TYPE);
			try {
				apiClient.addGatewayDeviceType(jsonGW);
				LoggerUtility.info(CLASS_NAME, METHOD, "Gateway device type " + GW_DEVICE_TYPE + " has been created.");
			} catch (IoTFCReSTException e) {
				e.printStackTrace();
			}
		}

		try {
			exist = apiClient.isDeviceTypeExist(DEVICE_TYPE);
		} catch (IoTFCReSTException e) {
			e.printStackTrace();
		}
		
		if (!exist) {
			try {
				apiClient.addDeviceType(DEVICE_TYPE, null, null, null);
				LoggerUtility.info(CLASS_NAME, METHOD, "Device type " + DEVICE_TYPE + " has been created.");
			} catch (IoTFCReSTException e) {
				e.printStackTrace();
			}
		}
		
		for (int i=1; i<= totalTests; i++) {			
			String devId = new String(DEVICE_ID_PREFIX + "_" + i);
			String gwDevId = new String(GW_DEVICE_ID_PREFIX + "_" + i);
			
			deleteDevice(DEVICE_TYPE, devId);
			deleteDevice(GW_DEVICE_TYPE, gwDevId);
			
			// Register gateway device
			try {
				apiClient.registerDevice(GW_DEVICE_TYPE, gwDevId, TestEnv.getGatewayToken(), null, null, null);
				LoggerUtility.info(CLASS_NAME, METHOD, "Gateway device " + gwDevId + " has been created.");
			} catch (IoTFCReSTException e) {
				e.printStackTrace();
			}
			
			// Register device under gateway
			try {
				apiClient.registerDeviceUnderGateway(DEVICE_TYPE, devId, GW_DEVICE_TYPE, gwDevId);
				LoggerUtility.info(CLASS_NAME, METHOD, "Device " + devId + " has been created.");
			} catch (IoTFCReSTException e) {
				e.printStackTrace();
			}
			
			Integer iTest = new Integer(i);
			TestHelper testHelper = null;
			try {
				testHelper = new TestHelper(GW_DEVICE_TYPE, gwDevId, DEVICE_TYPE, devId);
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			testMap.put(iTest, testHelper);
		}
		
		for (int i=1; i <= totalTests; i++) {
			Integer iTest = new Integer(i);
			TestHelper testHelper = testMap.get(iTest);
			if (testHelper != null) {
				try {
					exist = apiClient.isDeviceExist(testHelper.getAttachedDeviceType(), testHelper.getAttachedDeviceId());
					LoggerUtility.info(CLASS_NAME, METHOD, "Device " + testHelper.getAttachedDeviceId() + " does exist.");
					exist = apiClient.isDeviceExist(testHelper.getGatewayDeviceType(), testHelper.getGatewayDeviceId());
					LoggerUtility.info(CLASS_NAME, METHOD, "Gateway device " + testHelper.getGatewayDeviceId() + " does exist.");
					
					JsonObject jsonProps = null;
					try {
						jsonProps = apiClient.getAccessControlProperties(testHelper.getGatewayDeviceId(), null);
					} catch (UnsupportedEncodingException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					if (jsonProps != null) {
						LoggerUtility.info(CLASS_NAME, METHOD, testHelper.getGatewayDeviceId() + " access control : " 
								+ jsonProps.getAsString());
					}
					
				} catch (IoTFCReSTException e) { 
					e.printStackTrace();
				}
			}
		}
		LoggerUtility.info(CLASS_NAME, METHOD, METHOD + " setup is complete");
		
	}
	
	@AfterClass
	public static void oneTimeTearDown() {
		final String METHOD = "oneTimeTearDown";
{'fraction_non_alphanumeric': 0.12911216038393064, 'fraction_numerical': 0.003405836365043734, 'mean_word_length': 4.159744408945687, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3838172', 'n_tokens_mistral': 4610, 'n_tokens_neox': 3922, 'n_words': 935}