diff --git a/.gitignore b/.gitignore index 033f8f9..6db5371 100644 --- a/.gitignore +++ b/.gitignore @@ -7,4 +7,5 @@ # Java *.war *.jar -*.class \ No newline at end of file +*.class +Shevtsov/src/main/webapp/res/themes/manuscript/index.html \ No newline at end of file diff --git a/Shevtsov/.gitignore b/Shevtsov/.gitignore new file mode 100644 index 0000000..64325e2 --- /dev/null +++ b/Shevtsov/.gitignore @@ -0,0 +1,2 @@ +*/taget/ +*/classes/ diff --git a/Shevtsov/BlogSources/pom.xml b/Shevtsov/BlogSources/pom.xml new file mode 100644 index 0000000..1e982c4 --- /dev/null +++ b/Shevtsov/BlogSources/pom.xml @@ -0,0 +1,80 @@ + + + 4.0.0 + + com.example + blogsources + 0.0.1 + war + + demo + web blog demo + + + UTF-8 + 1.7 + 4.1.6.RELEASE + + + + com.example.blogsources + URLMapping + 0.0.1 + jar + + + org.apache.tomcat.embed + tomcat-embed-jasper + 8.0.23 + provided + + + javax.servlet + jstl + 1.2 + + + + org.springframework + spring-core + ${spring.version} + + + + org.springframework + spring-web + ${spring.version} + + + + org.springframework + spring-webmvc + ${spring.version} + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + ${java.version} + ${java.version} + + + + org.apache.tomcat.maven + tomcat7-maven-plugin + 2.2 + + http://localhost:8080/manager/text + TomcatServer + /demo + + + + + diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/AbstractPost.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/AbstractPost.java new file mode 100644 index 0000000..078c1f4 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/AbstractPost.java @@ -0,0 +1,115 @@ +package com.example.blogsources.Post; + +import java.io.PrintStream; +import java.util.Date; + +public abstract class AbstractPost implements Post { + private int id; + private int userId; + private String title; + private Date creationDate; + private Date updateDate; + protected String type = null; + protected String postContent = null; + + public AbstractPost() { + } + + public AbstractPost(int id, String title, int userId) { + this.id = id; + this.userId = userId; + this.title = title; + creationDate = new Date(); + updateDate = creationDate; + setPostType(); + } + + @Override + public int getPostId() { + return id; + } + + @Override + public Date getPostCreationDate() { + return creationDate; + } + + @Override + public Date getPostLastUpdateDate() { + return updateDate; + } + + public String getPostTitle() { + return title; + } + + @Override + public void setPostLastUpdateDate(Date lastUpdateDate) { + updateDate = lastUpdateDate; + } + + @Override + public String getPostType() { + return type; + } + + protected abstract void setPostType(); + protected void setPostContant(String postContant) { + this.postContent = postContant; + } + + @Override + public abstract void displayPost(PrintStream out); + + public int getId() { + return id; + } + + public void setId(int id) { + this.id = id; + } + + public int getUserId() { + return userId; + } + + public void setUserId(int userId) { + this.userId = userId; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public Date getCreationDate() { + return creationDate; + } + + public void setCreationDate(Date creationDate) { + this.creationDate = creationDate; + } + + public Date getUpdateDate() { + return updateDate; + } + + public void setUpdateDate(Date updateDate) { + this.updateDate = updateDate; + } + + public String getType() { + return type; + } + + public void setType(String type) { + this.type = type; + } + + public String getPostContent() { + return postContent; + } +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/Exceptions/IllegalPageTypeException.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/Exceptions/IllegalPageTypeException.java new file mode 100644 index 0000000..7a3b145 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/Exceptions/IllegalPageTypeException.java @@ -0,0 +1,8 @@ +package com.example.blogsources.Post.Exceptions; + +public class IllegalPageTypeException extends PageExeptions { + + public IllegalPageTypeException(String s) { + super(s); + } +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/Exceptions/NotExistsPageException.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/Exceptions/NotExistsPageException.java new file mode 100644 index 0000000..68e8d4f --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/Exceptions/NotExistsPageException.java @@ -0,0 +1,8 @@ +package com.example.blogsources.Post.Exceptions; + +public class NotExistsPageException extends PageExeptions { + + public NotExistsPageException(String s) { + super(s); + } +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/Exceptions/PageExeptions.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/Exceptions/PageExeptions.java new file mode 100644 index 0000000..f8d287d --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/Exceptions/PageExeptions.java @@ -0,0 +1,5 @@ +package com.example.blogsources.Post.Exceptions; + +public class PageExeptions extends RuntimeException{ + PageExeptions (String s) {super(s);} +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/ImagePost.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/ImagePost.java new file mode 100644 index 0000000..90aaeae --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/ImagePost.java @@ -0,0 +1,19 @@ +package com.example.blogsources.Post; + +import java.io.PrintStream; + +public class ImagePost extends AbstractPost { + + public ImagePost(int id, String content, int userId) { + super(id,content,userId); + } + + @Override + protected void setPostType() { + type = "image"; + } + + @Override + public void displayPost(PrintStream out) {out.println("There is I have to show a image - " + super.getPostTitle()); + } +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/Post.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/Post.java new file mode 100644 index 0000000..6b307c6 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/Post.java @@ -0,0 +1,15 @@ +package com.example.blogsources.Post; + +import java.io.PrintStream; +import java.util.Date; + +public interface Post { + int getPostId(); + String getPostTitle(); + String getPostContent(); + String getPostType(); + void displayPost(PrintStream out); + Date getPostCreationDate(); + Date getPostLastUpdateDate(); + void setPostLastUpdateDate(Date lastUpdateDate); +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/RegularPost.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/RegularPost.java new file mode 100644 index 0000000..5ccb8c3 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/RegularPost.java @@ -0,0 +1,29 @@ +package com.example.blogsources.Post; + +import java.io.PrintStream; + +public class RegularPost extends AbstractPost { + + public RegularPost(int id, String content, int userId) { + super(id,content,userId); + } + + public RegularPost() { + super(); + setPostType(); + } + + @Override + protected void setPostType() { + type = "regular"; + } + + @Override + public void displayPost(PrintStream out) { + System.out.println("The Regular page was created" + super.getPostCreationDate() + "Content:S"); + out.println(super.getPostTitle() + "\n" ); + } + public void setPostContent (String content){ + postContent = content; + } +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/URLMap.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/URLMap.java new file mode 100644 index 0000000..6750592 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/URLMap.java @@ -0,0 +1,21 @@ +package com.example.blogsources.Post; + +public class URLMap { + public static final String PROTO = "http://"; + public static final String HOST = "localhost"; + public static final String PORT = ":8080/"; + public static final String CONTEXT = "demo"; + public static final String FULL_URL = PROTO + HOST + PORT + CONTEXT; + public static final String LOGIN = "/login"; + public static final String FULL_LOGIN = FULL_URL + LOGIN; + public static final String LOGOUT = "/logout"; + public static final String FULL_LOGOUT = FULL_URL + LOGOUT; + public static final String REGISTER = "/register"; + public static final String FULL_REGISTER = FULL_URL + REGISTER; + public static final String ADDPOST = "/addPost"; + public static final String FULL_ADDPOST = FULL_URL + ADDPOST; + public static final String WELCOME = "/welcome"; + public static final String FULL_WELCOME = FULL_URL + WELCOME; + public static final String ABOUT = "/about"; + public static final String FULL_ABOUT = FULL_URL + ABOUT; +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/URLPost.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/URLPost.java new file mode 100644 index 0000000..c1b5438 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Post/URLPost.java @@ -0,0 +1,20 @@ +package com.example.blogsources.Post; + +import java.io.PrintStream; + +public class URLPost extends AbstractPost { + + public URLPost(int id, String content, int userId) { + super(id,content,userId); + } + + @Override + protected void setPostType() { + type = "url"; + } + + @Override + public void displayPost(PrintStream out) { + out.println("There is I have to get and show data from the extended URL - " + super.getPostTitle()); + } +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/BasicUser.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/BasicUser.java new file mode 100644 index 0000000..9633e15 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/BasicUser.java @@ -0,0 +1,82 @@ +package com.example.blogsources.Users; + +import com.example.blogsources.utils.Encyption; + +import java.io.Serializable; +import java.io.UnsupportedEncodingException; +import java.security.NoSuchAlgorithmException; + +public class BasicUser implements User , Serializable { + private int userId; + private String userName; + private String password; + private String email; + private int userPrivilegeLevel; + // 0 - root privilege, + // 1 - user that add comments + // 2+ - like a guest + // 100 - a guest + + public BasicUser() { + this.userId = -1; + this.userName = ""; + this.userPrivilegeLevel = 2; + } + + public BasicUser(int id, String userName,int userPrivilegeLevel){ + this.userId = id; + this.userName = userName; + this.userPrivilegeLevel = userPrivilegeLevel; + } + + public BasicUser(String userName,int userPrivilegeLevel){ + this.userId = -1; + this.userName = userName; + this.userPrivilegeLevel = userPrivilegeLevel; + } + @Override + public int getUserId() { + return userId; + } + + @Override + public String getUserName() { + return userName; + } + + public void setUserId(int userId) { + this.userId = userId; + } + + @Override + public void setUserName(String userName) { + this.userName = userName; + } + + public void setUserPrivilegeLevel(int userPrivilegeLevel) { + this.userPrivilegeLevel = userPrivilegeLevel; + } + + @Override + public int getUserPrivilegeLevel() { + return userPrivilegeLevel; + } + + public String getPassword() { + return password; + } + + public void setPassword(String password) throws UnsupportedEncodingException, NoSuchAlgorithmException { + this.password = Encyption.ecrypt(password); + } + + @Override + public String getEmail() { + return email; + } + + @Override + public void setEmail(String email) { + this.email = email; + } +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/Exceptions/IllegalPrivilegeLevelException.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/Exceptions/IllegalPrivilegeLevelException.java new file mode 100644 index 0000000..f7d795e --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/Exceptions/IllegalPrivilegeLevelException.java @@ -0,0 +1,8 @@ +package com.example.blogsources.Users.Exceptions; + +public class IllegalPrivilegeLevelException extends UserExeptions { + + public IllegalPrivilegeLevelException(String s) { + super(s); + } +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/Exceptions/NotExistUserException.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/Exceptions/NotExistUserException.java new file mode 100644 index 0000000..8a8c306 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/Exceptions/NotExistUserException.java @@ -0,0 +1,8 @@ +package com.example.blogsources.Users.Exceptions; + +public class NotExistUserException extends UserExeptions { + + public NotExistUserException(String s) { + super(s); + } +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/Exceptions/UserAlreadyExistsException.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/Exceptions/UserAlreadyExistsException.java new file mode 100644 index 0000000..34028ec --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/Exceptions/UserAlreadyExistsException.java @@ -0,0 +1,8 @@ +package com.example.blogsources.Users.Exceptions; + +public class UserAlreadyExistsException extends UserExeptions { + + public UserAlreadyExistsException(String s) { + super(s); + } +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/Exceptions/UserExeptions.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/Exceptions/UserExeptions.java new file mode 100644 index 0000000..c4f83b4 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/Exceptions/UserExeptions.java @@ -0,0 +1,5 @@ +package com.example.blogsources.Users.Exceptions; + +public class UserExeptions extends RuntimeException{ + UserExeptions(String s) {super(s);} +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/User.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/User.java new file mode 100644 index 0000000..00e2d6f --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/User.java @@ -0,0 +1,20 @@ +package com.example.blogsources.Users; + +import java.io.UnsupportedEncodingException; +import java.security.NoSuchAlgorithmException; + +public interface User { + int getUserId(); + void setUserId(int userId); + String getUserName(); + void setUserName(String userName); + String getPassword(); + void setPassword(String password) throws UnsupportedEncodingException, NoSuchAlgorithmException; + String getEmail(); + void setEmail(String email); + // 0 - root privilege, + // 1 - user that add comments + // 3 - a guest + int getUserPrivilegeLevel(); + void setUserPrivilegeLevel(int userPrivilegeLevel); +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/UserStorage.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/UserStorage.java new file mode 100644 index 0000000..824ab4e --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/UserStorage.java @@ -0,0 +1,16 @@ +package com.example.blogsources.Users; + +import java.io.PrintStream; + +public interface UserStorage { + public void addNewUser (User user); + public void removeUser (int id); + public void removeUser (String name); + public void showAllUser (PrintStream output); + public int getUserId(String name); + public String getUserName(int id); + public int getUserPrivilegeLevel(String name); + public boolean userIdExists(int id); + public boolean userNameExists(String name); + public boolean validateUser(User user); +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/UsersInRam.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/UsersInRam.java new file mode 100644 index 0000000..a772002 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/Users/UsersInRam.java @@ -0,0 +1,106 @@ +package com.example.blogsources.Users; + +import com.example.blogsources.Users.Exceptions.IllegalPrivilegeLevelException; +import com.example.blogsources.Users.Exceptions.NotExistUserException; +import com.example.blogsources.Users.Exceptions.UserAlreadyExistsException; + +import java.io.PrintStream; +import java.util.HashMap; +import java.util.Map; + +public class UsersInRam implements UserStorage { + private Map users = new HashMap<>(); + private int lastUserId=0; + + public void addNewUser (User user) { + if (userNameExists(user.getUserName())){ + throw new UserAlreadyExistsException("The user name already existed: " + user.getUserName()); + } + + if (user.getUserPrivilegeLevel() >=0 && user.getUserPrivilegeLevel() <= 3) { + user.setUserId(lastUserId + 1); + users.put(user.getUserName(), user); + lastUserId++; + }else { + throw new IllegalPrivilegeLevelException("Invalid userPrivilegeLevel: " + user.getUserPrivilegeLevel() + "Expected values 0...3"); + } + } + + @Override + public void removeUser(int id) { + for (Map.Entry entry: users.entrySet()){ + if (entry.getValue().getUserId() == id) + { + removeUser(entry.getKey()); + break; + } + } + throw new NotExistUserException("User with id: " + id + " wasn't found"); + } + + @Override + public void removeUser(String name) { + users.remove(name); + } + + @Override + public void showAllUser(PrintStream output) { + output.println("#Id #UserName #UserPrivilegeLevel"); + for (String key: users.keySet()) { + output.println(users.get(key).getUserId() + " " + users.get(key).getUserName() + " " + users.get(key).getUserPrivilegeLevel()); + } + } + + + @Override + public int getUserId(String name) { + return users.get(name).getUserId(); + } + + public User getUserByName(String name) { + return users.get(name); + } + + @Override + public String getUserName(int id) { + for (Map.Entry entry: users.entrySet()){ + if (entry.getValue().getUserId() == id) + { + return entry.getValue().getUserName(); + } + } + throw new NotExistUserException("User with id: " + id + " wasn't found"); + } + + public int getUserPrivilegeLevel(String name) { + return users.get(name).getUserPrivilegeLevel(); + } + + @Override + public boolean userIdExists(int id) { + boolean ret = false; + for (Map.Entry entry: users.entrySet()){ + if (entry.getValue().getUserId() == id) + { + ret = true; + break; + } + } + return ret; + } + + @Override + public boolean userNameExists(String name) { + return (users.containsKey(name)); + } + + @Override + public boolean validateUser(User user) { + if (userNameExists(user.getUserName()) ){ + User userInStorage = getUserByName(user.getUserName()); + return userInStorage.getPassword().equals(user.getPassword()); + }else{ + return false; + } + } +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/controllers/WebController.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/controllers/WebController.java new file mode 100644 index 0000000..096f6d3 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/controllers/WebController.java @@ -0,0 +1,221 @@ +/* + * Copyright 2012-2014 the original author or authors. + * + * 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.example.blogsources.controllers; + +import java.util.List; + +import com.example.blogsources.Post.Post; +import com.example.blogsources.Post.RegularPost; +import com.example.blogsources.Users.BasicUser; +import com.example.blogsources.Users.User; +import com.example.blogsources.Users.UsersInRam; +import com.example.blogsources.services.PostStorage; +import com.example.blogsources.utils.StringUtils; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.beans.factory.annotation.Qualifier; +import org.springframework.stereotype.Controller; +import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.ModelAndView; + +import javax.annotation.PostConstruct; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; +import javax.servlet.http.HttpSession; + +import static com.example.blogsources.URLMap.URLMap.*; + +// TODO: Add log4j +// TODO: Add DB +// TODO: Add Spring Security +// TODO: Add REST API +// TODO: Add i18n + +@Controller +@RequestMapping("/") +public class WebController { + + @Autowired + @Qualifier("postStorage") + PostStorage postStorage; + + @Autowired + @Qualifier("users") + UsersInRam users; + + @PostConstruct + public void initIt() throws Exception { + User user = new BasicUser("admin", 0); + if (!users.userNameExists(user.getUserName())){ + user.setPassword("1"); + user.setEmail("admin@example.com"); + users.addNewUser(user); + System.out.println("Add user admin"); + } + + User guest = new BasicUser("guest", 3); + if (!users.userNameExists(guest.getUserName())){ + guest.setPassword("1"); + guest.setEmail("guest@example.com"); + users.addNewUser(guest); + System.out.println("Add user guest"); + } + } + + @RequestMapping(value = "/", method = RequestMethod.GET) + public String hello(){ + return "welcome"; + } + + @RequestMapping(value = ABOUT, method = RequestMethod.GET) + public String about(){ + return "about"; + } + + @RequestMapping(value = WELCOME, method = RequestMethod.GET) + public ModelAndView welcome(){ + List posts = postStorage.displayAllPosts(); + return new ModelAndView("webBlog", "posts", posts); + } + + @RequestMapping(value = ADDPOST, method = RequestMethod.GET) + public ModelAndView addPost(HttpServletRequest request){ + ModelAndView modelAndView = new ModelAndView(); + User user = (User) request.getSession().getAttribute("currentUser"); + if (user == null || StringUtils.isEmpty(user.getUserName())){ + modelAndView.addObject("error_message", "Please log in"); + modelAndView.setViewName("error"); + return modelAndView; + } + if (user.getUserPrivilegeLevel() != 0){ + modelAndView.addObject("error_message", "You don't have enough permissions to add a new post"); + modelAndView.setViewName("error"); + return modelAndView; + } + return new ModelAndView("addPost", "command", new RegularPost()); + } + + @RequestMapping(value = ADDPOST, method = RequestMethod.POST) + public ModelAndView addPost(@ModelAttribute("post") RegularPost post){ + ModelAndView modelAndView = new ModelAndView(); + if (post == null){ + modelAndView.addObject("error_message", "The object post is NULL!!!"); + modelAndView.setViewName("error"); + return modelAndView; + } +// System.out.println("This is addPost in WebController. Title - " + post.getPostTitle() + " Content: " + post.getPostContent()); + postStorage.addNewPost(post); + List posts = postStorage.displayAllPosts(); + return new ModelAndView("webBlog", "posts", posts); + } + + + @RequestMapping(value = REGISTER, method = RequestMethod.GET) + public ModelAndView register(){ + return new ModelAndView("register", "command", new BasicUser()); + } + + @RequestMapping(value = REGISTER, method = RequestMethod.POST) + public ModelAndView register(@ModelAttribute("user") BasicUser user){ + ModelAndView modelAndView = new ModelAndView(); + if (user == null){ + modelAndView.addObject("error_message", "The object user is NULL!!!"); + modelAndView.setViewName("error"); + return modelAndView; + } + if (StringUtils.isEmpty(user.getUserName()) || StringUtils.isEmpty(user.getEmail()) ) { + modelAndView.addObject("error_message", "The user or email are incorrect !!!"); + modelAndView.setViewName("error"); + return modelAndView; + } + if (users.getUserByName(user.getUserName()) !=null) { + modelAndView.addObject("error_message", "The user '" + user.getUserName()+ "' already exists!!!"); + modelAndView.setViewName("error"); + return modelAndView; + } + users.addNewUser(user); + if (users.getUserByName(user.getUserName()) ==null) { + modelAndView.addObject("error_message", "The user '" + user.getUserName()+ "' was not registered!!!"); + modelAndView.setViewName("error"); + return modelAndView; + } +// modelAndView.addObject("message", "Id: " + users.getUserId(user.getUserName()) + " Name : " + user.getUserName() + " Password: " + user.getPassword() + " email: " + user.getEmail()); + modelAndView.addObject("message", "The new user '" + user.getUserName()+ "' was successfully registered!!!"); + modelAndView.setViewName("message"); + return modelAndView; + } + @RequestMapping(value = "/posts", method = RequestMethod.GET) + public ModelAndView getPosts(){ + List posts = postStorage.displayAllPosts(); + Post post = posts.get(posts.size() - 1); + System.out.println("The amount of posts: " + posts.size()); + System.out.println("The last one. Title - " + post.getPostTitle() + "Content: " + post.getPostContent()); + return new ModelAndView("webBlog", "posts", posts); + } + +// @RequestMapping(value = "/post", method = RequestMethod.GET) +// public ModelAndView getPost(@RequestParam("postTitle") String postTitle){ +// PostFromDemo post = postStorage.getPostByTitle(postTitle); +// return new ModelAndView("post", "post", post); +// } + + + @RequestMapping(value = LOGIN, method = RequestMethod.GET) + public ModelAndView login(){ + return new ModelAndView("login", "command", new BasicUser()); + } + + + @RequestMapping(value = LOGIN, method = {RequestMethod.POST}) + public ModelAndView login(@ModelAttribute("user") BasicUser user, HttpServletRequest request){ + ModelAndView modelAndView = new ModelAndView(); + if (user == null){ + modelAndView.addObject("error_message", "The object user is NULL!!!"); + modelAndView.setViewName("error"); + return modelAndView; + } + if (users.validateUser(user)){ + modelAndView.addObject("message", "Log in successful."); +// modelAndView.addObject("message", "Log in successful. Id: " + users.getUserId(user.getUserName()) + " Name : " + user.getUserName() + " Password: " + user.getPassword() + " email: " + user.getEmail()); + HttpSession ses = request.getSession(); + ses.setAttribute("currentUser", users.getUserByName(user.getUserName())); + modelAndView.setViewName("message"); + return modelAndView; + }else{ +// modelAndView.addObject("error_message", "Login failed: " + " Name : " + user.getUserName() + " Password: " + user.getPassword() + " email: " + user.getEmail()); + modelAndView.addObject("error_message", "Login failed. Please enter correct user and password."); + modelAndView.setViewName("error"); + return modelAndView; + } + } + + @RequestMapping(value = LOGOUT, method = RequestMethod.GET) + public ModelAndView logout(HttpServletRequest request, HttpServletResponse response){ + { + request.getSession().removeAttribute("currentUser"); + request.getSession().invalidate(); + response.setHeader("Cache-Control", "no-cache"); + response.setHeader("Pragma", "no-cache"); + response.setHeader("Expires", "-1"); + ModelAndView modelAndView = new ModelAndView(); + modelAndView.addObject("message", "Log Out successful"); + modelAndView.setViewName("message"); + return modelAndView; + } + } + + +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/services/PostStorage.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/services/PostStorage.java new file mode 100644 index 0000000..f011639 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/services/PostStorage.java @@ -0,0 +1,13 @@ +package com.example.blogsources.services; + +import com.example.blogsources.Post.Post; + +import java.util.List; + +public interface PostStorage { + void addNewPost(String type, String content, int userId); + void removePost(int id); +// void displayPost(int id, PrintStream out); + List displayAllPosts(); + void addNewPost(Post post); +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/services/PostsInRam.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/services/PostsInRam.java new file mode 100644 index 0000000..7477398 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/services/PostsInRam.java @@ -0,0 +1,58 @@ +package com.example.blogsources.services; + +import com.example.blogsources.Post.Exceptions.NotExistsPageException; +import com.example.blogsources.Post.Post; +import com.example.blogsources.Post.RegularPost; +import org.springframework.stereotype.Service; + +import java.io.PrintStream; +import java.util.ArrayList; +import java.util.List; + +@Service +public class PostsInRam implements PostStorage { + private List posts = new ArrayList<>(); + + public void addNewPost(String type, String content, int userId) { + posts.add(new RegularPost(posts.size(), content, userId)); + +// switch (type){ +// case "regular": //System.out.println("Add a new regular page #" + posts.size() + " and userId = " + userId+ "\n" + "Content:" + content+"\n"); +// posts.add(new RegularPost(posts.size(), content, userId)); +// break; +// case "url": posts.add( new URLPost(posts.size(), content, userId)); +// break; +// case "image": posts.add( new ImagePost(posts.size(), content, userId)); +// break; +// default: +// throw new IllegalPageTypeException("Invalid type of the page: " + type + "Expected values regular/url/image"); +// } + } + + + public void removePost(int id) { + if (id < 0 || id > posts.size()) { + throw new NotExistsPageException("Invalid id of the page: " + id + "\nmin value: 0;\n" + "Max Value: " + posts.size() ); + } else { + posts.remove(id); + } + } + + public void displayPost(int id, PrintStream out) { +// out.println("try to get page #"+id + " " + posts.get(id).getPostType() + " " + posts.get(id).getPostCreationDate() + "\n"); + if (id < 0 || id > posts.size()) { + throw new NotExistsPageException("Invalid id of the page: " + id + "\nmin value: 0;\n" + "Max Value: " + posts.size() ); + } else { + posts.get(id).displayPost(out); + } + } + + @Override + public List displayAllPosts() { + return posts; + } + + public void addNewPost(Post post) { + posts.add(post); + } +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/utils/Encyption.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/utils/Encyption.java new file mode 100644 index 0000000..ed0b80e --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/utils/Encyption.java @@ -0,0 +1,19 @@ +package com.example.blogsources.utils; + +import java.io.UnsupportedEncodingException; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; + +public class Encyption { + + public static String ecrypt(String plainTest) throws NoSuchAlgorithmException, UnsupportedEncodingException { + MessageDigest md = MessageDigest.getInstance("SHA-256"); + md.update(plainTest.getBytes("UTF-8")); + byte[] byteData = md.digest(); + StringBuffer sb = new StringBuffer(); + for (int i = 0; i < byteData.length; i++) { + sb.append(Integer.toString((byteData[i] & 0xff) + 0x100, 16).substring(1)); + } + return sb.toString(); + } +} diff --git a/Shevtsov/BlogSources/src/main/java/com/example/blogsources/utils/StringUtils.java b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/utils/StringUtils.java new file mode 100644 index 0000000..fc84f4e --- /dev/null +++ b/Shevtsov/BlogSources/src/main/java/com/example/blogsources/utils/StringUtils.java @@ -0,0 +1,7 @@ +package com.example.blogsources.utils; + +public class StringUtils { + public static boolean isEmpty(String string){ + return (string == null || string.isEmpty()) ? true : false; + } +} diff --git a/Shevtsov/BlogSources/src/main/resources/application.properties b/Shevtsov/BlogSources/src/main/resources/application.properties new file mode 100644 index 0000000..7179b58 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/resources/application.properties @@ -0,0 +1,5 @@ +spring.view.prefix: /WEB-INF/jsp/ +spring.view.suffix: .jsp +application.message: Hello Phil +server.servlet-path=/ +server.port=8080 \ No newline at end of file diff --git a/Shevtsov/BlogSources/src/main/webapp/WEB-INF/about.jsp b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/about.jsp new file mode 100644 index 0000000..9974b09 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/about.jsp @@ -0,0 +1,21 @@ +<%@include file="parts/begin.jsp" %> + +
+ <%@include file="parts/header.jsp" %> +
+
+ + +

