<commit_before>/*
 * ALMA - Atacama Large Millimiter Array (c) European Southern Observatory, 2006
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 */
package alma.acs.lasercore.test;

import java.io.File;
import java.util.HashMap;

import alma.acs.component.client.ComponentClientTestCase;
import alma.acs.container.ContainerServices;
import alma.acs.lasercore.test.stress.CategoryClient;
import alma.acs.lasercore.test.stress.category.AlarmView;
import alma.acs.lasercore.test.stress.category.CategoryListener;
import alma.alarmsystem.source.ACSAlarmSystemInterfaceFactory;

/**
 * Test the sending of core alarms from the alarm component.
 * <P>
 * The test checks if the alarm component send an alarm if one file
 * of the CDB (for instance <code>PS.xml</code>) is unreadable.
 * <P>
 * The test works by changing the readability property of <code>PS.xml</code>.
 * When the alarm component starts, it sends an alarm about a misconfigured CDB.
 * The alarm will be received by the category client.
 * <BR>
 * Note that the test does not do a real work: it only change the property of 
 * the file and connect the category client. As a consequence, the test method
 * is empty.
 * 
 * @author acaproni
 *
 */
public class CoreCDBAlarmTest extends ComponentClientTestCase implements CategoryListener {
	
	/**
	 *  The category client
	 */
	private CategoryClient categoryClient;
	
	/**
	 *  Container services
	 */
	private ContainerServices contSvcs;
	
	/**
	 * The vector with the alarms received from the categories.
	 * The key is the alarm ID
	 */
	private HashMap<String,AlarmView> alarms = new HashMap<String,AlarmView>();
	
	/**
	 * Constructor
	 * 
	 * @throws Exception
	 */
	public CoreCDBAlarmTest() throws Exception {
		super(CoreCDBAlarmTest.class.getName());
	}

	/**
	 * @see alma.acs.lasercore.test.stress.category.CategoryListener#alarmReceived(alma.acs.lasercore.test.stress.category.AlarmView)
	 */
	@Override
	public void alarmReceived(AlarmView alarm) {
		synchronized (alarms) {
			alarms.put(alarm.alarmID, alarm);
		}
	}

	/**
	 * @see alma.acs.component.client.ComponentClientTestCase#setUp()
	 */
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		// Change the permission of PS.xml
		File f = new File("CDB//Alarms//AlarmDefinitions//PS//PS.xml");
		assertTrue(f.canRead());
		f.setReadable(false);
		assertFalse(f.canRead());
		
		contSvcs = getContainerServices();
		assertNotNull(contSvcs);
		
		// Connect the categories
		categoryClient= new CategoryClient(contSvcs);
		assertNotNull(categoryClient);
		categoryClient.addAlarmListener(this);
		categoryClient.connect();
	}

	/**
	 * @see alma.acs.component.client.ComponentClientTestCase#tearDown()
	 */
	@Override
	protected void tearDown() throws Exception {
		categoryClient.disconnect();
		File f = new File("CDB//Alarms//AlarmDefinitions//PS//PS.xml");
		assertFalse(f.canRead());
		f.setReadable(true);
		assertTrue(f.canRead());
		super.tearDown();
	}
	
	/**
	 * Connect the category client and wait until the alarm is received
	 * of e timeout occur.
	 * 
	 * @throws Exception
	 */
	public void testCDBAlarm() throws Exception {
		long timeout=System.currentTimeMillis()+60*1000;;
		do {
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				continue;
			}
			synchronized (alarms) {
				if (alarms.size()>0) {
					break;
				}
			}
		} while (System.currentTimeMillis()<timeout); 
		assertEquals(1, alarms.size());
	}

}<commit_msg>The test is performed as follows  1. shut down the alarm service  2. change the permission of an XML file  3. restart the alarm service  4. wait for the alarm generated by the alarm service  5. check the type of the alarm<commit_after>/*
 * ALMA - Atacama Large Millimiter Array (c) European Southern Observatory, 2006
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 2.1 of the License, or (at your option)
 * any later version.
 * 
 * This library is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
 * details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with this library; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * 
 */
package alma.acs.lasercore.test;

import java.io.File;
import java.util.Collections;
import java.util.LinkedList;
import java.util.List;
import java.util.logging.Logger;

import alma.acs.component.client.ComponentClientTestCase;
import alma.acs.container.ContainerServices;
import alma.acs.lasercore.test.stress.CategoryClient;
import alma.acs.lasercore.test.stress.category.AlarmView;
import alma.acs.lasercore.test.stress.category.CategoryListener;
import alma.alarmsystem.AlarmService;
import alma.alarmsystem.corbaservice.utils.AlarmServiceUtils;
import alma.alarmsystem.core.alarms.LaserCoreFaultState;
import alma.alarmsystem.core.alarms.LaserCoreFaultState.LaserCoreFaultCodes;

