@Test
public void findContentIdsByTag (){
final String tag = "Outlook";
Connection connection = null;
Statement statement = null;
ResultSet resultSet = null;
String url = "jdbc:mysql://localhost:4444/nsh";
String name = "root";
String pass = "root";
String query = String.format("SELECT content_id FROM tags WHERE tags.tag = '%s'", tag);
try {
connection = DriverManager.getConnection(url, name, pass);
statement = connection.createStatement();
resultSet = statement.executeQuery(query);
List<Integer> list = new ArrayList<>();
while (resultSet.next()){
list.add(resultSet.getInt("content_id"));
}
Assertions.assertNotEquals(0, list.size(), "The size of list is 0!");
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
try {
resultSet.close();
statement.close();
connection.close();
} catch (SQLException e) {
throw new RuntimeException(e);
}
}
}
/**
* The method fetches all content from the database and then filters it by a specific tag. If the content contains the required tag, it is stored in a separate array.
* The method is terrible and with a large database it will be too expensive both in time and in terms of server computing power.
* We need to find another way to solve this problem.
* @param tag name by which the search for the desired content will be performed.
* @return List<ContentDB>
*/
public List<ContentDB> findAllByTag (final String tag){
List<ContentDB>contentDBList = new ArrayList<>();
Iterable<ContentDB> contentDBS = contentRepository.findAll();
contentDBS.forEach(contentDB -> {
Set<Tags> tagsSet = contentDB.getTagsSet();
tagsSet.stream().forEach(tags -> {
if(tags.getTag().equals(tag))
contentDBList.add(contentDB);
});
});
return contentDBList;
}
/**
* Base Net class for download data from internet
*/
public class Net implements LoadingDataFromNet {
private final String log_Net = "log_Net";
private LoaderFromNet loaderFromNet;
private String url;
private Loading loading;
/**
* Public constructor sets main parameters for downloading json string
*
* @param loaderFromNet - Interface
* @param url - website's address
*/
public Net(LoaderFromNet loaderFromNet, String url) {
this.loaderFromNet = loaderFromNet;
this.url = url;
execute();
}
private void execute() {
if (loaderFromNet == null)
throw new NullPointerException("Loader interface is null!");
if (url == null)
throw new NullPointerException("URL is null!");
loading = new Loading(this, url);
}
@Override
public void loadingDataFromNet(String object) {
loaderFromNet.loadFromNetDone(object);
}
}
public class Loading implements Runnable {
private LoadingDataFromNet loadingDataFromNet;
private String url;
protected Loading(LoadingDataFromNet loadingDataFromNet, String url) {
this.loadingDataFromNet = loadingDataFromNet;
this.url = url;
execute();
}
private void execute() {
new Thread(this).start();
}
@Override
public void run() {
try {
URL mURL = new URL(url);
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) mURL.openConnection();
httpsURLConnection.setRequestMethod("GET");
httpsURLConnection.connect();
int responseCode = httpsURLConnection.getResponseCode();
httpsURLConnection.disconnect();
if (responseCode == 200){
InputStream inputStream = mURL.openStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
int letter = -1;
StringBuilder stringBuilder = new StringBuilder();
while ((letter = bufferedReader.read()) != -1)
stringBuilder.append((char) letter);
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
loadingDataFromNet.loadingDataFromNet(stringBuilder.toString());
} else {
loadingDataFromNet.loadingDataFromNet(null);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
public interface LoaderFromNet {
public void loadFromNetDone (final String object);
}
public interface LoadingDataFromNet {
public void loadingDataFromNet (final String object);
}
/**
* Base Net class for download data from internet
*/
public class Net implements LoadingDataFromNet {
private final String log_Net = "log_Net";
private LoaderFromNet loaderFromNet;
private String url;
/**
* Public constructor sets main parameters for downloading json string
*
* @param loaderFromNet - Interface
* @param url - website's address
*/
public Net(LoaderFromNet loaderFromNet, String url) {
this.loaderFromNet = loaderFromNet;
this.url = url;
execute();
}
private void execute() {
if (loaderFromNet == null)
throw new NullPointerException("Loader interface is null!");
if (url == null)
throw new NullPointerException("URL is null!");
new Loading(this, url);
}
@Override
public void loadingDataFromNet(String object) {
loaderFromNet.loadFromNetDone(object);
}
}
public class Loading implements Runnable {
private LoadingDataFromNet loadingDataFromNet;
private String url;
protected Loading(LoadingDataFromNet loadingDataFromNet, String url) {
this.loadingDataFromNet = loadingDataFromNet;
this.url = url;
execute();
}
private void execute() {
new Thread(this).start();
}
@Override
public void run() {
try {
HttpsURLConnection httpsURLConnection = (HttpsURLConnection) new URL(url).openConnection();
httpsURLConnection.setRequestMethod("GET");
httpsURLConnection.connect();
int responseCode = httpsURLConnection.getResponseCode();
httpsURLConnection.disconnect();
if (responseCode == 200){
InputStream inputStream = new URL(url).openStream();
InputStreamReader inputStreamReader = new InputStreamReader(inputStream, StandardCharsets.UTF_8);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
int letter = -1;
StringBuilder stringBuilder = new StringBuilder();
while ((letter = bufferedReader.read()) != -1)
stringBuilder.append((char) letter);
bufferedReader.close();
inputStreamReader.close();
inputStream.close();
loadingDataFromNet.loadingDataFromNet(stringBuilder.toString());
} else {
loadingDataFromNet.loadingDataFromNet(null);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
@Test
public void justTest() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
System.out.println("HEHEHEHEHEHEHEH");
}
});
thread.start();
}
public class UsersTest {
private final String jsonArrayString = "[{\n" +
" \"login\": \"mojombo\",\n" +
" \"id\": 1,\n" +
" \"node_id\": \"MDQ6VXNlcjE=\",\n" +
" \"avatar_url\": \"https://avatars.githubusercontent.com/u/1?v=4\",\n" +
" \"gravatar_id\": \"\",\n" +
" \"url\": \"https://api.github.com/users/mojombo\",\n" +
" \"html_url\": \"https://github.com/mojombo\",\n" +
" \"followers_url\": \"https://api.github.com/users/mojombo/followers\",\n" +
" \"following_url\": \"https://api.github.com/users/mojombo/following{/other_user}\",\n" +
" \"gists_url\": \"https://api.github.com/users/mojombo/gists{/gist_id}\",\n" +
" \"starred_url\": \"https://api.github.com/users/mojombo/starred{/owner}{/repo}\",\n" +
" \"subscriptions_url\": \"https://api.github.com/users/mojombo/subscriptions\",\n" +
" \"organizations_url\": \"https://api.github.com/users/mojombo/orgs\",\n" +
" \"repos_url\": \"https://api.github.com/users/mojombo/repos\",\n" +
" \"events_url\": \"https://api.github.com/users/mojombo/events{/privacy}\",\n" +
" \"received_events_url\": \"https://api.github.com/users/mojombo/received_events\",\n" +
" \"type\": \"User\",\n" +
" \"site_admin\": false\n" +
" },\n" +
" {\n" +
" \"login\": \"defunkt\",\n" +
" \"id\": 2,\n" +
" \"node_id\": \"MDQ6VXNlcjI=\",\n" +
" \"avatar_url\": \"https://avatars.githubusercontent.com/u/2?v=4\",\n" +
" \"gravatar_id\": \"\",\n" +
" \"url\": \"https://api.github.com/users/defunkt\",\n" +
" \"html_url\": \"https://github.com/defunkt\",\n" +
" \"followers_url\": \"https://api.github.com/users/defunkt/followers\",\n" +
" \"following_url\": \"https://api.github.com/users/defunkt/following{/other_user}\",\n" +
" \"gists_url\": \"https://api.github.com/users/defunkt/gists{/gist_id}\",\n" +
" \"starred_url\": \"https://api.github.com/users/defunkt/starred{/owner}{/repo}\",\n" +
" \"subscriptions_url\": \"https://api.github.com/users/defunkt/subscriptions\",\n" +
" \"organizations_url\": \"https://api.github.com/users/defunkt/orgs\",\n" +
" \"repos_url\": \"https://api.github.com/users/defunkt/repos\",\n" +
" \"events_url\": \"https://api.github.com/users/defunkt/events{/privacy}\",\n" +
" \"received_events_url\": \"https://api.github.com/users/defunkt/received_events\",\n" +
" \"type\": \"User\",\n" +
" \"site_admin\": false\n" +
" }\n" +
"]";
@Test
public void parsingJSONArrayTest() throws JSONException{
JSONArray jsonArray = new JSONArray(jsonArrayString);
//Get first object from array
JSONObject jsonObject = jsonArray.getJSONObject(0);
String login = jsonObject.getString("login");
int id = jsonObject.getInt("id");
System.out.println("login: " + login + ", id: " + id);
}
}
Method getInt in org.json.JSONObject not mocked. See g.co/androidstudio/not-mocked for details.
java.lang.RuntimeException: Method getInt in org.json.JSONObject not mocked. See g.co/androidstudio/not-mocked for details.
at org.json.JSONObject.getInt(JSONObject.java)
Например, пользователь вводит только числа, тогда идеально подойдет регулярное выражение
Или пользователь вводит одно слово или предложение:
Вариантов использования регулярных выражений достаточно широкая.