About this blog

+ This is a template for a blog which uses Spring MVC + + +
+ <%@include file="parts/leftmenu.jsp" %> +
+ <%@include file="parts/prefooter.jsp" %> + <%@include file="parts/footer.jsp" %> +
+ + + diff --git a/Shevtsov/BlogSources/src/main/webapp/WEB-INF/addPost.jsp b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/addPost.jsp new file mode 100644 index 0000000..7fff3d9 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/addPost.jsp @@ -0,0 +1,37 @@ +<%@include file="parts/begin.jsp" %> + +
+ <%@include file="parts/header.jsp" %> +
+
+ + +

Add a new post

+ + + + + + + + + + + + + +
+ +
+ +
+ + +
+ <%@include file="parts/leftmenu.jsp" %> +
+ <%@include file="parts/prefooter.jsp" %> + <%@include file="parts/footer.jsp" %> +
+ + diff --git a/Shevtsov/BlogSources/src/main/webapp/WEB-INF/addUser.jsp b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/addUser.jsp new file mode 100644 index 0000000..6d137ab --- /dev/null +++ b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/addUser.jsp @@ -0,0 +1,34 @@ +<%@include file="parts/begin.jsp" %> + +
+ <%@include file="parts/header.jsp" %> +
+
+ + +

Register a new user

