/*
 * Copyright 2015 Adaptris Ltd.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.adaptris.core.services;

import java.io.File;

import org.junit.Test;

import com.adaptris.core.AdaptrisMessage;
import com.adaptris.core.AdaptrisMessageFactory;
import com.adaptris.core.GeneralServiceExample;
import com.adaptris.core.ServiceException;
import com.adaptris.core.stubs.DefectiveMessageFactory;

public class ZipServiceTest extends GeneralServiceExample
{

	public ZipServiceTest(final String testName)
	{
		super(testName);
	}

	@Override
	protected void setUp() throws Exception
	{
		/* empty method */
	}

	@Test
	public void testZipDirectoryService() throws Exception
	{
		final AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage();
		msg.addMetadata("zip-path", "ivy");
		final ZipDirectoryService zip = new ZipDirectoryService();
		zip.setDirectoryPath("%message{zip-path}");
		execute(zip, msg);

		final byte[] zippedData = msg.getPayload();

		msg.setPayload(zippedData);
		execute(new UnzipDirectoryService(), msg);
		final String unzippedPath = msg.getContent();

		File dir = new File(unzippedPath); // the root extracted directory ($TMP/$message-id)
		assertTrue(dir.isDirectory());
		dir = new File(dir.getAbsolutePath(), "ivy");
		assertTrue(dir.isDirectory());
		for (final File f : dir.listFiles())
		{
			File f2 = new File("ivy", f.getName());
			assertTrue(f2.exists());
		}
	}

	@Test
	public void testZipDirectoryServiceSingleFile() throws Exception
	{
		final AdaptrisMessage msg = AdaptrisMessageFactory.getDefaultInstance().newMessage();
		msg.addMetadata("zip-path", "build.xml");
		final ZipDirectoryService zip = new ZipDirectoryService();
		zip.setDirectoryPath("%message{zip-path}");
		execute(zip, msg);

		final byte[] zippedData = msg.getPayload();

		msg.setPayload(zippedData);
		execute(new UnzipDirectoryService(), msg);
		final String unzippedPath = msg.getContent();

		final File dir = new File(unzippedPath);
		assertTrue(dir.isDirectory());
		final File file = new File(dir, "build.xml");
		assertTrue(file.exists());
	}

	@Test
	public void testZipDirectoryServiceFailure() throws Exception
	{
		final AdaptrisMessage msg = new DefectiveMessageFactory().newMessage();
		msg.addMetadata("zip-path", "ivy");
		try
		{
			final ZipDirectoryService zip = new ZipDirectoryService();
			zip.setDirectoryPath("%message{zip-path}");
			execute(zip, msg);
			fail();
		}
		catch (@SuppressWarnings("unused") final ServiceException expected)
		{
			/* expected result */
		}
	}

	@Override
	protected Object retrieveObjectForSampleConfig()
	{
		return new GzipService();
	}

}

@@ -1619,16 +1619,22 @@
 t();%0A%0A%09%09
+final 
 File dir
@@ -1711,16 +1711,38 @@
 age-id)%0A
+%09%09dir.deleteOnExit();%0A
 %09%09assert
@@ -1768,17 +1768,31 @@
 y());%0A%09%09
-d
+final File ivyD
 ir = new
@@ -1824,24 +1824,49 @@
 (), %22ivy%22);%0A
+%09%09ivyDir.deleteOnExit();%0A
 %09%09assertTrue
@@ -1858,33 +1858,36 @@
 );%0A%09%09assertTrue(
-d
+ivyD
 ir.isDirectory()
@@ -1911,17 +1911,20 @@
 ile f : 
-d
+ivyD
 ir.listF
@@ -1938,16 +1938,43 @@
 %0A%09%09%7B%0A%09%09%09
+f.deleteOnExit();%0A%09%09%09final 
 File f2 
@@ -2592,16 +2592,38 @@
 dPath);%0A
+%09%09dir.deleteOnExit();%0A
 %09%09assert
@@ -2695,16 +2695,39 @@
 .xml%22);%0A
+%09%09file.deleteOnExit();%0A
 %09%09assert

Update unit tests