/**
 * Test the sending of core alarms from the alarm component.
 * <P>
 * The test checks if the alarm component send an alarm if one file
 * of the CDB (for instance <code>PS.xml</code>) is unreadable.
 * <P>
 * The test works by changing the readability property of <code>PS.xml</code>.
 * When the alarm component starts, it sends an alarm about a misconfigured CDB.
 * The alarm will be received by the category client.
 * 
 * @author acaproni
 *
 */
public class CoreCDBAlarmTest extends ComponentClientTestCase implements CategoryListener {
	
	/**
	 *  The category client
	 */
	private CategoryClient categoryClient;
	
	/**
	 *  Container services
	 */
	private ContainerServices contSvcs;
	
	/**
	 * The logger
	 */
	private Logger logger;
	
	/**
	 * The vector with the alarms received from the categories.
	 * The key is the alarm ID
	 */
	private final List<AlarmView> alarms = Collections.synchronizedList(new LinkedList<AlarmView>());
	
	/**
	 * Constructor
	 * 
	 * @throws Exception
	 */
	public CoreCDBAlarmTest() throws Exception {
		super(CoreCDBAlarmTest.class.getName());
	}

	/**
	 * @see alma.acs.lasercore.test.stress.category.CategoryListener#alarmReceived(alma.acs.lasercore.test.stress.category.AlarmView)
	 */
	@Override
	public void alarmReceived(AlarmView alarm) {
		alarms.add(alarm);
	}

	/**
	 * @see alma.acs.component.client.ComponentClientTestCase#setUp()
	 */
	@Override
	protected void setUp() throws Exception {
		super.setUp();
		contSvcs = getContainerServices();
		assertNotNull(contSvcs);
		logger=contSvcs.getLogger();
		assertNotNull(logger);
		
		// Connect the categories
		categoryClient= new CategoryClient(contSvcs);
		assertNotNull(categoryClient);
		categoryClient.addAlarmListener(this);
		categoryClient.connect();
	}

	/**
	 * @see alma.acs.component.client.ComponentClientTestCase#tearDown()
	 */
	@Override
	protected void tearDown() throws Exception {
		categoryClient.disconnect();
		super.tearDown();
	}
	
	/**
	 * To test if the alarm service send an alarm in case of an error 
	 * in the CDB:
	 * <OL>
	 * 	<LI>shut down the AS
	 * 	<LI>change the reading permission of a file
	 * 	<LI>restart the AS
	 * 	<LI>wait for the alarm or a timeout
	 * </OL>
	 * 
	 * @throws Exception
	 */
	public void testCDBAlarm() throws Exception {
		// Shuts down the AS
		shutdownAS();
		// Change the permission of PS.xml
		File f = new File("CDB//Alarms//AlarmDefinitions//PS//PS.xml");
		assertTrue(f.canRead());
		f.setReadable(false);
		assertFalse(f.canRead());
		// Start the AS again
		startAS();
		
		logger.info("Waiting for alarms");
		long timeout=System.currentTimeMillis()+60*1000;;
		do {
			try {
				Thread.sleep(500);
			} catch (InterruptedException e) {
				continue;
			}
			if (alarms.size()>0) {
				break;
			}
		} while (System.currentTimeMillis()<timeout); 
		assertEquals(1, alarms.size());
		// Check if the alarm is the right one
		AlarmView alarm = alarms.get(0);
		assertNotNull(alarm);
		String expectedID=LaserCoreFaultState.FaultFamily+":"+LaserCoreFaultState.FaultMember+":"+LaserCoreFaultCodes.ALARMS_CDB.faultCode;
		assertEquals(expectedID, alarm.alarmID);
		
		// Restore the reading permission of the file
		f = new File("CDB//Alarms//AlarmDefinitions//PS//PS.xml");
		assertFalse(f.canRead());
		f.setReadable(true);
		assertTrue(f.canRead());
	}
	
	/**
	 * Shuts down the alarm service
	 */
	private void shutdownAS() throws Exception {
		logger.info("Shutting down the AS");
		AlarmServiceUtils utils = new AlarmServiceUtils(contSvcs);
		assertNotNull(utils);
		AlarmService alarmService = utils.getAlarmService();
		assertNotNull(alarmService);
		alarmService.shutdown();
		logger.info("Leaving the AS time to shut down");
		// Leave the AS time to shut down
		try {
			Thread.sleep(30*1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Launch the alarm service
	 * 
	 * @throws Exception
	 */
	private void startAS() throws Exception {
		logger.info("Restarting the AS");
		File f=new File("../bin/alarmService");
		assertTrue(f.canExecute());
		Process p = Runtime.getRuntime().exec(f.getAbsolutePath());
		logger.info("Leaving the AS time to startup");
		// Leave the AS time to shut down
		try {
			Thread.sleep(60*1000);
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
	}

}
{'fraction_non_alphanumeric': 0.10656378198866966, 'fraction_numerical': 0.007814026176987694, 'mean_word_length': 3.924963924963925, 'pattern_counts': {'":': 2, '<': 28, '<?xml version=': 0, '>': 28, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 10}, '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': '3320880', 'n_tokens_mistral': 3371, 'n_tokens_neox': 3050, 'n_words': 950}