Skip to content

Commit

Permalink
Initial commit, with the main basic feature and some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
raulexposito committed Jul 17, 2015
0 parents commit 5ba7c36
Show file tree
Hide file tree
Showing 10 changed files with 614 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
target/
.idea/
*.iml
9 changes: 9 additions & 0 deletions README.md
@@ -0,0 +1,9 @@
# YAUS - Yet Another URL Shortener

Implementation of a simple URL Shortener.

##Branches:

* [development](https://github.com/raulexposito/yaus/tree/development) (work in progress)
* [master](https://github.com/raulexposito/yaus/tree/master) (releases)

295 changes: 295 additions & 0 deletions lib/formatter.xml

Large diffs are not rendered by default.

31 changes: 31 additions & 0 deletions lib/pom.xml
@@ -0,0 +1,31 @@
<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/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>persistence</artifactId>
<packaging>jar</packaging>
<name>yaus - lib</name>

<parent>
<groupId>com.raulexposito.yaus</groupId>
<artifactId>yaus</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>

<dependencies>
<!-- testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
</dependency>

<!-- third party -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
</dependency>
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,8 @@
package com.raulexposito.yaus.shortener;

public class InvalidURLException extends Exception {

public InvalidURLException(String message) {
super(message);
}
}
@@ -0,0 +1,23 @@
package com.raulexposito.yaus.shortener;

import org.apache.commons.codec.digest.DigestUtils;

public class UrlShortener {

private static final int BEGINNING = 0;
static final int MAX_LENGTH = 8;

private Validator validator = new Validator();

public String generate(final String url) throws InvalidURLException {

int i = 0;

validator.validate(url);
return limit(DigestUtils.sha1Hex(url));
}

private String limit(final String hash) {
return hash.substring(BEGINNING, MAX_LENGTH);
}
}
17 changes: 17 additions & 0 deletions lib/src/main/java/com/raulexposito/yaus/shortener/Validator.java
@@ -0,0 +1,17 @@
package com.raulexposito.yaus.shortener;

import org.apache.commons.validator.routines.UrlValidator;

class Validator {

private static final String[] VALID_PROTOCOLS = { "http", "https" };
private static final boolean IS_VALID = true;

private final UrlValidator urlValidator = new UrlValidator(VALID_PROTOCOLS);

public boolean validate(final String url) throws InvalidURLException {
if (!urlValidator.isValid(url))
throw new InvalidURLException("The URL '" + url + "' is not valid");
return IS_VALID;
}
}
@@ -0,0 +1,33 @@
package com.raulexposito.yaus.shortener;

import org.junit.Assert;
import org.junit.Test;

public class UrlShortenerTest {

private UrlShortener urlShortener = new UrlShortener();

// http://www.sha1-online.com/
// http://raulexposito.com -> 9cc810cdab40aae66568bcb2a0397a0ceb50f9b1
// http://testingdomain.com -> 7862655dd0ae9820157ba01b9e300579beb77045
@Test
public void testMyDomainURL() throws InvalidURLException {
Assert.assertEquals("The (limited) hash of 'http://raulexposito.com' must be '9cc810cd'",
"9cc810cd",
urlShortener.generate("http://raulexposito.com"));
}

@Test
public void testOsocoDomainURL() throws InvalidURLException {
Assert.assertEquals("The (limited) hash of 'http://osoco.es' must be '1f206102'",
"7862655d",
urlShortener.generate("http://testingdomain.com"));
}

@Test
public void testHashesHave8CharsLength() throws InvalidURLException {
Assert.assertEquals("The (limited) hash must have a length of 8 chars",
UrlShortener.MAX_LENGTH,
urlShortener.generate("http://testingdomain.com").length());
}
}
@@ -0,0 +1,49 @@
package com.raulexposito.yaus.shortener;

import org.junit.Assert;
import org.junit.Test;

public class ValidatorTest {

private Validator validator = new Validator();

@Test
public void checkHttpURLIsValid() throws InvalidURLException {
Assert.assertTrue(validator.validate("http://testingdomain.com"));
}

@Test
public void checkHttpURLWithOneParameterIsValid() throws InvalidURLException {
Assert.assertTrue(validator.validate("http://testingdomain.com?param1=value"));
}

@Test
public void checkHttpURLWithTwoParametersIsValid() throws InvalidURLException {
Assert.assertTrue(validator.validate("http://testingdomain.com?param1=value&param2=value"));
}

@Test
public void checkHttpsURLIsValid() throws InvalidURLException {
Assert.assertTrue(validator.validate("https://testingdomain.com"));
}

@Test (expected = InvalidURLException.class)
public void checkURLAnotherProtocolIsInvalid() throws InvalidURLException {
validator.validate("ftp://testingdomain.com");
}

@Test (expected = InvalidURLException.class)
public void checkURLWithoutProtocolIsInvalid() throws InvalidURLException {
validator.validate("testingdomain.com");
}

@Test (expected = InvalidURLException.class)
public void checkURLWithoutHostIsInvalid() throws InvalidURLException {
validator.validate("http://.com");
}

@Test (expected = InvalidURLException.class)
public void checkURLWithoutDomainIsInvalid() throws InvalidURLException {
validator.validate("http://testingdomain");
}
}
146 changes: 146 additions & 0 deletions pom.xml
@@ -0,0 +1,146 @@
<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>

<groupId>com.raulexposito.yaus</groupId>
<artifactId>yaus</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>pom</packaging>
<name>yaus</name>

<modules>
<module>lib</module>
</modules>

<developers>
<developer>
<id>jdoe</id>
<name>Raúl Expósito</name>
<url>http://raulexposito.com/</url>
</developer>
</developers>

<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<java.version>1.7</java.version>

<!-- ::::::::::::::::::::::::::::::::::::::::::::: -->
<!-- PLUGINS -->
<!-- ::::::::::::::::::::::::::::::::::::::::::::: -->

<maven.compiler.plugin.version>3.3</maven.compiler.plugin.version>
<jacoco.plugin.version>0.7.5.201505241946</jacoco.plugin.version>
<maven.java.formatter.plugin.version>0.4</maven.java.formatter.plugin.version>
<maven.pmd.plugin.version>3.5</maven.pmd.plugin.version>

<!-- ::::::::::::::::::::::::::::::::::::::::::::: -->
<!-- DEPENDENCIES -->
<!-- ::::::::::::::::::::::::::::::::::::::::::::: -->

<!-- Testing -->
<junit.version>4.12</junit.version>

<!-- Third party -->
<commons-codec.version>1.10</commons-codec.version>
<commons-validator.version>1.4.1</commons-validator.version>
</properties>

<build>
<plugins>
<!-- java compilation -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven.compiler.plugin.version}</version>
<configuration>
<compilerVersion>${java.version}</compilerVersion>
<source>${java.version}</source>
<target>${java.version}</target>
</configuration>
</plugin>

<!-- test code coverage -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.plugin.version}</version>
<configuration>
<datafile>${basedir}/target/coverage-reports/jacoco-unit.exec</datafile>
</configuration>
<executions>
<execution>
<id>jacoco-initialize</id>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<execution>
<id>jacoco-site</id>
<phase>test</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>

<!-- java code formatter -->
<plugin>
<groupId>com.googlecode.maven-java-formatter-plugin</groupId>
<artifactId>maven-java-formatter-plugin</artifactId>
<version>${maven.java.formatter.plugin.version}</version>
<configuration>
<configFile>${project.basedir}/formatter.xml</configFile>
</configuration>
<executions>
<execution>
<id>formatter</id>
<phase>compile</phase>
<goals>
<goal>format</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
<reporting>
<plugins>
<!-- static code analysis. Includes CPD and PMD -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-pmd-plugin</artifactId>
<version>${maven.pmd.plugin.version}</version>
<configuration>
<linkXref>true</linkXref>
<sourceEncoding>utf-8</sourceEncoding>
<minimumTokens>70</minimumTokens>
</configuration>
</plugin>
</plugins>
</reporting>

<dependencyManagement>
<dependencies>
<!-- testing -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency>

<!-- third party -->
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>${commons-codec.version}</version>
</dependency>
<dependency>
<groupId>commons-validator</groupId>
<artifactId>commons-validator</artifactId>
<version>${commons-validator.version}</version>
</dependency>
</dependencies>
</dependencyManagement>
</project>

0 comments on commit 5ba7c36

Please sign in to comment.