+ + + + + + + + + + + +
+ +
+
+ + +
+ <%@include file="parts/leftmenu.jsp" %> +
+ <%@include file="parts/prefooter.jsp" %> + <%@include file="parts/footer.jsp" %> +
+ + diff --git a/Shevtsov/BlogSources/src/main/webapp/WEB-INF/error.jsp b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/error.jsp new file mode 100644 index 0000000..7ceeae4 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/error.jsp @@ -0,0 +1,20 @@ +<%@include file="parts/begin.jsp" %> + +
+ <%@include file="parts/header.jsp" %> +
+
+ + +

Error

+ Error message: ${error_message} + + +
+ <%@include file="parts/leftmenu.jsp" %> +
+ <%@include file="parts/prefooter.jsp" %> + <%@include file="parts/footer.jsp" %> +
+ + diff --git a/Shevtsov/BlogSources/src/main/webapp/WEB-INF/login.jsp b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/login.jsp new file mode 100644 index 0000000..415b0dd --- /dev/null +++ b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/login.jsp @@ -0,0 +1,34 @@ +<%@include file="parts/begin.jsp" %> + +
+ <%@include file="parts/header.jsp" %> +
+
+ + +

Please Log In

+ + + + + + + + + + + +
+ +
+
+ + +
+ <%@include file="parts/leftmenu.jsp" %> +
+ <%@include file="parts/prefooter.jsp" %> + <%@include file="parts/footer.jsp" %> +
+ + diff --git a/Shevtsov/BlogSources/src/main/webapp/WEB-INF/logout.jsp b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/logout.jsp new file mode 100644 index 0000000..d4b7602 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/logout.jsp @@ -0,0 +1,21 @@ +<%@include file="parts/begin.jsp" %> + +
+ <%@include file="parts/header.jsp" %> +
+
+ + + + + Log Out successful + + +
+ <%@include file="parts/leftmenu.jsp" %> +
+ <%@include file="parts/prefooter.jsp" %> + <%@include file="parts/footer.jsp" %> +
+ + diff --git a/Shevtsov/BlogSources/src/main/webapp/WEB-INF/message.jsp b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/message.jsp new file mode 100644 index 0000000..e392781 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/message.jsp @@ -0,0 +1,19 @@ +<%@include file="parts/begin.jsp" %> + +
+ <%@include file="parts/header.jsp" %> +
+
+ + + ${message} + + +
+ <%@include file="parts/leftmenu.jsp" %> +
+ <%@include file="parts/prefooter.jsp" %> + <%@include file="parts/footer.jsp" %> +
+ + diff --git a/Shevtsov/BlogSources/src/main/webapp/WEB-INF/parts/begin.jsp b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/parts/begin.jsp new file mode 100644 index 0000000..5838758 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/parts/begin.jsp @@ -0,0 +1,14 @@ + + + + <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> + <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> + <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> + This is Demo + + + + diff --git a/Shevtsov/BlogSources/src/main/webapp/WEB-INF/parts/footer.jsp b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/parts/footer.jsp new file mode 100644 index 0000000..30a5d86 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/parts/footer.jsp @@ -0,0 +1,18 @@ + + + +<%-- +

