Skip to content

Commit

Permalink
Adding unit tests for SeLion-Grid
Browse files Browse the repository at this point in the history
- Added tests for FileExtractor , ConfigParser , AuthenticationHelper,
  SauceConfigReader, FileDownloader and ArtifactDetails.
- Updated .gitattributes file
  • Loading branch information
Yuvha Secaran authored and Doug Simmons committed Jun 2, 2015
1 parent f5cb5e1 commit ecb8519
Show file tree
Hide file tree
Showing 11 changed files with 302 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,8 @@
*.nib binary
*.strings binary
*.apk binary
# The below type of files are specific to unit tests in the server module
*.zip binary
*.tar.bz2 binary


Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ public static List<String> extractArchive(String archiveFile) {
FileOutputStream decompressStream = new FileOutputStream(outputArchiveName);
IOUtils.copy(is, decompressStream);
is.close();
decompressStream.close();
// The archive is de-compressed. Replacing the compressed file name to the archive name
archiveFile = outputArchiveName;
// Add the file name to the list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,60 @@

package com.paypal.selion.grid;

import static org.testng.Assert.assertTrue;
import static org.testng.Assert.assertNull;

import java.io.File;
import java.io.IOException;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang.SystemUtils;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

import com.paypal.selion.grid.FileDownloader;
import com.paypal.selion.pojos.SeLionGridConstants;

public class FileDownloaderTest {

File downloadedFile = null;
File homeDir = null;

@BeforeClass
public void createHomeDir() throws IOException{
//Re-setting the path -just in case if any other test changes the SeLion_home_dir constant
SeLionGridConstants.SELION_HOME_DIR= SystemUtils.USER_HOME + "/.selion/";
homeDir = new File(SeLionGridConstants.SELION_HOME_DIR+"/downloads/");
homeDir.mkdirs();
}

@Test(expectedExceptions = { UnsupportedOperationException.class })
public void testUnsupportedFileType() {
// gz compression type is not supported.
String unsupportedFileURL = "https://bitbucket.org/ariya/phantomjs/downloads/phantomjs-1.9.7-linux-i686.tar.gz";
FileDownloader.downloadFile(unsupportedFileURL, "123CEFRGxSdfnsfwefla");
}

@Test
public void testFileDownload() {
String testUrl = "https://selenium-release.storage.googleapis.com/2.45/IEDriverServer_Win32_2.45.0.zip";
String tempChecksum = "dde210e04e5c1b0d6019fd8a1199df18";
String result = FileDownloader.downloadFile(testUrl, tempChecksum);
downloadedFile = new File(result);
assertTrue(downloadedFile.exists());
}

@Test
public void testInvalidChecksum() {
String testUrl = "https://chromedriver.storage.googleapis.com/2.14/chromedriver_win32.zip";
String tempChecksum = "dde210e04e5c1b0d6019fd8a1199df18";
String result = FileDownloader.downloadFile(testUrl, tempChecksum);
assertNull(result);
}

@AfterClass(alwaysRun = true)
public void cleanUpFile() {
FileUtils.deleteQuietly(downloadedFile);
FileUtils.deleteQuietly(homeDir);
}
}
87 changes: 87 additions & 0 deletions server/src/test/java/com/paypal/selion/grid/FileExtractorTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
/*-------------------------------------------------------------------------------------------------------------------*\
| Copyright (C) 2015 eBay Software Foundation |
| |
| 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.paypal.selion.grid;

import static org.testng.Assert.assertTrue;

import java.io.File;
import java.util.List;

import org.apache.commons.lang.SystemUtils;
import org.openqa.selenium.Platform;
import org.testng.annotations.AfterMethod;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import com.paypal.selion.pojos.SeLionGridConstants;

public class FileExtractorTest {
File extractedFile = null;
File tarFile = null;

@BeforeClass
public void mockTestPaths() {
SeLionGridConstants.SELION_HOME_DIR = SystemUtils.USER_DIR + "/";
tarFile = new File(SeLionGridConstants.SELION_HOME_DIR
+ "src/test/resources/archives/DummyBz2Archive.tar");
}

@BeforeMethod
public void initExtractedFile(){
extractedFile = new File(SeLionGridConstants.SELION_HOME_DIR + getExtractedFileName());
}

@Test
public void testExtractingZip() {
List<String> files = FileExtractor.extractArchive(SeLionGridConstants.SELION_HOME_DIR
+ "src/test/resources/archives/DummyArchive.zip");
assertTrue(extractedFile.exists());
assertTrue(files.size() == 1);
}

@Test
public void testExtractingBzip2() {
List<String> files = FileExtractor.extractArchive(SeLionGridConstants.SELION_HOME_DIR
+ "src/test/resources/archives/DummyBz2Archive.tar.bz2");
assertTrue(extractedFile.exists());
assertTrue(tarFile.exists());
assertTrue(files.size() == 2);
}

@AfterMethod
public void cleanExtractedFiles(){
extractedFile.delete();
if(tarFile.exists()){
tarFile.delete();
}
}

private String getExtractedFileName() {
String fileName = null;
switch (Platform.getCurrent()) {
case LINUX:
case MAC:
case UNIX:
fileName = "phantomjs";
break;
default:
fileName = "phantomjs.exe";
break;
}
return fileName;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package com.paypal.selion.pojos;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.List;

import org.testng.annotations.Test;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;

import com.paypal.selion.pojos.ArtifactDetails.URLChecksumEntity;

public class ArtifactDetailsTest {
@Test
public void testGetArtifactDetails() throws FileNotFoundException{
File fileToTest = new File("src/test/resources/config/Dummydownload.json");
List<URLChecksumEntity> parsedDetails = ArtifactDetails.getArtifactDetailsForCurrentPlatform(fileToTest);
assertTrue(parsedDetails.size()==2);
URLChecksumEntity entity = parsedDetails.get(0);
//Asserting for details read using any key
assertEquals(entity.getUrl().getValue(),"seleniumURL");
assertEquals(entity.getChecksum().getValue(),"seleniumChecksum");
//Asserting for details read for a platform
entity =parsedDetails.get(1);
assertNotNull(entity.getUrl().getValue());
assertNotNull(entity.getChecksum().getValue());

}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*-------------------------------------------------------------------------------------------------------------------*\
| Copyright (C) 2015 eBay Software Foundation |
| |
| 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.paypal.selion.utils;

import java.io.File;

import org.apache.commons.lang.SystemUtils;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;
import static org.testng.Assert.assertTrue;
import static org.testng.Assert.assertFalse;

import com.paypal.selion.pojos.SeLionGridConstants;

public class AuthenticationHelperTest {

@BeforeClass
public void mockTestPath() {
SeLionGridConstants.SELION_HOME_DIR = SystemUtils.USER_DIR + "/src/test/resources/";
}

@Test
public void testAuthentication() {
boolean isValidLogin = AuthenticationHelper.authenticate("admin", "admin");
assertTrue(isValidLogin);
}

@Test(dependsOnMethods = "testAuthentication")
public void testPasswordChange() {
boolean isPasswordChanged = AuthenticationHelper.changePassword("admin", "dummy");
assertTrue(isPasswordChanged);
}

@Test(dependsOnMethods = "testPasswordChange")
public void authenticateNewPassword() {
boolean val = AuthenticationHelper.authenticate("admin", "dummy");
assertTrue(val);
}

@Test(dependsOnMethods = "authenticateNewPassword")
public void authenticateWrongPassword() {
boolean isValidPassword = AuthenticationHelper.authenticate("admin", "dummy123");
assertFalse(isValidPassword);
}

@Test(dependsOnMethods = "authenticateWrongPassword")
public void authenticateWrongUsername() {
boolean isValidUser = AuthenticationHelper.authenticate("dummy", "dummy");
assertFalse(isValidUser);
}

@AfterClass(alwaysRun=true)
public void cleanUpAuthFile() {
File authFile = new File(SeLionGridConstants.SELION_HOME_DIR + ".authFile");
authFile.delete();
}
}
36 changes: 36 additions & 0 deletions server/src/test/java/com/paypal/selion/utils/ConfigParserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*-------------------------------------------------------------------------------------------------------------------*\
| Copyright (C) 2015 eBay Software Foundation |
| |
| 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.paypal.selion.utils;

import static org.testng.Assert.assertEquals;

import org.testng.annotations.Test;

public class ConfigParserTest {

@Test
public void testSetConfig() {
// Mock the location of the config file
ConfigParser.setConfigFile("src/test/resources/config/DummySeLionConfig.json");
ConfigParser parser = ConfigParser.getInstance();
int key1 = parser.getInt("Key1");
String key2 = parser.getString("Key2");
long key3 = parser.getLong("Key3");
assertEquals(1000, key1);
assertEquals("Sample", key2);
assertEquals(250000000, key3);
}
}
Binary file added server/src/test/resources/archives/DummyArchive.zip
Binary file not shown.
Binary file not shown.
5 changes: 5 additions & 0 deletions server/src/test/resources/config/DummySeLionConfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"Key1": 1000,
"Key2": "Sample",
"Key3": 250000000
}
24 changes: 24 additions & 0 deletions server/src/test/resources/config/Dummydownload.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
[
{
"name": "selenium",
"any": {
"url": "seleniumURL",
"checksum": "seleniumChecksum"
}
},
{
"name": "chrome",
"windows": {
"url": "chromeURL",
"checksum": "chromeCheckSum"
},
"linux": {
"url": "chromeURL_linux",
"checksum": "chromeCheckSum_linux"
},
"mac": {
"url": "chromeURL_mac",
"checksum": "chromeCheckSum_mac"
}
}
]

0 comments on commit ecb8519

Please sign in to comment.