@Override public Pair<String, String> load(String userId) throws Exception { String clientId = prefix + userId; String uuid = null; JsonNode results = requests.list().get("results"); for( JsonNode jsonNode : results ) { JsonNode clientIdNode = jsonNode.get("clientId"); if( clientId.equals(clientIdNode.asText()) ) { uuid = jsonNode.get("uuid").asText(); break; } } if( uuid == null ) { uuid = requests.createClient(clientId, adminPassword, userId, "default"); } return new Pair<String, String>(clientId, uuid); }
@Test(description = "This test case tests handling SQLException when updating profile", dependsOnMethods = {"testUpdateProfileThrowingFeatureManagerDAOException"}, expectedExceptions = IllegalTransactionStateException.class) public void testUpdateProfileThrowingIllegalTransactionStateException() throws Exception { //Retrieving profile object Profile savedProfile = profileManager.getProfile(profile1.getProfileId()); Pair<Connection, Pair<DataSource, DataSource>> pair = mockConnection(); PowerMockito.doThrow(new SQLException()).when(pair.first()).setAutoCommit(anyBoolean()); String newProfileName = "Updated Test Profile"; savedProfile.setProfileName(newProfileName); try { profileManager.updateProfile(savedProfile); } finally { PolicyManagementDAOFactory.init(pair.second().first()); } }
@Test(description = "This test case tests handling SQLException when adding new profile feature", dependsOnMethods = "testAddProfileFeatureThrowingFeatureManagerDAOException", expectedExceptions = IllegalTransactionStateException.class) public void testAddProfileThrowingIllegalTransactionStateException() throws Exception { //Creating profile object Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_D); ProfileFeature profileFeature = profile.getProfileFeaturesList().get(0); Pair<Connection, Pair<DataSource, DataSource>> pair = mockConnection(); PowerMockito.doThrow(new SQLException()).when(pair.first()).setAutoCommit(anyBoolean()); try { featureManager.addProfileFeature(profileFeature, profile.getProfileId()); } finally { PolicyManagementDAOFactory.init(pair.second().first()); } }
@Test(description = "This test case tests handling SQLException when adding new profile feature", dependsOnMethods = "testAddProfileFeaturesThrowingFeatureManagerDAOException", expectedExceptions = IllegalTransactionStateException.class) public void testAddProfileFeaturesThrowingIllegalTransactionStateException() throws Exception { //Creating profile object Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_D); List<ProfileFeature> profileFeaturesList = profile.getProfileFeaturesList(); Pair<Connection, Pair<DataSource, DataSource>> pair = mockConnection(); PowerMockito.doThrow(new SQLException()).when(pair.first()).setAutoCommit(anyBoolean()); try { featureManager.addProfileFeatures(profileFeaturesList, profile.getProfileId()); } finally { PolicyManagementDAOFactory.init(pair.second().first()); } }
@Test(description = "This test case tests handling SQLException when adding new profile feature", dependsOnMethods = "testUpdateProfileFeaturesThrowingFeatureManagerDAOException", expectedExceptions = IllegalTransactionStateException.class) public void testUpdateProfileFeaturesThrowingIllegalTransactionStateException() throws Exception { //Creating profile object Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_D); List<ProfileFeature> profileFeaturesList = profile.getProfileFeaturesList(); Pair<Connection, Pair<DataSource, DataSource>> pair = mockConnection(); PowerMockito.doThrow(new SQLException()).when(pair.first()).setAutoCommit(anyBoolean()); try { featureManager.updateProfileFeatures(profileFeaturesList, profile.getProfileId()); } finally { PolicyManagementDAOFactory.init(pair.second().first()); } }
protected Pair<Connection, Pair<DataSource, DataSource>> mockConnection() throws Exception { //Throwing PolicyManagerDAOException while adding profile DatabaseMetaData databaseMetaData = mock(DatabaseMetaData.class); when(databaseMetaData.getDatabaseProductName()).thenReturn("H2"); Connection conn = mock(Connection.class); when(conn.getMetaData()).thenReturn(databaseMetaData); DataSource dataSource = mock(DataSource.class); when(dataSource.getConnection()).thenReturn(conn); Field dataSourceField = PolicyManagementDAOFactory.class.getDeclaredField("dataSource"); dataSourceField.setAccessible(true); DataSource oldDataSource = (DataSource) dataSourceField.get(null); PolicyManagementDAOFactory.init(dataSource); return new Pair<>(conn, new Pair<>(oldDataSource, dataSource)); }
public void invalidate(String userId) { Pair<String, String> clientInfo = clientCache.getIfPresent(userId); if( clientInfo != null ) { requests.delete(clientInfo.second()); clientCache.invalidate(userId); tokenMap.invalidate(userId); } }
@Test(description = "This test case tests handling PolicyComplianceException when checking policy compliance", dependsOnMethods = "testCheckPolicyComplianceThrowingProfileManagerDAOException", expectedExceptions = PolicyComplianceException.class) public void testAddProfileThrowingPolicyComplianceException() throws Exception { Pair<Connection, Pair<DataSource, DataSource>> pair = mockConnection(); PowerMockito.doAnswer(new Answer<Connection>() { int callCounter = 0; @Override public Connection answer(InvocationOnMock invocationOnMock) throws Throwable { if(callCounter > 0){ Field currentConnectionField = PolicyManagementDAOFactory.class.getDeclaredField("currentConnection"); currentConnectionField.setAccessible(true); ThreadLocal<Connection> threadLocal = new ThreadLocal<>(); threadLocal.set(pair.first()); currentConnectionField.set(null, threadLocal); throw new SQLException(); } callCounter++; return pair.second().first().getConnection(); } }).when(pair.second().second()).getConnection(); DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setType(DEVICE_TYPE_E); deviceIdentifier.setId(String.valueOf(device5.getDeviceIdentifier())); try { monitoringManager.checkPolicyCompliance(deviceIdentifier, new ArrayList<ComplianceFeature>()); } finally { PolicyManagementDAOFactory.init(pair.second().first()); } }
@Test(description = "This test case tests handling SQLException when checking is compliant", dependsOnMethods = "testIsCompliantThrowingMonitoringDAOException", expectedExceptions = IllegalTransactionStateException.class) public void testIsCompliantThrowingIllegalTransactionStateException() throws Exception { Pair<Connection, Pair<DataSource, DataSource>> pair = mockConnection(); PowerMockito.doThrow(new SQLException()).when(pair.second().second()).getConnection(); try { DeviceIdentifier deviceIdentifier = new DeviceIdentifier(); deviceIdentifier.setType(DEVICE_TYPE_E); deviceIdentifier.setId(String.valueOf(device5.getDeviceIdentifier())); monitoringManager.isCompliant(deviceIdentifier); } finally { PolicyManagementDAOFactory.init(pair.second().first()); } }
@Test(description = "This test case tests handling SQLException when adding new profile", dependsOnMethods = "testAddProfileThrowingFeatureManagerDAOException", expectedExceptions = IllegalTransactionStateException.class) public void testAddProfileThrowingIllegalTransactionStateException() throws Exception { //Creating profile object Profile profile = ProfileCreator.getProfile(FeatureCreator.getFeatureList(), DEVICE_TYPE_C); Pair<Connection, Pair<DataSource, DataSource>> pair = mockConnection(); PowerMockito.doThrow(new SQLException()).when(pair.first()).setAutoCommit(anyBoolean()); try { profileManager.addProfile(profile); } finally { PolicyManagementDAOFactory.init(pair.second().first()); } }
@Test(description = "This test case tests handling SQLException when retrieving profile", dependsOnMethods = "testGetProfileThrowingFeatureManagerDAOException", expectedExceptions = IllegalTransactionStateException.class) public void testGetProfileThrowingIllegalTransactionStateException() throws Exception { //Creating profile object Pair<Connection, Pair<DataSource, DataSource>> pair = mockConnection(); PowerMockito.doThrow(new SQLException()).when(pair.second().second()).getConnection(); try { profileManager.getProfile(profile1.getProfileId()); } finally { PolicyManagementDAOFactory.init(pair.second().first()); } }
@Test(description = "This test case tests handling SQLException when retrieving all profiles", dependsOnMethods = "testGetAllProfilesThrowingFeatureManagerDAOException", expectedExceptions = IllegalTransactionStateException.class) public void testGetAllProfilesThrowingIllegalTransactionStateException() throws Exception { //Creating profile object Pair<Connection, Pair<DataSource, DataSource>> pair = mockConnection(); PowerMockito.doThrow(new SQLException()).when(pair.second().second()).getConnection(); try { profileManager.getAllProfiles(); } finally { PolicyManagementDAOFactory.init(pair.second().first()); } }
@Test(description = "This test case tests handling SQLException when retrieving all profiles of a device type", dependsOnMethods = "testGetProfilesOfDeviceTypeThrowingFeatureManagerDAOException", expectedExceptions = IllegalTransactionStateException.class) public void testGetProfilesOfDeviceTypeThrowingIllegalTransactionStateException() throws Exception { //Creating profile object Pair<Connection, Pair<DataSource, DataSource>> pair = mockConnection(); PowerMockito.doThrow(new SQLException()).when(pair.second().second()).getConnection(); try { profileManager.getProfilesOfDeviceType(DEVICE_TYPE_C); } finally { PolicyManagementDAOFactory.init(pair.second().first()); } }
@Test(description = "This test case tests handling SQLException when deleting a profile", dependsOnMethods = "testDeleteProfileThrowingFeatureManagerDAOException", expectedExceptions = IllegalTransactionStateException.class) public void testDeleteProfileThrowingIllegalTransactionStateException() throws Exception { //Creating profile object Pair<Connection, Pair<DataSource, DataSource>> pair = mockConnection(); PowerMockito.doThrow(new SQLException()).when(pair.second().second()).getConnection(); try { profileManager.deleteProfile(profile1); } finally { PolicyManagementDAOFactory.init(pair.second().first()); } }
@Test(description = "This test case tests handling SQLException when all features of a device type", dependsOnMethods = "testGetAllFeatures", expectedExceptions = {IllegalTransactionStateException.class, FeatureManagementException.class}) public void testGetAllFeaturesThrowingIllegalTransactionStateException() throws Exception { Pair<Connection, Pair<DataSource, DataSource>> pair = mockConnection(); PowerMockito.doThrow(new SQLException()).when(pair.second().second()).getConnection(); try { featureManager.getAllFeatures(DEVICE_TYPE_D); } finally { PolicyManagementDAOFactory.init(pair.second().first()); } }
@Test(description = "This test case tests handling SQLException when retrieving features of a profile", dependsOnMethods = "testGetFeaturesForProfileThrowingFeatureManagerDAOException", expectedExceptions = IllegalTransactionStateException.class) public void testGetFeaturesForProfileThrowingIllegalTransactionStateException() throws Exception { Pair<Connection, Pair<DataSource, DataSource>> pair = mockConnection(); PowerMockito.doThrow(new SQLException()).when(pair.second().second()).getConnection(); try { featureManager.getFeaturesForProfile(profile1.getProfileId()); } finally { PolicyManagementDAOFactory.init(pair.second().first()); } }
@Test(description = "This test case tests handling SQLException when deleting features of a profile", dependsOnMethods = "testDeleteFeaturesOfProfileThrowingFeatureManagerDAOException", expectedExceptions = IllegalTransactionStateException.class) public void testDeleteFeaturesOfProfileThrowingIllegalTransactionStateException() throws Exception { Pair<Connection, Pair<DataSource, DataSource>> pair = mockConnection(); PowerMockito.doThrow(new SQLException()).when(pair.second().second()).getConnection(); try { featureManager.deleteFeaturesOfProfile(profile1); } finally { PolicyManagementDAOFactory.init(pair.second().first()); } }
@Test public void testMoveToTrash() throws IOException { TrashTestBase trash = new TrashTestBase(new Properties()); Path pathToDelete = new Path("/path/to/delete"); final List<Pair<Path, Path>> movedPaths = Lists.newArrayList(); when(trash.fs.exists(any(Path.class))).thenReturn(false); when(trash.fs.rename(any(Path.class), any(Path.class))).thenAnswer(new Answer<Boolean>() { @Override public Boolean answer(InvocationOnMock invocation) throws Throwable { Object[] args = invocation.getArguments(); movedPaths.add(new Pair<Path, Path>((Path) args[0], (Path) args[1])); return true; } }); Assert.assertTrue(trash.trash.moveToTrash(pathToDelete)); verify(trash.fs, times(1)).mkdirs(any(Path.class)); Assert.assertEquals(movedPaths.size(), 1); Assert.assertTrue(movedPaths.get(0).first().equals(pathToDelete)); Assert.assertTrue(movedPaths.get(0).second().toString().endsWith(pathToDelete.toString())); Assert.assertTrue(movedPaths.get(0).second().getParent().getParent().getParent().equals(trash.trash.getTrashLocation())); }
@Test public void testMoveToTrashExistingFile() throws IOException { TrashTestBase trash = new TrashTestBase(new Properties()); String fileName = "delete"; Path pathToDelete = new Path("/path/to", fileName); Pattern expectedNamePattern = Pattern.compile("^" + fileName + "_[0-9]+$"); final List<Pair<Path, Path>> movedPaths = Lists.newArrayList(); when(trash.fs.exists(any(Path.class))).thenReturn(true); when(trash.fs.rename(any(Path.class), any(Path.class))).thenAnswer(new Answer<Boolean>() { @Override public Boolean answer(InvocationOnMock invocation) throws Throwable { Object[] args = invocation.getArguments(); movedPaths.add(new Pair<Path, Path>((Path) args[0], (Path) args[1])); return true; } }); Assert.assertTrue(trash.trash.moveToTrash(pathToDelete)); verify(trash.fs, times(0)).mkdirs(any(Path.class)); Assert.assertEquals(movedPaths.size(), 1); Assert.assertTrue(movedPaths.get(0).first().equals(pathToDelete)); Assert.assertTrue(movedPaths.get(0).second().getParent().toString().endsWith(pathToDelete.getParent().toString())); Assert.assertTrue(expectedNamePattern.matcher(movedPaths.get(0).second().getName()).matches()); Assert.assertTrue(movedPaths.get(0).second().getParent().getParent().getParent().equals(trash.trash.getTrashLocation())); }
@Test public void checkTerminalTypingCharsWithoutShift() { loader.waitOnClosed(); terminal.selectTerminalTab(); terminal.waitTerminalConsole(); terminal.waitTerminalIsNotEmpty(); for (Pair<String, String> pair : keyPairs) { terminal.typeIntoTerminal(pair.first()); terminal.waitExpectedTextIntoTerminal("$ " + pair.first()); terminal.typeIntoTerminal(Keys.BACK_SPACE.toString()); } }
@Test public void checkTerminalTypingWithShift() { loader.waitOnClosed(); terminal.selectTerminalTab(); terminal.waitTerminalConsole(); terminal.waitTerminalIsNotEmpty(); for (Pair<String, String> pair : keyPairs) { terminal.typeIntoTerminal(Keys.SHIFT + pair.first()); terminal.waitExpectedTextIntoTerminal("$ " + pair.second()); terminal.typeIntoTerminal(Keys.BACK_SPACE.toString()); } }
@Override public boolean SendData(byte[] data, String destinationId) { synchronized (msg) { msg.add(Pair.create(destinationId, NetIPConverter.parseConcreteMessage(data))); msg.notifyAll(); } return true; }
/** * Finds TestNG methods that the specified TestNG method depends upon * @param m TestNG method * @param methods list of methods to search for depended upon methods * @return list of methods that match the criteria */ protected static ITestNGMethod[] findDependedUponMethods(ITestNGMethod m, ITestNGMethod[] methods) { String canonicalMethodName = calculateMethodCanonicalName(m); List<ITestNGMethod> vResult = Lists.newArrayList(); String regexp = null; for (String fullyQualifiedRegexp : m.getMethodsDependedUpon()) { boolean foundAtLeastAMethod = false; if (null != fullyQualifiedRegexp) { // Escapes $ in regexps as it is not meant for end - line matching, but inner class matches. regexp = fullyQualifiedRegexp.replace("$", "\\$"); boolean usePackage = regexp.indexOf('.') != -1; Pattern pattern = Pattern.compile(regexp); for (ITestNGMethod method : methods) { ConstructorOrMethod thisMethod = method.getConstructorOrMethod(); String thisMethodName = thisMethod.getName(); String methodName = usePackage ? calculateMethodCanonicalName(method) : thisMethodName; Pair<String, String> cacheKey = Pair.create(regexp, methodName); Boolean match = MATCH_CACHE.get(cacheKey); if (match == null) { match = pattern.matcher(methodName).matches(); MATCH_CACHE.put(cacheKey, match); } if (match) { vResult.add(method); foundAtLeastAMethod = true; } } } if (!foundAtLeastAMethod) { if (m.ignoreMissingDependencies()) { continue; } if (m.isAlwaysRun()) { continue; } Method maybeReferringTo = findMethodByName(m, regexp); if (maybeReferringTo != null) { throw new TestNGException(canonicalMethodName + "() is depending on method " + maybeReferringTo + ", which is not annotated with @Test or not included."); } throw new TestNGException(canonicalMethodName + "() depends on nonexistent method " + regexp); } }//end for return vResult.toArray(new ITestNGMethod[vResult.size()]); }