Request Scope (key==values)

+<% + java.util.Enumeration reqEnum = request.getAttributeNames(); + while (reqEnum.hasMoreElements()) { + String s = reqEnum.nextElement(); + out.print(s); + out.println("==" + request.getAttribute(s)); +%>
+<% + } +%> +--%> \ No newline at end of file diff --git a/Shevtsov/BlogSources/src/main/webapp/WEB-INF/parts/header.jsp b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/parts/header.jsp new file mode 100644 index 0000000..f89b59d --- /dev/null +++ b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/parts/header.jsp @@ -0,0 +1,16 @@ +<%@ page import="com.example.blogsources.Users.User" %> +<%@ page session="true" %> + \ No newline at end of file diff --git a/Shevtsov/BlogSources/src/main/webapp/WEB-INF/parts/leftmenu.jsp b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/parts/leftmenu.jsp new file mode 100644 index 0000000..ff87939 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/parts/leftmenu.jsp @@ -0,0 +1,29 @@ +
+ +
+ diff --git a/Shevtsov/BlogSources/src/main/webapp/WEB-INF/parts/prefooter.jsp b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/parts/prefooter.jsp new file mode 100644 index 0000000..48e5cdc --- /dev/null +++ b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/parts/prefooter.jsp @@ -0,0 +1,18 @@ + diff --git a/Shevtsov/BlogSources/src/main/webapp/WEB-INF/register.jsp b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/register.jsp new file mode 100644 index 0000000..04a832a --- /dev/null +++ b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/register.jsp @@ -0,0 +1,37 @@ +<%@include file="parts/begin.jsp" %> + +
+ <%@include file="parts/header.jsp" %> +
+
+ + +

Register a new user

+ + + + + + + + + + + + + + +
+ +
+
+ + +
+ <%@include file="parts/leftmenu.jsp" %> +
+ <%@include file="parts/prefooter.jsp" %> + <%@include file="parts/footer.jsp" %> +
+ + diff --git a/Shevtsov/BlogSources/src/main/webapp/WEB-INF/springframeworkmvc-servlet.xml b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/springframeworkmvc-servlet.xml new file mode 100644 index 0000000..87db704 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/springframeworkmvc-servlet.xml @@ -0,0 +1,50 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Shevtsov/BlogSources/src/main/webapp/WEB-INF/web.xml b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/web.xml new file mode 100644 index 0000000..80f98f7 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/web.xml @@ -0,0 +1,48 @@ + + + Web Blog Demo + + + springframeworkmvc + org.springframework.web.servlet.DispatcherServlet + 1 + + + + springframeworkmvc + / + + + + index.jsp + + + + contextConfigLocation + /WEB-INF/springframeworkmvc-servlet.xml + + + org.springframework.web.context.ContextLoaderListener + + + + + + + + + \ No newline at end of file diff --git a/Shevtsov/BlogSources/src/main/webapp/WEB-INF/webBlog.jsp b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/webBlog.jsp new file mode 100644 index 0000000..d118471 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/webapp/WEB-INF/webBlog.jsp @@ -0,0 +1,25 @@ +<%@include file="parts/begin.jsp" %> + +
+ <%@include file="parts/header.jsp" %> +
+
+ + +

