-
Notifications
You must be signed in to change notification settings - Fork 64
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into bugfix/create-request
- Loading branch information
Showing
7 changed files
with
247 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
69 changes: 69 additions & 0 deletions
69
src/main/java/ru/alfabank/tests/core/helpers/BlackList.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
/** | ||
* Copyright 2017 Alfa Laboratory | ||
* | ||
* 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 ru.alfabank.tests.core.helpers; | ||
|
||
import net.lightbody.bmp.proxy.BlacklistEntry; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
public class BlackList { | ||
private List<BlacklistEntry> blacklistEntries = new ArrayList<>(); | ||
|
||
private BlackListManager blackListManager = new BlackListManager(); | ||
|
||
public BlackList() { | ||
setDefaultBlacklistEntries(); | ||
} | ||
|
||
public BlackList(List<BlacklistEntry> newEntries) { | ||
setNewBlacklistEntries(newEntries); | ||
} | ||
|
||
/** | ||
* Список регулярных выражений соответствующих URL, которые добавляются в Blacklist по умолчанию | ||
*/ | ||
private void setDefaultBlacklistEntries() { | ||
blackListManager.fillBlackList(blacklistEntries); | ||
} | ||
|
||
/** | ||
* очищает дефолтный сисок URL из Blacklist и задает новый Blacklist | ||
* @param newEntries - новый список URL для доавления в Blacklist | ||
*/ | ||
private void setNewBlacklistEntries(List<BlacklistEntry> newEntries) { | ||
blacklistEntries.clear(); | ||
blacklistEntries.addAll(newEntries); | ||
} | ||
|
||
/** | ||
* | ||
* @return - спискок URL, которые находятся в Blacklist | ||
*/ | ||
public List<BlacklistEntry> getBlacklistEntries() { | ||
return blacklistEntries; | ||
} | ||
|
||
|
||
/** | ||
* добавляет новые URL к дефолтному Blacklist | ||
* | ||
* @param newEntries - новые URL, которые будут добавлены в Blacklist | ||
*/ | ||
public void addToDefaultBlacklistEntries(List<BlacklistEntry> newEntries) { | ||
blacklistEntries.addAll(newEntries); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
src/main/java/ru/alfabank/tests/core/helpers/BlackListManager.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/** | ||
* Copyright 2017 Alfa Laboratory | ||
* | ||
* 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 ru.alfabank.tests.core.helpers; | ||
|
||
import net.lightbody.bmp.proxy.BlacklistEntry; | ||
|
||
import java.io.File; | ||
import java.io.IOException; | ||
import java.nio.file.Files; | ||
import java.util.List; | ||
import java.util.regex.Matcher; | ||
import java.util.regex.Pattern; | ||
|
||
public class BlackListManager { | ||
/** | ||
* Производится парсинг строки из файла blacklist на наличие ссылок типа: | ||
* .*ru.fp.kaspersky-labs.com.* | ||
* http://google.com/ 200 | ||
* При необходимости можно указывать статус код, по умолчанию будет присвоен 404 | ||
* @param blacklistEntries - список ссылок и статус кодов | ||
*/ | ||
public void fillBlackList(List<BlacklistEntry> blacklistEntries) { | ||
String file = getResource(); | ||
Pattern pattern = Pattern.compile("((https?:\\/\\/)?([\\da-z\\.\\*-]+)\\.([a-z\\.]{2,6})([\\/\\w\\.\\*-]*)*\\/?)\\s?(\\d{3})*"); | ||
Matcher matcher = pattern.matcher(file); | ||
while (matcher.find()) { | ||
if (matcher.group(6) == null) | ||
blacklistEntries.add(new BlacklistEntry(matcher.group(1), 404)); | ||
else blacklistEntries.add(new BlacklistEntry(matcher.group(1), Integer.parseInt(matcher.group(6)))); | ||
} | ||
} | ||
|
||
private String getResource() { | ||
ClassLoader classLoader = getClass().getClassLoader(); | ||
byte[] file = new byte[0]; | ||
try { | ||
file = Files.readAllBytes(new File(classLoader.getResource("blacklist").getFile()).toPath()); | ||
return new String(file, "UTF-8"); | ||
} catch (IOException e) { | ||
System.out.println("Файла 'blacklist' - не существует\n"); | ||
e.printStackTrace(); | ||
} | ||
return new String(file); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/** | ||
* Copyright 2017 Alfa Laboratory | ||
* | ||
* 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 ru.alfabank.core; | ||
|
||
|
||
import net.lightbody.bmp.proxy.BlacklistEntry; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import ru.alfabank.tests.core.helpers.BlackList; | ||
import ru.alfabank.tests.core.helpers.BlackListManager; | ||
|
||
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.List; | ||
|
||
import static org.hamcrest.MatcherAssert.assertThat; | ||
import static org.hamcrest.beans.SamePropertyValuesAs.samePropertyValuesAs; | ||
|
||
public class BlackListTests { | ||
private static List<BlacklistEntry> defaultBlacklistEntries = new ArrayList<>(); | ||
|
||
@Test | ||
public void testGetDefaultBlacklistEntries() { | ||
BlackList blackList = new BlackList(); | ||
assertThat(defaultBlacklistEntries, samePropertyValuesAs(blackList.getBlacklistEntries())); | ||
} | ||
|
||
@Test | ||
public void testAddToDefaultBlacklistEntries() { | ||
List<BlacklistEntry> expectedEntries = new ArrayList<>(); | ||
expectedEntries.addAll(defaultBlacklistEntries); | ||
|
||
List<BlacklistEntry> newEntries = new ArrayList<>(Arrays.asList(new BlacklistEntry("new.entry", 404))); | ||
expectedEntries.addAll(newEntries); | ||
|
||
BlackList blackList = new BlackList(); | ||
blackList.addToDefaultBlacklistEntries(newEntries); | ||
assertThat(expectedEntries, samePropertyValuesAs(blackList.getBlacklistEntries())); | ||
} | ||
|
||
@Test | ||
public void testNewBlacklistEntries() { | ||
List<BlacklistEntry> newEntries = new ArrayList<>(Arrays.asList(new BlacklistEntry("new.entry", 404))); | ||
BlackList blackList = new BlackList(newEntries); | ||
assertThat(newEntries, samePropertyValuesAs(blackList.getBlacklistEntries())); | ||
} | ||
|
||
@Before | ||
public void initBlackList() { | ||
new BlackListManager().fillBlackList(defaultBlacklistEntries); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
.*ru.fp.kaspersky-labs.com.* | ||
.*google-analytics.com.* | ||
.*yastatic.net.* | ||
.*googletagmanager.com.* 200 |