FazBrowse GitHub Viewer | Trending |
URL:
| Home
Tools: [Download Repo ZIP]   [Original HTTPS Page]

Added unit tests by willedwards · Pull Request #1 · hmkcode/Java · GitHub

Repository navigation

Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension .java  (3) .json  (4) .md  (1) .xml  (1) dotfile  (2) All 5 file types selected
Only manifest files
Viewed files
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Unified
Split
Hide whitespace
Diff view
Unified
Split
Hide whitespace
3 changes: 3 additions & 0 deletions gson-json-java/.gitignore
This file contains hidden or 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,3 @@
target
*.iml
.idea
3 changes: 3 additions & 0 deletions java-servlet-json/.gitignore
This file contains hidden or 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,3 @@
target
*.iml
.idea
1 change: 1 addition & 0 deletions java-servlet-json/README.md
This file contains hidden or 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 @@
http://hmkcode.com/java-servlet-send-receive-json-using-jquery-ajax/
1 change: 1 addition & 0 deletions java-servlet-json/modified.json
This file contains hidden or 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 @@
[{"title":"bbc","url":"news.bbc.co.uk","categories":["news"],"tags":["uk news"]},{"title":null,"url":"www.google.co.uk","categories":["youtube","gmail","gdrive"],"tags":["search"]}]
12 changes: 12 additions & 0 deletions java-servlet-json/pom.xml
This file contains hidden or 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
Expand Up @@ -35,6 +35,18 @@
<artifactId>jackson-annotations</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>4.9</version>
<exclusions>
<exclusion>
<artifactId>junit</artifactId>
<groupId>junit</groupId>
</exclusion>
</exclusions>
</dependency>

</dependencies>

<build>
Expand Down
This file contains hidden or 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
@@ -1,48 +1,48 @@
package com.hmkcode;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hmkcode.vo.Article;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hmkcode.vo.Article;

public class JSONServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

// This will store all received articles
List<Article> articles = new LinkedList<Article>();

/***************************************************
* URL: /jsonservlet
* doPost(): receives JSON data, parse it, map it and send back as JSON
****************************************************/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{

// 1. get received JSON data from request
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
String json = "";
if(br != null){
json = br.readLine();
}

// 2. initiate jackson mapper
ObjectMapper mapper = new ObjectMapper();

// 3. Convert received JSON to Article
Article article = mapper.readValue(json, Article.class);

// 4. Set response type to JSON
response.setContentType("application/json");
response.setContentType("application/json");

// 5. Add article to List<Article>
articles.add(article);

Expand Down
This file contains hidden or 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
Expand Up @@ -5,35 +5,10 @@

public class Article {

private String title;
private String url;
private List<String> categories;
private List<String> tags;

public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public List<String> getCategories() {
return categories;
}
public void setCategories(List<String> categories) {
this.categories = categories;
}
public List<String> getTags() {
return tags;
}
public void setTags(List<String> tags) {
this.tags = tags;
}
public String title;
public String url;
public List<String> categories;
public List<String> tags;

public void addCategory(String category){
if(this.categories == null)
Expand Down
This file contains hidden or 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,86 @@
package com.hmkcode;/**
* Author: wge
* Date: 22/11/2013
* Time: 18:09
*/

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hmkcode.vo.Article;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


public class ArticleMapperTest
{
private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(ArticleMapperTest.class.getName());

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality



@Test
public void testValidJson() throws Exception

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

{
String json = readlineFromResourceFile("/valid.json");

ObjectMapper mapper = new ObjectMapper();
Article article = mapper.readValue(json, Article.class);

assert(article.url.equals("news.bbc.co.uk"));

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

assert(article.categories.contains("news"));
assert(article.categories.size() == 1);
assert(article.tags.size() == 1);
assert(article.tags.contains("uk news"));

List<Article> articles = new ArrayList<Article>();
articles.add(article);

Article extra = new Article();
extra.url = "www.google.co.uk";
List categoryList = new ArrayList<String>();
categoryList.add("youtube");
categoryList.add("gmail");
categoryList.add("gdrive");

extra.categories = categoryList;
extra.addTag("search");

articles.add(extra);

//this will appear in the root of your project.
mapper.writeValue(new File("modified.json"), articles);
}

@Test (expected=com.fasterxml.jackson.core.JsonParseException.class)
public void testBadInput() throws Exception
{
String json = readlineFromResourceFile("/malformed.json");
ObjectMapper mapper = new ObjectMapper();
Article article = mapper.readValue(json, Article.class);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

}

@Test
public void missingUrl() throws Exception

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

{
String json = readlineFromResourceFile("/missingUrl.json");
ObjectMapper mapper = new ObjectMapper();
Article article = mapper.readValue(json, Article.class);
assert(article.url == null);
assert(article.categories.contains("news"));
assert(article.categories.size() == 1);
assert(article.tags.size() == 1);
assert(article.tags.contains("uk news"));


}

String readlineFromResourceFile(String filename) throws IOException

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Choose a reason Spam Abuse Off Topic Outdated Duplicate Resolved Low Quality

{
InputStream is = getClass().getResourceAsStream(filename);
byte[] buff = new byte[1000];
is.read(buff);
return new String(buff);
}
}
This file contains hidden or 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 @@
{"title":"bbc","url":news.bbc.co.uk,"categories":["news"],"tags":["uk news"]};
This file contains hidden or 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 @@
{"title":"bbc","categories":["news"],"tags":["uk news"]};
1 change: 1 addition & 0 deletions java-servlet-json/src/test/resources/valid.json
This file contains hidden or 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 @@
{"title":"bbc","url":"news.bbc.co.uk","categories":["news"],"tags":["uk news"]};

Back | FazBrowse Home | New Git URL