Blog Template

+ +

${post.title}

+ ${post.postContent} +

Posted by Jack Read more Comments (15) 17.01.

+ +
+ + +
+ <%@include file="parts/leftmenu.jsp" %> +
+ <%@include file="parts/prefooter.jsp" %> + <%@include file="parts/footer.jsp" %> +
+ + diff --git a/Shevtsov/BlogSources/src/main/webapp/index.jsp b/Shevtsov/BlogSources/src/main/webapp/index.jsp new file mode 100644 index 0000000..9d15ba9 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/webapp/index.jsp @@ -0,0 +1,14 @@ +<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> +<%-- +// TODO: replace all URLs in jsp by URL constans from URLMap +--%> +<%@ page contentType="text/html;charset=UTF-8" language="java" %> + + + + + + + + + diff --git a/Shevtsov/BlogSources/src/main/webapp/res/themes/css/custom.css b/Shevtsov/BlogSources/src/main/webapp/res/themes/css/custom.css new file mode 100644 index 0000000..caddf49 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/webapp/res/themes/css/custom.css @@ -0,0 +1,46 @@ +input:not([type=submit]):not([type=file]) { + border: 5px solid white; + -webkit-box-shadow: + inset 0 0 8px rgba(0,0,0,0.1), + 0 0 16px rgba(0,0,0,0.1); + -moz-box-shadow: + inset 0 0 8px rgba(0,0,0,0.1), + 0 0 16px rgba(0,0,0,0.1); + box-shadow: + inset 0 0 8px rgba(0,0,0,0.1), + 0 0 16px rgba(0,0,0,0.1); + padding: 15px; + background: rgba(255,255,255,0.5); + margin: 0 0 10px 0; +} + +textarea { + border: 5px solid white; + -webkit-box-shadow: + inset 0 0 8px rgba(0,0,0,0.1), + 0 0 16px rgba(0,0,0,0.1); + -moz-box-shadow: + inset 0 0 8px rgba(0,0,0,0.1), + 0 0 16px rgba(0,0,0,0.1); + box-shadow: + inset 0 0 8px rgba(0,0,0,0.1), + 0 0 16px rgba(0,0,0,0.1); + padding: 15px; + background: rgba(255,255,255,0.5); + margin: 0 0 10px 0; +} + +#username { + font-weight: bold; + color: red; + } + +#error_message { + font-weight: bold; + color: red; + } + +#message { + font-weight: bold; + color: green; +} \ No newline at end of file diff --git a/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/images/arrow.gif b/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/images/arrow.gif new file mode 100644 index 0000000..ca63156 Binary files /dev/null and b/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/images/arrow.gif differ diff --git a/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/images/bg.jpg b/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/images/bg.jpg new file mode 100644 index 0000000..e73f088 Binary files /dev/null and b/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/images/bg.jpg differ diff --git a/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/images/bg2.jpg b/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/images/bg2.jpg new file mode 100644 index 0000000..bfc1801 Binary files /dev/null and b/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/images/bg2.jpg differ diff --git a/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/images/comment.gif b/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/images/comment.gif new file mode 100644 index 0000000..ef304fc Binary files /dev/null and b/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/images/comment.gif differ diff --git a/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/images/footer.jpg b/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/images/footer.jpg new file mode 100644 index 0000000..3225b09 Binary files /dev/null and b/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/images/footer.jpg differ diff --git a/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/images/header.jpg b/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/images/header.jpg new file mode 100644 index 0000000..55d8693 Binary files /dev/null and b/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/images/header.jpg differ diff --git a/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/images/more.gif b/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/images/more.gif new file mode 100644 index 0000000..c55873a Binary files /dev/null and b/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/images/more.gif differ diff --git a/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/images/prefooter.jpg b/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/images/prefooter.jpg new file mode 100644 index 0000000..7e870ff Binary files /dev/null and b/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/images/prefooter.jpg differ diff --git a/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/images/style.css b/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/images/style.css new file mode 100644 index 0000000..4fcca0d --- /dev/null +++ b/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/images/style.css @@ -0,0 +1,83 @@ +/* + +Manuscript Free Template +By Herreman David +http://www.free-css-templates.com + +*/ +* { padding: 0; margin: 0 } +body { margin:5px 0; padding:0; font: 74% Arial, Sans-Serif; color:#000; line-height: 1.4em; background : #575757;} +.content { color: #3B2A0A; margin: 0 auto; padding: 0; width: 825px; background: url(bg2.jpg) repeat-y center bottom; } +#header { background: url(header.jpg) no-repeat; width: 825px; height: 213px; color: #3B2A0A; } +#header h1 { background: inherit; font: bold 3em "Tahoma", Arial; color: #85621E; line-height: 1em;} +#header h3 { background: inherit; font: bold 1em "Tahoma", Arial; color: #000; } +#header .title { padding: 60px 0 0 60px; } +#main { background: url(bg.jpg) no-repeat top; height: 353px; margin: 0; padding: 0 } +#main .center { width: 550px; float: right; padding: 0 60px 0 0px; } +#main h2 { background-color: inherit; color: #85621E; line-height: 0.2em; } +#main h3 { font: 85% Arial, Sans-Serif; margin: 0 0 10px 0; padding: 0; color: #5f5f5f; background: inherit; } + +.leftmenu { width: 120px; float: left; padding: 0; padding: 0 0 0 40px; } +.padding { padding: 15px; } + +.img { background: #FFF; float: left; padding: 1px; margin: 5px 8px 5px 0; color: #333; border: 1px solid #e0e0e0; } + +/* PREFOOTER */ +#prefooter { background: url(prefooter.jpg) no-repeat; color: #85621f; height: 190px; text-align: left; clear: both; padding: 0px; margin: 0; } +#prefooter .particles { float: right; width: 300px; padding: 20px 30px 0 0; background: transparent } +#prefooter .comments { width: 270px; padding: 20px 0px 0 0px; float: right; background: transparent } + + +/* FOOTER */ +#footer { background: url(footer.jpg) no-repeat; color: #8f8f8f; height: 44px; text-align: right; font-size: 90%; clear: both; padding: 0; margin: 0;} +#footer .padding { padding: 20px 60px 0 0; } +#footer hr { width: 740px; color: #888; border: 1px solid #f0f0f0; margin: 0 25px 0 25px; } +#footer a { color: #999; text-decoration:none; } +#footer a:hover { text-decoration: underline; color: #FFF;} + +/* NAVIGATION */ +.nav { clear: both; text-align: left; color: #85621E; padding: 20px 0 0 0; } +.nav li { font-weight: bold; margin: 0 0 8px 0; padding: 0 0 0 5px; border-bottom: 1px solid #D2B881 } +.nav li a { color: #546078; text-decoration: none; } +.nav li a:hover { color: #95722E; text-decoration: none; } + + +.boxads { background: #E7EDF0; padding: 5px; border: 1px solid #999; color: #333; } + +/* ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------*/ + +p { margin: 0 0 5px 0; padding: 0; color: #555; background: inherit; } + +hr { border: 0; height: 1px; color: #eee; background-color: #eee; } +a { color: #0F5B7F; background: inherit; text-decoration:none; } +a:hover { background: inherit; text-decoration: underline; } +h1 { padding:0; margin:0; color: #0F5B7F; background: inherit;font: bold 1.8em Arial, Sans-Serif; letter-spacing: -1px; } +h1 a {color: #0F5BFF; background: inherit;} +h2 { background-color: inherit; color:#85621E; font-size:140%; font-weight:bold; margin: 10px 0 10px 0; padding:0; } +h2 a { color: #85621E; } +h2 a:hover { color: #BD9C5C; text-decoration: none;} + +ul { margin: 5px 0 20px 15px; padding : 0; list-style : none; } +li { list-style-type: none; color: #F29900; margin: 0 0 0px 0; padding: 0 0 0 0px; } +li a { color: #546078; } +li a:hover { color: #F29900; } + +/*----------------- for snews --------------------------*/ + +fieldset { border: 1px solid #eee; } +textarea.text { height: 150px; width: 90%; border: 1px solid #ccc; background: #fff; color: #000; } +textarea.text:hover { border: 1px solid #eee; background: #FFFFF4; color: #000; } +input.field { border: 1px solid #ccc; background-color: #3C3C3C; width: 100px; color: #000; } +input.field:hover { border: 1px solid #eee; background: #ccc; color: #000; } +input.text { margin: 0; width: 110px; border: 1px solid #546078; background: #FFF; color: #808080; } +input.text:hover { border: 1px solid #3a4354; } +input.searchbutton { margin: 0; font-size: 100%; font-family: Arial, Sans-serif; border: none; background: #FFFFFF; color: #333; padding: 1px; font-weight: bold; } +.date { color: #5f5f5f; background: inherit; text-align: right; margin: 4px 0 5px 0; padding: 0.4em 0 0 0; border-top: 1px solid #85621E; } +.date a { color: #546078; } +input.button { background: #FFFFF4; color: #808080; border-right: 1px solid #ccc; border-bottom: 1px solid #ccc; } +.comments { padding: 10px 10px 8px 10px; margin: 0 0 7px 0; background: #FFF; color: #333; } +.commentsbox { padding: 8px 0 10px 10px; margin: 0 0 10px 0; background: #f4f4f4; color: #000; } +.error { color: #990000; background-color: #FFF0F0; padding: 7px; margin-top: 5px; margin-bottom: 10px; border: 1px dashed #990000; } +.error h2 { color: #990000; background: inherit; } +.success { color: #000000; background: #FAFAFA; padding: 7px; margin-top: 5px; margin-bottom: 5px; border: 1px dashed #546078; } +.success h2 { color: #546078; background: inherit; } diff --git a/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/images/timeicon.gif b/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/images/timeicon.gif new file mode 100644 index 0000000..441126c Binary files /dev/null and b/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/images/timeicon.gif differ diff --git a/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/index.html b/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/index.html new file mode 100644 index 0000000..0194d66 --- /dev/null +++ b/Shevtsov/BlogSources/src/main/webapp/res/themes/manuscript/index.html @@ -0,0 +1,68 @@ + + + +Manuscript Free Template + + + + +
+ +
+
+

