initial commit for having a backup
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
import java.net.MalformedURLException;
|
||||
import java.util.Set;
|
||||
import javax.servlet.ServletException;
|
||||
import org.apache.catalina.Context;
|
||||
import org.apache.catalina.LifecycleException;
|
||||
import org.apache.catalina.startup.Tomcat;
|
||||
import org.apache.commons.io.FileUtils;
|
||||
import org.apache.tomcat.util.scan.StandardJarScanner;
|
||||
import org.glassfish.jersey.server.ResourceConfig;
|
||||
import org.glassfish.jersey.servlet.ServletContainer;
|
||||
|
||||
public class EmbeddedTomcat {
|
||||
|
||||
private final static String TMP_DIR = System.getProperty("java.io.tmpdir")+"/EmbeddedTomcat";
|
||||
|
||||
Tomcat tomcat;
|
||||
|
||||
public void start(int port,String appContext) throws ServletException,
|
||||
MalformedURLException,
|
||||
LifecycleException {
|
||||
String webAppLocation = "src/main/webapp/";
|
||||
tomcat = new Tomcat();
|
||||
tomcat.setBaseDir(TMP_DIR);
|
||||
tomcat.setPort(port);
|
||||
|
||||
// Define a web application context.
|
||||
Context context = tomcat.addWebapp(appContext, new File(webAppLocation).getAbsolutePath());
|
||||
((StandardJarScanner) context.getJarScanner()).setScanManifest(false);
|
||||
|
||||
// Add servlet that will register Jersey REST resources
|
||||
Tomcat.addServlet(context, "jersey-container-servlet", resourceConfig());
|
||||
context.addServletMappingDecoded("/api/*", "jersey-container-servlet");
|
||||
tomcat.start();
|
||||
//Don't comment the line below in unless you know what you do. It will block here, waiting for the server to stop.
|
||||
//This can be usefull for testing the embedded server from a browser or postman
|
||||
//tomcat.getServer().await();
|
||||
}
|
||||
|
||||
private ServletContainer resourceConfig() {
|
||||
//Load the Wizard-generated rest.ApplicationConfig - class
|
||||
Set<Class<?>> resources = new rest.ApplicationConfig().getClasses();
|
||||
System.out.println("Loaded Classes Count: "+resources.size());
|
||||
return new ServletContainer(new ResourceConfig(resources));
|
||||
}
|
||||
|
||||
public void stop() {
|
||||
try {
|
||||
tomcat.stop();
|
||||
tomcat.destroy();
|
||||
//Delete our embedded Tomcat's temp
|
||||
FileUtils.deleteDirectory(new File(TMP_DIR));
|
||||
}
|
||||
catch (IOException | LifecycleException ex) {
|
||||
throw new RuntimeException(ex);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
package accepttests;
|
||||
|
||||
import io.restassured.RestAssured;
|
||||
import static io.restassured.RestAssured.given;
|
||||
import io.restassured.parsing.Parser;
|
||||
import java.net.MalformedURLException;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import javax.servlet.ServletException;
|
||||
import org.apache.catalina.LifecycleException;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
|
||||
public class AcceptTestDemo {
|
||||
private static final String APP_CONTEXT = "/jwtbackend"; //IMPORTANT--> this should reflect the value in META-INF/context.xml
|
||||
private static EntityManagerFactory emf;
|
||||
|
||||
@Test
|
||||
public void serverIsRunning() {
|
||||
System.out.println("TESTING IF SERVER IS RUNNING");
|
||||
given().when().get().then().statusCode(200);
|
||||
}
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpBeforeAll() throws LifecycleException, ServletException, MalformedURLException {
|
||||
RestAssured.baseURI = "https://deploy.mydemos.dk/";
|
||||
//RestAssured.port = 80;
|
||||
//RestAssured.basePath = APP_CONTEXT;
|
||||
RestAssured.defaultParser = Parser.JSON;
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDownAfterAll() throws LifecycleException {
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,131 @@
|
||||
package integrationtests;
|
||||
|
||||
import org.junit.BeforeClass;
|
||||
import io.restassured.RestAssured;
|
||||
import static io.restassured.RestAssured.*;
|
||||
import io.restassured.http.ContentType;
|
||||
import io.restassured.parsing.Parser;
|
||||
import java.net.MalformedURLException;
|
||||
import javax.servlet.ServletException;
|
||||
import org.apache.catalina.LifecycleException;
|
||||
import static org.hamcrest.Matchers.*;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.Test;
|
||||
import utils.PuSelector;
|
||||
|
||||
public class IntegrationTest {
|
||||
/*
|
||||
protected static int SERVER_PORT;
|
||||
protected static String APP_CONTEXT;
|
||||
protected static String SERVER_URL;
|
||||
|
||||
public IntegrationTest() {
|
||||
}
|
||||
|
||||
private static EmbeddedTomcat tomcat;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpBeforeAll() throws ServletException, MalformedURLException, LifecycleException {
|
||||
System.out.println("INTEGRATION TEST");
|
||||
TestUtils.setupTestUsers(PuSelector.getEntityManagerFactory("pu_integration_test"));
|
||||
SERVER_PORT = 7777;
|
||||
APP_CONTEXT = "/restbackend";
|
||||
SERVER_URL = "http://localhost";
|
||||
|
||||
//Setup and start Embedded Tomcat for testing
|
||||
tomcat = new EmbeddedTomcat();
|
||||
tomcat.start(SERVER_PORT, APP_CONTEXT);
|
||||
|
||||
//Setup RestAssured
|
||||
RestAssured.baseURI = SERVER_URL;
|
||||
RestAssured.port = SERVER_PORT;
|
||||
RestAssured.basePath = APP_CONTEXT;
|
||||
RestAssured.defaultParser = Parser.JSON;
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDownAfterAll() {
|
||||
tomcat.stop();
|
||||
}
|
||||
|
||||
//This is how we hold on to the token after login, similar to that a client must store the token somewhere
|
||||
private static String securityToken;
|
||||
|
||||
//Utility method to login and set the returned securityToken
|
||||
private static void login(String role, String password) {
|
||||
String json = String.format("{username: \"%s\", password: \"%s\"}", role, password);
|
||||
securityToken = given()
|
||||
.contentType("application/json")
|
||||
.body(json)
|
||||
.when().post("/api/login")
|
||||
.then()
|
||||
.extract().path("token");
|
||||
}
|
||||
|
||||
private void logOut() {
|
||||
securityToken = null;
|
||||
}
|
||||
|
||||
@Test
|
||||
public void serverIsRunning() {
|
||||
System.out.println("Testing is server UP");
|
||||
given().when().get("/").then().statusCode(200);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRestNoAuthenticationRequired() {
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.when()
|
||||
.get("/api/info").then()
|
||||
.statusCode(200)
|
||||
.body("msg", equalTo("Hello anonymous"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRestForAdmin() {
|
||||
login("admin", "test");
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.accept(ContentType.JSON)
|
||||
.header("x-access-token", securityToken)
|
||||
.when()
|
||||
.get("/api/info/admin").then()
|
||||
.statusCode(200)
|
||||
.body("msg", equalTo("Hello to (admin) User: admin"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testRestForUser() {
|
||||
login("user", "test");
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.header("x-access-token", securityToken)
|
||||
.when()
|
||||
.get("/api/info/user").then()
|
||||
.statusCode(200)
|
||||
.body("msg", equalTo("Hello to User: user"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void userNotAuthenticated() {
|
||||
logOut();
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.when()
|
||||
.get("/api/info/user").then()
|
||||
.statusCode(403);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void adminNotAuthenticated() {
|
||||
logOut();
|
||||
given()
|
||||
.contentType("application/json")
|
||||
.when()
|
||||
.get("/api/info/user").then()
|
||||
.statusCode(403);
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
@@ -0,0 +1,39 @@
|
||||
package unittest;
|
||||
|
||||
import exceptions.AuthenticationException;
|
||||
import javax.persistence.EntityManagerFactory;
|
||||
import org.junit.AfterClass;
|
||||
import org.junit.BeforeClass;
|
||||
import org.junit.Test;
|
||||
import static org.junit.Assert.*;
|
||||
import utils.PuSelector;
|
||||
|
||||
public class TestUsers {
|
||||
/*
|
||||
private static UserFacade facade;
|
||||
|
||||
@BeforeClass
|
||||
public static void setUpClass() {
|
||||
EntityManagerFactory emf = PuSelector.getEntityManagerFactory("pu_unit_test_mock");
|
||||
facade = UserFacade.getInstance(emf);
|
||||
TestUtils.setupTestUsers(emf);
|
||||
}
|
||||
|
||||
@AfterClass
|
||||
public static void tearDownClass() {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getUserValid() throws AuthenticationException {
|
||||
User u = facade.getVeryfiedUser("user", "test");
|
||||
assertEquals("user", u.getUserName());
|
||||
}
|
||||
|
||||
@Test(expected = AuthenticationException.class)
|
||||
public void getUserInValid() throws AuthenticationException {
|
||||
User u = facade.getVeryfiedUser("user", "testxxxx");
|
||||
assertEquals("user", u.getUserName());
|
||||
}
|
||||
*/
|
||||
|
||||
}
|
||||
@@ -0,0 +1,6 @@
|
||||
INSERT INTO users (user_name, user_pass) VALUES ('user', 'test');
|
||||
INSERT INTO users (user_name, user_pass) VALUES ('admin', 'test');
|
||||
INSERT INTO roles (role_name) VALUES ('user');
|
||||
INSERT INTO roles (role_name) VALUES ('admin');
|
||||
INSERT INTO user_roles (user_name, role_name) VALUES ('user', 'admin');
|
||||
INSERT INTO user_roles (user_name, role_name) VALUES ('admin', 'admin');
|
||||
Reference in New Issue
Block a user