Add template for assignment 2
This commit is contained in:
@@ -0,0 +1,72 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
|
||||
<parent>
|
||||
<groupId>at.ac.tuwien.infosys.dst</groupId>
|
||||
<artifactId>dst</artifactId>
|
||||
<version>2021.1</version>
|
||||
<relativePath>../..</relativePath>
|
||||
</parent>
|
||||
|
||||
<artifactId>ass2-service-auth-client</artifactId>
|
||||
|
||||
<name>DST :: Assignment 2 :: Service :: Auth Client</name>
|
||||
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>at.ac.tuwien.infosys.dst</groupId>
|
||||
<artifactId>ass2-service-api</artifactId>
|
||||
<version>${project.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>io.grpc</groupId>
|
||||
<artifactId>grpc-protobuf</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.grpc</groupId>
|
||||
<artifactId>grpc-stub</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.grpc</groupId>
|
||||
<artifactId>grpc-netty</artifactId>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>at.ac.tuwien.infosys.dst</groupId>
|
||||
<artifactId>ass2-service-auth</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>at.ac.tuwien.infosys.dst</groupId>
|
||||
<artifactId>ass1-jpa</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<type>test-jar</type>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>at.ac.tuwien.infosys.dst</groupId>
|
||||
<artifactId>ass2-service-auth</artifactId>
|
||||
<version>${project.version}</version>
|
||||
<type>test-jar</type>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework</groupId>
|
||||
<artifactId>spring-orm</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
</project>
|
||||
+37
@@ -0,0 +1,37 @@
|
||||
package dst.ass2.service.auth.client;
|
||||
|
||||
/**
|
||||
* This class holds the host and port value used to connect to the gRPC server. The CDI context provides an instance
|
||||
* that you can inject into your implementation of {@link IAuthenticationClient}. The config values are loaded from the
|
||||
* application.properties file.
|
||||
*/
|
||||
public class AuthenticationClientProperties {
|
||||
|
||||
private String host;
|
||||
private int port;
|
||||
|
||||
public AuthenticationClientProperties() {
|
||||
|
||||
}
|
||||
|
||||
public AuthenticationClientProperties(String host, int port) {
|
||||
this.host = host;
|
||||
this.port = port;
|
||||
}
|
||||
|
||||
public String getHost() {
|
||||
return host;
|
||||
}
|
||||
|
||||
public void setHost(String host) {
|
||||
this.host = host;
|
||||
}
|
||||
|
||||
public int getPort() {
|
||||
return port;
|
||||
}
|
||||
|
||||
public void setPort(int port) {
|
||||
this.port = port;
|
||||
}
|
||||
}
|
||||
+17
@@ -0,0 +1,17 @@
|
||||
package dst.ass2.service.auth.client;
|
||||
|
||||
import dst.ass2.service.api.auth.AuthenticationException;
|
||||
import dst.ass2.service.api.auth.NoSuchUserException;
|
||||
|
||||
public interface IAuthenticationClient extends AutoCloseable {
|
||||
|
||||
String authenticate(String email, String password) throws NoSuchUserException, AuthenticationException;
|
||||
|
||||
boolean isTokenValid(String token);
|
||||
|
||||
/**
|
||||
* Shuts down any underlying resource required to maintain this client.
|
||||
*/
|
||||
@Override
|
||||
void close();
|
||||
}
|
||||
+32
@@ -0,0 +1,32 @@
|
||||
package dst.ass2.service.auth.client.impl;
|
||||
|
||||
import dst.ass2.service.api.auth.AuthenticationException;
|
||||
import dst.ass2.service.api.auth.NoSuchUserException;
|
||||
import dst.ass2.service.auth.client.AuthenticationClientProperties;
|
||||
import dst.ass2.service.auth.client.IAuthenticationClient;
|
||||
|
||||
public class GrpcAuthenticationClient implements IAuthenticationClient {
|
||||
|
||||
// TODO make use of the generated grpc sources to implement a blocking client
|
||||
|
||||
public GrpcAuthenticationClient(AuthenticationClientProperties properties) {
|
||||
// TODO
|
||||
}
|
||||
|
||||
@Override
|
||||
public String authenticate(String email, String password) throws NoSuchUserException, AuthenticationException {
|
||||
// TODO
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean isTokenValid(String token) {
|
||||
// TODO
|
||||
return false;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void close() {
|
||||
// TODO
|
||||
}
|
||||
}
|
||||
+76
@@ -0,0 +1,76 @@
|
||||
package dst.ass2.service.auth.client;
|
||||
|
||||
import static org.junit.Assert.assertFalse;
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.ActiveProfiles;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
import dst.ass1.jpa.tests.TestData;
|
||||
import dst.ass2.service.api.auth.AuthenticationException;
|
||||
import dst.ass2.service.api.auth.NoSuchUserException;
|
||||
import dst.ass2.service.auth.AuthenticationServiceApplication;
|
||||
import dst.ass2.service.auth.client.impl.GrpcAuthenticationClient;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest(classes = AuthenticationServiceApplication.class)
|
||||
@Transactional
|
||||
@ActiveProfiles({"testdata", "grpc"})
|
||||
public class AuthenticationClientTest {
|
||||
|
||||
@Value("${grpc.port}")
|
||||
private int port;
|
||||
|
||||
private IAuthenticationClient client;
|
||||
|
||||
@Before
|
||||
public void setUp() throws Exception {
|
||||
AuthenticationClientProperties properties = new AuthenticationClientProperties("localhost", port);
|
||||
client = new GrpcAuthenticationClient(properties);
|
||||
}
|
||||
|
||||
@After
|
||||
public void tearDown() throws Exception {
|
||||
client.close();
|
||||
}
|
||||
|
||||
@Test(expected = NoSuchUserException.class)
|
||||
public void authenticate_invalidUser_throwsException() throws Exception {
|
||||
client.authenticate("nonexisting@example.com", "foo");
|
||||
}
|
||||
|
||||
@Test(expected = AuthenticationException.class)
|
||||
public void authenticate_invalidPassword_throwsException() throws Exception {
|
||||
client.authenticate(TestData.RIDER_1_EMAIL, "foo");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void authenticate_existingUser_returnsToken() throws Exception {
|
||||
String token = client.authenticate(TestData.RIDER_1_EMAIL, TestData.RIDER_1_PW);
|
||||
assertNotNull(token);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isTokenValid_invalidToken_returnsFalse() throws Exception {
|
||||
boolean valid = client.isTokenValid(UUID.randomUUID().toString()); // should be false in *almost* all cases ;-)
|
||||
assertFalse(valid);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void isTokenValid_onCreatedToken_returnsTrue() throws Exception {
|
||||
String token = client.authenticate(TestData.RIDER_2_EMAIL, TestData.RIDER_2_PW);
|
||||
boolean valid = client.isTokenValid(token);
|
||||
assertTrue(valid);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user