Manuscript Template

+

YOUR NEWS ON A OLD PAPER

+ Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec sem. Praesent eu metus. Vivamus ac urna. Maecenas tincidunt libero id ipsum. Duis ipsum erat, laoreet in, ultrices at, blandit non, enim. Maecenas et libero. In laoreet vehicula enim. Nam at massa. Donec porttitor, odio id scelerisque pretium, augue eros cursus est, eget interdum dui justo et tellus. Aenean a neque eu mauris ultrices viverra. In ac urna. Etiam in dolor sit amet arcu auctor interdum. Fusce non quam. Nunc aliquet, quam eu facilisis venenatis, pede augue adipiscing lorem, nec tincidunt nunc felis eget odio. Integer iaculis pretium odio. Integer viverra. Integer porttitor elementum diam. In hac habitasse platea dictumst. Donec porta elit. +

Posted by Jack Read more Comments (15) 17.01.

+
+

Try it with sNews 1.4

+

AND WHY NOT WITH sNEWS 1.5 ;-)

+ Lorem ipsum dolor sit amet, consectetuer adipiscing elit. Donec sem. Praesent eu metus. Vivamus ac urna. Maecenas tincidunt libero id ipsum. Duis ipsum erat, laoreet in, ultrices at, blandit non, enim. Maecenas et libero. In laoreet vehicula enim. Nam at massa. Donec porttitor, odio id scelerisque pretium, augue eros cursus est, eget interdum dui justo et tellus. Aenean a neque eu mauris ultrices viverra. In ac urna. Etiam in dolor sit amet arcu auctor interdum. Fusce non quam. Nunc aliquet, quam eu facilisis venenatis, pede augue adipiscing lorem, nec tincidunt nunc felis eget odio. Integer iaculis pretium odio. Integer viverra. Integer porttitor elementum diam. In hac habitasse platea dictumst. Donec porta elit. +

Posted by Jack Read more Comments (15) 17.01.

