Skip to content

Commit

Permalink
Merge pull request #48 from eurohlam/web-test
Browse files Browse the repository at this point in the history
Automated web test
  • Loading branch information
eurohlam authored Sep 17, 2019
2 parents 5569c25 + 38f2c45 commit c4401db
Show file tree
Hide file tree
Showing 57 changed files with 2,617 additions and 141 deletions.
6 changes: 5 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ target
*/log
*logs
*data
misc
*allure-results
.allure
docker-dir/tomcat/resources/*.war
.DS_Store
kindle/kindlegen.exe
kindle/kindlegen.exe
kindle/kindlegen
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
<junit.version>4.11</junit.version>
<mockito.version>2.1.0-RC.1</mockito.version>
<testng.version>6.9.8</testng.version>
<javafaker.version>0.17.2</javafaker.version>
<!-- Maven plugins versions -->
<resources.plugin.version>3.0.2</resources.plugin.version>
<compiler.plugin.version>3.6.1</compiler.plugin.version>
Expand All @@ -43,6 +44,7 @@
<module>rss-2-kindle-rest-api</module>
<module>rss-2-kindle-web</module>
<module>docker-dir</module>
<module>rss-2-kindle-web-test</module>
</modules>

<dependencies>
Expand Down Expand Up @@ -105,6 +107,11 @@
<version>${mockito.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>${javafaker.version}</version>
</dependency>
</dependencies>

<build>
Expand All @@ -128,6 +135,11 @@
<encoding>UTF-8</encoding>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>3.0.0-M3</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-checkstyle-plugin</artifactId>
Expand Down
5 changes: 2 additions & 3 deletions rss-2-kindle-camel/src/test/resources/log4j.properties
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,9 @@ log4j.rootLogger=DEBUG, out
# CONSOLE appender not used by default
log4j.appender.out=org.apache.log4j.ConsoleAppender
log4j.appender.out.layout=org.apache.log4j.PatternLayout
log4j.appender.out.layout.ConversionPattern=[%30.30t] %-30.30c{1} %-5p %m%n
#log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
log4j.appender.out.layout.ConversionPattern=[%p][%t] <%c{1}> - %m%n

log4j.logger.org.apache.camel.component.file.remote=TRACE
log4j.logger.org.apache.camel.component.file.remote=INFO

log4j.logger.org.springframework=WARN, SPRING
log4j.appender.SPRING=org.apache.log4j.RollingFileAppender
Expand Down
1 change: 0 additions & 1 deletion rss-2-kindle-datasource-mongo/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.20</version>
<configuration>
<skipTests>true</skipTests>
</configuration>
Expand Down
7 changes: 6 additions & 1 deletion rss-2-kindle-rest-api/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,12 @@
<version>2.26-b09</version>
<scope>test</scope>
</dependency>

<dependency>
<groupId>com.github.javafaker</groupId>
<artifactId>javafaker</artifactId>
<version>${javafaker.version}</version>
<scope>test</scope>
</dependency>
</dependencies>

<profiles>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* Created by eurohlam on 30.08.17.
*/
@Service
@Path("profile/{username: [a-zA-Z][a-zA-Z_0-9]*}")
@Path("profile/{username: [a-zA-Z_.0-9]*}")
public class ProfileManager {

private final Logger logger = LoggerFactory.getLogger(ProfileManager.class);
Expand All @@ -34,6 +36,8 @@ public class ProfileManager {

private ModelFactory modelFactory = new ModelFactory();

private List<String> validationErrors = Collections.EMPTY_LIST;

@GET
@Produces(MediaType.APPLICATION_JSON)
public Response getUserDetails(@PathParam("username") String username) {
Expand Down Expand Up @@ -62,7 +66,7 @@ public Response getAllSubscribers(@PathParam("username") String username) {
}

@GET
@Path("/{email: \\w+@\\w+\\.[a-zA-Z]{2,}}")
@Path("/{email: [\\w\\.]+@[\\w\\.]+}")
@Produces(MediaType.APPLICATION_JSON)
public Response getSubscriber(@PathParam("username") String username, @PathParam("email") String subscriberId) {
logger.debug("Fetch subscriber {} for user {}", subscriberId, username);
Expand All @@ -76,7 +80,7 @@ public Response getSubscriber(@PathParam("username") String username, @PathParam
}

@GET
@Path("/{email: \\w+@\\w+\\.[a-zA-Z]{2,}}/suspend")
@Path("/{email: [\\w\\.]+@[\\w\\.]+}/suspend")
@Produces(MediaType.APPLICATION_JSON)
public Response suspendSubscriber(@PathParam("username") String username, @PathParam("email") String subscriberId) {
logger.warn("Suspend subscriber {} for user {}", subscriberId, username);
Expand All @@ -94,7 +98,7 @@ public Response suspendSubscriber(@PathParam("username") String username, @PathP
}

@GET
@Path("/{email: \\w+@\\w+\\.[a-zA-Z]{2,}}/resume")
@Path("/{email: [\\w\\.]+@[\\w\\.]+}/resume")
@Produces(MediaType.APPLICATION_JSON)
public Response resumeSubscriber(@PathParam("username") String username, @PathParam("email") String subscriberId) {
logger.warn("Resume subscriber {} for user {}", subscriberId, username);
Expand All @@ -121,7 +125,11 @@ public Response addSubscriber(@PathParam("username") String username,
@FormParam("rss") String rss) {
logger.info("Add new subscriber {} for user {}", email, username);
try {
OperationResult result = subscriberRepository.addSubscriber(username, modelFactory.newSubscriber(email, name, rss));
Subscriber subscriber = modelFactory.newSubscriber(email, name, rss);
if (!isValid(subscriber)) {
return Response.status(Response.Status.BAD_REQUEST.getStatusCode(), validationErrors.toString()).build();
}
OperationResult result = subscriberRepository.addSubscriber(username, subscriber);
if (result == OperationResult.SUCCESS) {
return Response.ok(result.toJson(), MediaType.APPLICATION_JSON_TYPE).build();
} else {
Expand All @@ -142,6 +150,9 @@ public Response addSubscriber(@PathParam("username") String username,
logger.info("Requested to add a new subscriber for user {} with data {}", username, message);
try {
Subscriber subscriber = modelFactory.json2Pojo(Subscriber.class, message);
if (!isValid(subscriber)) {
return Response.status(Response.Status.BAD_REQUEST.getStatusCode(), validationErrors.toString()).build();
}
OperationResult result = subscriberRepository.addSubscriber(username, subscriber);
if (result == OperationResult.SUCCESS) {
return Response.ok(result.toJson(), MediaType.APPLICATION_JSON_TYPE).build();
Expand All @@ -168,7 +179,11 @@ public Response updateSubscriber(@PathParam("username") String username,
@FormParam("rss") String rss) {
logger.warn("Update existing subscriber {} for user {}", email, username);
try {
OperationResult result = subscriberRepository.updateSubscriber(username, modelFactory.newSubscriber(email, name, rss));
Subscriber subscriber = modelFactory.newSubscriber(email, name, rss);
if (!isValid(subscriber)) {
return Response.status(Response.Status.BAD_REQUEST.getStatusCode(), validationErrors.toString()).build();
}
OperationResult result = subscriberRepository.updateSubscriber(username, subscriber);
if (result == OperationResult.SUCCESS) {
return Response.ok(result.toJson(), MediaType.APPLICATION_JSON_TYPE).build();
} else {
Expand All @@ -188,6 +203,9 @@ public Response updateSubscriber(@PathParam("username") String username, String
logger.warn("Requested to update existing subscriber for user {} with data {}", username, message);
try {
Subscriber subscriber = modelFactory.json2Pojo(Subscriber.class, message);
if (!isValid(subscriber)) {
return Response.status(Response.Status.BAD_REQUEST.getStatusCode(), validationErrors.toString()).build();
}
OperationResult result = subscriberRepository.updateSubscriber(username, subscriber);
if (result == OperationResult.SUCCESS) {
return Response.ok(result.toJson(), MediaType.APPLICATION_JSON_TYPE).build();
Expand All @@ -201,7 +219,7 @@ public Response updateSubscriber(@PathParam("username") String username, String
}

@DELETE
@Path("/{email: \\w+@\\w+\\.[a-zA-Z]{2,}}/remove")
@Path("/{email: [\\w\\.]+@[\\w\\.]+}/remove")
@Produces(MediaType.APPLICATION_JSON)
public Response removeSubscriber(@PathParam("username") String username, @PathParam("email") String subscriberId) {
logger.warn("Remove subscriber {} for user {}", subscriberId, username);
Expand All @@ -219,7 +237,7 @@ public Response removeSubscriber(@PathParam("username") String username, @PathPa
}

@GET
@Path("/{email: \\w+@\\w+\\.[a-zA-Z]{2,}}/subscriptions")
@Path("/{email: [\\w\\.]+@[\\w\\.]+}/subscriptions")
@Produces(MediaType.APPLICATION_JSON)
public Response getAllSubscriptions(@PathParam("username") String username, @PathParam("email") String subscriberId) {
logger.debug("Fetch all subscriptions for subscriber {} by user {}", subscriberId, username);
Expand All @@ -234,7 +252,7 @@ public Response getAllSubscriptions(@PathParam("username") String username, @Pat
}

@POST
@Path("/{email: \\w+@\\w+\\.[a-zA-Z]{2,}}/subscribe")
@Path("/{email: [\\w\\.]+@[\\w\\.]+}/subscribe")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
public Response addSubscription(@PathParam("username") String username,
Expand All @@ -245,7 +263,7 @@ public Response addSubscription(@PathParam("username") String username,
Subscriber subscriber = subscriberRepository.getSubscriber(username, subscriberId);
for (Rss r : subscriber.getRsslist()) {
if (r.getRss().equals(rss)) {
return Response.status(Response.Status.CONFLICT).build();
return Response.status(Response.Status.CONFLICT.getStatusCode(), "Duplicated subscription").build();
}
}

Expand All @@ -266,7 +284,7 @@ public Response addSubscription(@PathParam("username") String username,
}

@POST
@Path("/{email: \\w+@\\w+\\.[a-zA-Z]{2,}}/unsubscribe")
@Path("/{email: [\\w\\.]+@[\\w\\.]+}/unsubscribe")
@Consumes(MediaType.APPLICATION_FORM_URLENCODED)
@Produces(MediaType.APPLICATION_JSON)
public Response removeSubscription(@PathParam("username") String username,
Expand Down Expand Up @@ -295,4 +313,42 @@ public Response removeSubscription(@PathParam("username") String username,
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).build();
}
}

private boolean isValid(Subscriber subscriber) {
List<String> errors = new ArrayList<>();
boolean isValid = true;
if (subscriber.getName().isEmpty()) {
errors.add("subscriber's name must not be empty");
isValid = false;
}
if (subscriber.getEmail().isEmpty()) {
errors.add("subscriber's email must not be empty");
isValid = false;
}
if (subscriber.getRsslist().size() < 1) {
errors.add("subscriber must have at least 1 subscription");
isValid = false;
}
if (!subscriber.getEmail().matches("[\\w.]+@[\\w.]+")) {
errors.add("email contains unexpected symbols. It must satisfy the mask [\\w.]+@[\\w.]+");
isValid = false;
}
for (Rss rss: subscriber.getRsslist()) {
if (rss.getRss().isEmpty()) {
errors.add("subscription must not be empty");
isValid = false;
}
String pattern = "https?:\\/\\/(www\\.)?[-a-zA-Z0-9@:%._\\+~#=]{2,256}\\.[a-z]{2,6}\\b([-a-zA-Z0-9@:%_\\+.~#?&//=]*)";
if (!rss.getRss().matches(pattern)) {
errors.add("incorrect URL for subscription: " + rss.getRss());
isValid = false;
}
}
if (isValid) {
validationErrors = Collections.EMPTY_LIST;
} else {
validationErrors = errors;
}
return isValid;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class ServiceManager {
private SubscriberRepository subscriberRepository;

@GET
@Path("/{email: \\w+@\\w+\\.[a-zA-Z]{2,}}")
@Path("/{email: [\\w\\.]+@[\\w\\.]+}")
@Produces(MediaType.APPLICATION_JSON)
public Response runRssPollingForSubscriber(@PathParam("username") String username, @PathParam("email") String id) {
logger.info("Run RSS polling by demand: user {}, subscriber {}", username, id);
Expand Down
Loading

0 comments on commit c4401db

Please sign in to comment.