+
+
+ +
+ + +
+ + diff --git a/Shevtsov/TestingFramework/pom.xml b/Shevtsov/TestingFramework/pom.xml new file mode 100644 index 0000000..14c9b4d --- /dev/null +++ b/Shevtsov/TestingFramework/pom.xml @@ -0,0 +1,81 @@ + + + 4.0.0 + + com.example + TestingFramework + 1.0-SNAPSHOT + jar + + TestingFramework + This is testing framework for BlogSources + + + 1.7 + 6.9.4 + 2.46.0 + UTF-8 + + + + + com.example.blogsources + URLMapping + 0.0.1 + jar + + + + org.seleniumhq.selenium + selenium-java + ${selenium.version} + + + + org.apache.httpcomponents + httpclient + 4.4.1 + + + + org.testng + testng + ${testng.version} + + + + org.hamcrest + hamcrest-all + 1.3 + + + + ru.stqa.selenium + webdriver-factory + 1.1.43 + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + ${java.version} + ${java.version} + + + + org.apache.maven.plugins + maven-surefire-plugin + + false + + + + + \ No newline at end of file diff --git a/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/Actions/AddPostPage.java b/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/Actions/AddPostPage.java new file mode 100644 index 0000000..0c8120b --- /dev/null +++ b/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/Actions/AddPostPage.java @@ -0,0 +1,60 @@ +package com.example.blogsources.demotest.Actions; + +import com.example.blogsources.demotest.Driver; +import com.example.blogsources.demotest.utils.Elements; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; + +import java.util.List; + +import static com.example.blogsources.URLMap.URLMap.FULL_ADDPOST; +import static com.example.blogsources.URLMap.URLMap.FULL_REGISTER; + +public class AddPostPage { + private static String SUBMIT_ADDPOST_XPATH = "//input[@test-id='submit-addpost']"; + private static String TITLE_ADDPOST_XPATH = "//input[@test-id='addpost-title']"; + private static String CONTENT_ADDPOST_XPATH = "//textarea[@test-id='addpost-content']"; + private static String TITLE_XPATH = "//*[@id=\"main\"]/div[1]/h3*']"; + + public static void go(){ + Driver.driver.navigate().to(FULL_ADDPOST); + } + + public static boolean at(){ + boolean result = false; + result = (Driver.driver.getCurrentUrl().toString().equals(FULL_ADDPOST) && + Elements.isFoundAndDisplayed(By.xpath(SUBMIT_ADDPOST_XPATH)) && + Elements.isFoundAndDisplayed(By.xpath(TITLE_ADDPOST_XPATH)) && + Elements.isFoundAndDisplayed(By.xpath(CONTENT_ADDPOST_XPATH))) + ? true :false; + return result; + } + + public static void addPost (String title, String content){ + WebElement titleElement = Elements.getElement(By.xpath(TITLE_ADDPOST_XPATH)); + if (titleElement != null) {titleElement.sendKeys(title);} + WebElement contentElement = Elements.getElement(By.xpath(CONTENT_ADDPOST_XPATH)); + if (contentElement != null) { + contentElement.sendKeys(content); + contentElement.submit(); + } + } + + public static boolean getAddPostStatus(){ + if ( Elements.isFoundAndDisplayed(By.id("error_message")) == true ){ + return false; + } + WebElement postTitlesList = Elements.getElement(By.xpath(TITLE_XPATH)); + System.out.println("Titles:" + postTitlesList); + return true; +// if ( Elements.isFoundAndDisplayed(By.id("message")) != true ){ +// return false; +// } +// WebElement message = Elements.getElement(By.id("message")); +// if (message.getText().contains("was successfully registered")){ +// return true; +// }else { +// return false; +// } + } +} diff --git a/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/Actions/LoginPage.java b/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/Actions/LoginPage.java new file mode 100644 index 0000000..241a33f --- /dev/null +++ b/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/Actions/LoginPage.java @@ -0,0 +1,48 @@ +package com.example.blogsources.demotest.Actions; + +import com.example.blogsources.demotest.Driver; +import com.example.blogsources.demotest.utils.Elements; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; + +import static com.example.blogsources.URLMap.URLMap.*; + +public class LoginPage { + private static String SUBMIT_INPUT_XPATH = "//input[@test-id='submit-login']"; + private static String USERNAME_INPUT_XPATH = "//input[@test-id='input-username']"; + public static String PASSWORD_INPUT_XPATH = "//input[@test-id='input-password']"; + private static String USERNAME_ID = "username"; + public static void go(){ + Driver.driver.navigate().to(FULL_LOGIN); + } + + public static boolean at(){ + boolean result = false; + result = (Driver.driver.getCurrentUrl().toString().equals(FULL_LOGIN) && + Elements.isFoundAndDisplayed(By.xpath(SUBMIT_INPUT_XPATH)) && + Elements.isFoundAndDisplayed(By.xpath(USERNAME_INPUT_XPATH)) && + Elements.isFoundAndDisplayed(By.xpath(PASSWORD_INPUT_XPATH))) + ? true :false; + return result; + } + + public static void login (String username, String password){ + WebElement usernameElement = Elements.getElement(By.xpath(USERNAME_INPUT_XPATH)); + if (usernameElement != null) {usernameElement.sendKeys(username);} + WebElement passwordElement = Elements.getElement(By.xpath(PASSWORD_INPUT_XPATH)); + if (passwordElement != null) { + passwordElement.sendKeys(password); + passwordElement.submit(); + } + } + + public static boolean isLoggedIn (){ + WebElement element = null; + return (Elements.isFoundAndDisplayed(By.id(USERNAME_ID))) ? true : false; + } + + public static boolean isLoggedInAs (String username){ + return (Elements.isFoundAndDisplayed(By.id(USERNAME_ID)) && Elements.getElement(By.id(USERNAME_ID)).getText().equals(username)) ? true : false; + } + +} diff --git a/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/Actions/LogoutPage.java b/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/Actions/LogoutPage.java new file mode 100644 index 0000000..e49fd15 --- /dev/null +++ b/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/Actions/LogoutPage.java @@ -0,0 +1,26 @@ +package com.example.blogsources.demotest.Actions; + +import com.example.blogsources.demotest.Driver; +import com.example.blogsources.demotest.utils.Elements; +import org.openqa.selenium.By; + +import static com.example.blogsources.URLMap.URLMap.FULL_LOGOUT; + +public class LogoutPage { + private static String TOP_LOGIN_URL_ID = "top-login"; + public static void go(){ + Driver.driver.navigate().to(FULL_LOGOUT); + } + + public static boolean at(){ + return (Elements.isFoundAndDisplayed(By.id(TOP_LOGIN_URL_ID)) && Driver.driver.getCurrentUrl().toString().equals(FULL_LOGOUT)) ? true :false; + } + + public static void logout (){ + go(); + } + + public static boolean isLoggedOut (){ + return (Elements.isFoundAndDisplayed(By.id(TOP_LOGIN_URL_ID))) ? true : false; + } +} diff --git a/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/Actions/RegistrationPage.java b/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/Actions/RegistrationPage.java new file mode 100644 index 0000000..f58fd90 --- /dev/null +++ b/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/Actions/RegistrationPage.java @@ -0,0 +1,56 @@ +package com.example.blogsources.demotest.Actions; + +import com.example.blogsources.demotest.Driver; +import com.example.blogsources.demotest.utils.Elements; +import org.openqa.selenium.By; +import org.openqa.selenium.WebElement; + +import static com.example.blogsources.URLMap.URLMap.FULL_REGISTER; + +public class RegistrationPage { + private static String SUBMIT_REGISTRATION_XPATH = "//input[@test-id='submit-register']"; + private static String EMAIL_REGISTRATION_XPATH = "//input[@test-id='register-email']"; + private static String USERNAME_REGISTRATION_XPATH = "//input[@test-id='register-username']"; + public static String PASSWORD_REGISTRATION_XPATH = "//input[@test-id='register-password']"; + public static void go(){ + Driver.driver.navigate().to(FULL_REGISTER); + } + + public static boolean at(){ + boolean result = false; + result = (Driver.driver.getCurrentUrl().toString().equals(FULL_REGISTER) && + Elements.isFoundAndDisplayed(By.xpath(SUBMIT_REGISTRATION_XPATH)) && + Elements.isFoundAndDisplayed(By.xpath(EMAIL_REGISTRATION_XPATH)) && + Elements.isFoundAndDisplayed(By.xpath(USERNAME_REGISTRATION_XPATH)) && + Elements.isFoundAndDisplayed(By.xpath(PASSWORD_REGISTRATION_XPATH))) + ? true :false; + return result; + } + + public static void register (String email, String username, String password){ + WebElement emailElement = Elements.getElement(By.xpath(EMAIL_REGISTRATION_XPATH)); + if (emailElement != null) {emailElement.sendKeys(email);} + WebElement usernameElement = Elements.getElement(By.xpath(USERNAME_REGISTRATION_XPATH)); + if (usernameElement != null) {usernameElement.sendKeys(username);} + WebElement passwordElement = Elements.getElement(By.xpath(PASSWORD_REGISTRATION_XPATH)); + if (passwordElement != null) { + passwordElement.sendKeys(password); + passwordElement.submit(); + } + } + + public static boolean getRegistrationStatus(){ + if ( Elements.isFoundAndDisplayed(By.id("error_message")) == true ){ + return false; + } + if ( Elements.isFoundAndDisplayed(By.id("message")) != true ){ + return false; + } + WebElement message = Elements.getElement(By.id("message")); + if (message.getText().contains("was successfully registered")){ + return true; + }else { + return false; + } + } +} diff --git a/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/Driver.java b/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/Driver.java new file mode 100644 index 0000000..db685c1 --- /dev/null +++ b/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/Driver.java @@ -0,0 +1,11 @@ +package com.example.blogsources.demotest; + +import org.openqa.selenium.WebDriver; +import org.openqa.selenium.firefox.FirefoxDriver; +import org.openqa.selenium.remote.DesiredCapabilities; +import ru.stqa.selenium.factory.WebDriverFactory; + +public class Driver { + public static WebDriver driver = WebDriverFactory.getDriver(DesiredCapabilities.firefox()); +// public static WebDriver driver = new FirefoxDriver(); +} diff --git a/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/Tests/Registration.java b/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/Tests/Registration.java new file mode 100644 index 0000000..49e2e3c --- /dev/null +++ b/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/Tests/Registration.java @@ -0,0 +1,44 @@ +package com.example.blogsources.demotest.Tests; + +import com.example.blogsources.demotest.Actions.LoginPage; +import com.example.blogsources.demotest.Actions.LogoutPage; +import com.example.blogsources.demotest.Actions.RegistrationPage; +import com.example.blogsources.demotest.dataProviders.loginDataProvider; +import com.example.blogsources.demotest.dataProviders.registrationDataProvider; +import com.gargoylesoftware.htmlunit.WebClient; +import org.testng.annotations.Test; + +import static com.example.blogsources.URLMap.URLMap.FULL_URL; +import static com.example.blogsources.demotest.Driver.driver; +import static org.apache.http.HttpStatus.SC_OK; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +@Test(dependsOnGroups = {"init", "login"}) +public class Registration { + + @Test (groups = { "sanity", "registration" }) + public void isRegistrationPageAvailable(){ + RegistrationPage.go(); + assertThat(RegistrationPage.at(), is(true)); + } + + @Test(dataProvider = "getRegistrationDataProvider", dataProviderClass = registrationDataProvider.class, + dependsOnMethods = { "isRegistrationPageAvailable" }, + groups = { "registration" }, + timeOut = 3000) + public void testRegistration(String email, String username, String password, boolean expected){ + RegistrationPage.go(); + assertThat(RegistrationPage.at(), is(true)); + RegistrationPage.register(email, username, password); + assertThat(RegistrationPage.getRegistrationStatus(), is(expected)); + if (expected) { + LoginPage.go(); + assertThat(LoginPage.isLoggedIn(), is(false)); + LoginPage.login(username, password); + assertThat(LoginPage.isLoggedInAs(username), is(expected)); + LogoutPage.logout(); + assertThat(LogoutPage.isLoggedOut(), is(true)); + } + } +} \ No newline at end of file diff --git a/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/Tests/addPost.java b/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/Tests/addPost.java new file mode 100644 index 0000000..1fd1c7c --- /dev/null +++ b/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/Tests/addPost.java @@ -0,0 +1,35 @@ +package com.example.blogsources.demotest.Tests; + +import com.example.blogsources.demotest.Actions.AddPostPage; +import com.example.blogsources.demotest.Actions.LoginPage; +import com.example.blogsources.demotest.Actions.LogoutPage; +import com.example.blogsources.demotest.Actions.RegistrationPage; +import com.example.blogsources.demotest.dataProviders.addPostDataProvider; +import com.example.blogsources.demotest.dataProviders.registrationDataProvider; +import org.testng.annotations.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +@Test(dependsOnGroups = {"init", "login"}) +public class addPost { + + private boolean isAddPagePageAvailable(){ + AddPostPage.go(); + return AddPostPage.at(); + } + + @Test(dataProvider = "getAddPostDataProvider", dataProviderClass = addPostDataProvider.class, + groups = { "addPost" }, + timeOut = 3000) + public void testRegistration(String username, String password, String title, String content, boolean isAddPageAvailable, boolean expected){ + LoginPage.go(); + LoginPage.login(username, password); + assertThat(LoginPage.isLoggedInAs(username), is(true)); + assertThat(isAddPagePageAvailable(), is(isAddPageAvailable)); + if (isAddPageAvailable) { + AddPostPage.addPost(title, content); + assertThat(AddPostPage.getAddPostStatus(), is(expected)); + } + } +} \ No newline at end of file diff --git a/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/Tests/init.java b/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/Tests/init.java new file mode 100644 index 0000000..7b45967 --- /dev/null +++ b/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/Tests/init.java @@ -0,0 +1,25 @@ +package com.example.blogsources.demotest.Tests; + +import com.example.blogsources.demotest.Actions.LoginPage; +import com.example.blogsources.demotest.Actions.LogoutPage; +import com.example.blogsources.demotest.dataProviders.loginDataProvider; +import com.gargoylesoftware.htmlunit.WebClient; +import org.testng.annotations.Test; + +import static com.example.blogsources.URLMap.URLMap.FULL_URL; +import static com.example.blogsources.demotest.Driver.driver; +import static org.apache.http.HttpStatus.SC_OK; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +@Test(groups = "init") +public class init { + @Test + public void isAppAvailable() throws Exception{ + driver.navigate().to(FULL_URL); + + WebClient webClient = new WebClient(); + int statusCode = webClient.getPage(FULL_URL).getWebResponse().getStatusCode(); + assertThat(statusCode, is(SC_OK)); + } +} \ No newline at end of file diff --git a/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/Tests/loginLogout.java b/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/Tests/loginLogout.java new file mode 100644 index 0000000..e800d4d --- /dev/null +++ b/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/Tests/loginLogout.java @@ -0,0 +1,48 @@ +package com.example.blogsources.demotest.Tests; + +import com.example.blogsources.demotest.Actions.LoginPage; +import com.example.blogsources.demotest.Actions.LogoutPage; +import com.example.blogsources.demotest.dataProviders.loginDataProvider; +import com.gargoylesoftware.htmlunit.WebClient; +import org.testng.annotations.Test; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.is; + +import static com.example.blogsources.URLMap.URLMap.*; + +import static com.example.blogsources.demotest.Driver.driver; +import static org.apache.http.HttpStatus.*; + +@Test(dependsOnGroups = "init") +public class loginLogout { + + @Test (groups = { "sanity", "login" }) + public void isLoginPageAvailable(){ + LoginPage.go(); + assertThat(LoginPage.at(), is(true)); + } + + @Test (groups = { "sanity", "logout" }) + public void isLogoutPageAvailable(){ + LogoutPage.go(); + assertThat(LogoutPage.at(), is(true)); + } + + @Test(dataProvider = "getLoginDataProvider", dataProviderClass = loginDataProvider.class, + dependsOnMethods = { "isLoginPageAvailable" }, + groups = { "login" }, + timeOut = 3000) + public void testLogin(String username, String password, boolean expected){ + LoginPage.go(); + assertThat(LoginPage.isLoggedIn(), is(false)); + LoginPage.login(username, password); + assertThat(LoginPage.isLoggedInAs(username), is(expected)); + logout(); + } + + private void logout(){ + LogoutPage.logout(); + assertThat(LogoutPage.isLoggedOut(), is (true)); + } +} \ No newline at end of file diff --git a/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/dataProviders/addPostDataProvider.java b/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/dataProviders/addPostDataProvider.java new file mode 100644 index 0000000..2b03a61 --- /dev/null +++ b/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/dataProviders/addPostDataProvider.java @@ -0,0 +1,15 @@ +package com.example.blogsources.demotest.dataProviders; + +import org.testng.annotations.DataProvider; + +public class addPostDataProvider { + @DataProvider(name = "getAddPostDataProvider") + public static Object [][] getAddPostDataProvider(){ + return new Object[][]{ +// {"admin", "1", null,null, false}, + {"admin", "1", "title1","content 1", true, true}, + {"guest", "1", "title2","content 2", false, false}, + {"admin", "1", "title3","content 3", true, true}, + }; + } +} diff --git a/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/dataProviders/loginDataProvider.java b/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/dataProviders/loginDataProvider.java new file mode 100644 index 0000000..696c28b --- /dev/null +++ b/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/dataProviders/loginDataProvider.java @@ -0,0 +1,17 @@ +package com.example.blogsources.demotest.dataProviders; + +import org.testng.annotations.DataProvider; + +import java.util.Objects; + +public class loginDataProvider { + @DataProvider(name = "getLoginDataProvider") + public static Object [][] getLoginDataProvider(){ + return new Object[][]{ + {null,null, false}, + {"NonExistedUser","1", false}, + {"admin","1", true}, + {"guest","1", true}, + }; + } +} diff --git a/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/dataProviders/registrationDataProvider.java b/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/dataProviders/registrationDataProvider.java new file mode 100644 index 0000000..ab39edd --- /dev/null +++ b/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/dataProviders/registrationDataProvider.java @@ -0,0 +1,15 @@ +package com.example.blogsources.demotest.dataProviders; + +import org.testng.annotations.DataProvider; + +public class registrationDataProvider { + @DataProvider(name = "getRegistrationDataProvider") + public static Object [][] getRegistrationDataProvider(){ + return new Object[][]{ + {null,null,null, false}, +// {"IncorrectEmail","User","1", false}, + {"existedUser@example.com","admin","1", false}, + {"newuser@example.com","newuser","111", true}, + }; + } +} diff --git a/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/utils/Elements.java b/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/utils/Elements.java new file mode 100644 index 0000000..771c2db --- /dev/null +++ b/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/utils/Elements.java @@ -0,0 +1,23 @@ +package com.example.blogsources.demotest.utils; + +import com.example.blogsources.demotest.Driver; +import org.openqa.selenium.By; +import org.openqa.selenium.NoSuchElementException; +import org.openqa.selenium.WebElement; + +public class Elements { + public static boolean isFoundAndDisplayed (By by){ + WebElement element = null; + try { + element = Driver.driver.findElement(by);; + return (element != null && element.isDisplayed()) ? true : false; + }catch (NoSuchElementException e) { + return false; + } + } + + public static WebElement getElement (By by){ + return (isFoundAndDisplayed(by)) ? Driver.driver.findElement(by) : null; + } + +} diff --git a/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/xmlConfigs/All.xml b/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/xmlConfigs/All.xml new file mode 100644 index 0000000..959e9af --- /dev/null +++ b/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/xmlConfigs/All.xml @@ -0,0 +1,61 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/xmlConfigs/login.xml b/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/xmlConfigs/login.xml new file mode 100644 index 0000000..7430b1d --- /dev/null +++ b/Shevtsov/TestingFramework/src/main/java/com/example/blogsources/demotest/xmlConfigs/login.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/Shevtsov/URLMapping/pom.xml b/Shevtsov/URLMapping/pom.xml new file mode 100644 index 0000000..078437c --- /dev/null +++ b/Shevtsov/URLMapping/pom.xml @@ -0,0 +1,35 @@ + + + 4.0.0 + + com.example.blogsources + URLMapping + 0.0.1 + jar + + URLMapping + It contains all links + + + 1.7 + UTF-8 + + + + + + + + + org.apache.maven.plugins + maven-compiler-plugin + 3.3 + + ${java.version} + ${java.version} + + + + + diff --git a/Shevtsov/URLMapping/src/main/java/com/example/blogsources/URLMap/URLMap.java b/Shevtsov/URLMapping/src/main/java/com/example/blogsources/URLMap/URLMap.java new file mode 100644 index 0000000..a55933c --- /dev/null +++ b/Shevtsov/URLMapping/src/main/java/com/example/blogsources/URLMap/URLMap.java @@ -0,0 +1,21 @@ +package com.example.blogsources.URLMap; + +public class URLMap { + public static final String PROTO = "http://"; + public static final String HOST = "localhost"; + public static final String PORT = ":8080/"; + public static final String CONTEXT = "demo"; + public static final String FULL_URL = PROTO + HOST + PORT + CONTEXT; + public static final String LOGIN = "/login"; + public static final String FULL_LOGIN = FULL_URL + LOGIN; + public static final String LOGOUT = "/logout"; + public static final String FULL_LOGOUT = FULL_URL + LOGOUT; + public static final String REGISTER = "/register"; + public static final String FULL_REGISTER = FULL_URL + REGISTER; + public static final String ADDPOST = "/addPost"; + public static final String FULL_ADDPOST = FULL_URL + ADDPOST; + public static final String WELCOME = "/welcome"; + public static final String FULL_WELCOME = FULL_URL + WELCOME; + public static final String ABOUT = "/about"; + public static final String FULL_ABOUT = FULL_URL + ABOUT; +} diff --git a/Shevtsov/pom.xml b/Shevtsov/pom.xml new file mode 100644 index 0000000..6189219 --- /dev/null +++ b/Shevtsov/pom.xml @@ -0,0 +1,17 @@ + + 4.0.0 + + com.example + blog + 1.0-SNAPSHOT + pom + + + blogsources + TestingFramework + URLMapping + + \ No newline at end of file