-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* initial poc * Remove unnecessary things for first iteration * add poc of cassandraRepository with fake imp of KGS and test :) * better key generator
- Loading branch information
Showing
18 changed files
with
333 additions
and
28 deletions.
There are no files selected for viewing
14 changes: 0 additions & 14 deletions
14
src/main/java/com/victuxbb/systemdesigns/tinyurlapi/TinyurlapiApplication.java
This file was deleted.
Oops, something went wrong.
21 changes: 21 additions & 0 deletions
21
src/main/java/com/victuxbb/systemdesigns/tinyurlapi/application/CreateShortURL.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
package com.victuxbb.systemdesigns.tinyurlapi.application; | ||
|
||
import com.victuxbb.systemdesigns.tinyurlapi.domain.shorturl.ShortURLRepository; | ||
import com.victuxbb.systemdesigns.tinyurlapi.domain.shorturl.ShortURL; | ||
import com.victuxbb.systemdesigns.tinyurlapi.domain.kgs.KGSRepository; | ||
import reactor.core.publisher.Mono; | ||
|
||
public class CreateShortURL { | ||
private final ShortURLRepository shortURLRepository; | ||
private final KGSRepository kgsRepository; | ||
|
||
public CreateShortURL(ShortURLRepository shortURLRepository, KGSRepository kgsRepository) { | ||
this.shortURLRepository = shortURLRepository; | ||
this.kgsRepository = kgsRepository; | ||
} | ||
|
||
public Mono<ShortURL> create(ShortURLRequest shortUrlRequest) { | ||
return kgsRepository.getUniqueKey() | ||
.flatMap(urlKey -> shortURLRepository.save(new ShortURL(urlKey.getKey(), shortUrlRequest.getOriginalURL()))); | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/victuxbb/systemdesigns/tinyurlapi/application/ShortURLRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.victuxbb.systemdesigns.tinyurlapi.application; | ||
|
||
public class ShortURLRequest { | ||
private String originalURL; | ||
|
||
public ShortURLRequest(String originalURL) { | ||
this.originalURL = originalURL; | ||
} | ||
|
||
public String getOriginalURL() { | ||
return originalURL; | ||
} | ||
} |
7 changes: 7 additions & 0 deletions
7
src/main/java/com/victuxbb/systemdesigns/tinyurlapi/domain/kgs/KGSRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package com.victuxbb.systemdesigns.tinyurlapi.domain.kgs; | ||
|
||
import reactor.core.publisher.Mono; | ||
|
||
public interface KGSRepository { | ||
Mono<URLKey> getUniqueKey(); | ||
} |
13 changes: 13 additions & 0 deletions
13
src/main/java/com/victuxbb/systemdesigns/tinyurlapi/domain/kgs/URLKey.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.victuxbb.systemdesigns.tinyurlapi.domain.kgs; | ||
|
||
public class URLKey { | ||
private String key; | ||
|
||
public URLKey(String key) { | ||
this.key = key; | ||
} | ||
|
||
public String getKey() { | ||
return key; | ||
} | ||
} |
33 changes: 33 additions & 0 deletions
33
src/main/java/com/victuxbb/systemdesigns/tinyurlapi/domain/shorturl/ShortURL.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package com.victuxbb.systemdesigns.tinyurlapi.domain.shorturl; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
public class ShortURL { | ||
private String hash; | ||
private String originalURL; | ||
private LocalDateTime creationDate; | ||
|
||
public ShortURL(String hash, String originalURL) { | ||
this.hash = hash; | ||
this.originalURL = originalURL; | ||
this.creationDate = LocalDateTime.now(); | ||
} | ||
|
||
public ShortURL(String hash, String originalURL, LocalDateTime creationDate) { | ||
this.hash = hash; | ||
this.originalURL = originalURL; | ||
this.creationDate = creationDate; | ||
} | ||
|
||
public String getHash() { | ||
return hash; | ||
} | ||
|
||
public String getOriginalURL() { | ||
return originalURL; | ||
} | ||
|
||
public LocalDateTime getCreationDate() { | ||
return creationDate; | ||
} | ||
} |
9 changes: 9 additions & 0 deletions
9
src/main/java/com/victuxbb/systemdesigns/tinyurlapi/domain/shorturl/ShortURLRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
package com.victuxbb.systemdesigns.tinyurlapi.domain.shorturl; | ||
|
||
import com.victuxbb.systemdesigns.tinyurlapi.domain.kgs.URLKey; | ||
import reactor.core.publisher.Mono; | ||
|
||
public interface ShortURLRepository { | ||
Mono<ShortURL> save(ShortURL url); | ||
Mono<Void> delete(URLKey urlKey); | ||
} |
16 changes: 16 additions & 0 deletions
16
...com/victuxbb/systemdesigns/tinyurlapi/infrastructure/bootstrap/TinyURLAPIApplication.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.victuxbb.systemdesigns.tinyurlapi.infrastructure.bootstrap; | ||
|
||
import org.springframework.boot.SpringApplication; | ||
import org.springframework.boot.autoconfigure.SpringBootApplication; | ||
|
||
@SpringBootApplication(scanBasePackages = { | ||
"com.victuxbb.systemdesigns.tinyurlapi.infrastructure" | ||
}) | ||
public class TinyURLAPIApplication { | ||
|
||
public static void main(String[] args) { | ||
SpringApplication.run(TinyURLAPIApplication.class, args); | ||
} | ||
|
||
} | ||
|
16 changes: 16 additions & 0 deletions
16
...emdesigns/tinyurlapi/infrastructure/bootstrap/configuration/ApplicationConfiguration.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.victuxbb.systemdesigns.tinyurlapi.infrastructure.bootstrap.configuration; | ||
|
||
import com.victuxbb.systemdesigns.tinyurlapi.application.CreateShortURL; | ||
import com.victuxbb.systemdesigns.tinyurlapi.domain.kgs.KGSRepository; | ||
import com.victuxbb.systemdesigns.tinyurlapi.domain.shorturl.ShortURLRepository; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
|
||
@Configuration | ||
public class ApplicationConfiguration { | ||
|
||
@Bean | ||
public CreateShortURL createShortURL(ShortURLRepository shortURLRepository, KGSRepository kgsRepository) { | ||
return new CreateShortURL(shortURLRepository, kgsRepository); | ||
} | ||
} |
19 changes: 19 additions & 0 deletions
19
...com/victuxbb/systemdesigns/tinyurlapi/infrastructure/controller/CreateTinyURLRequest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package com.victuxbb.systemdesigns.tinyurlapi.infrastructure.controller; | ||
|
||
import com.fasterxml.jackson.annotation.JsonCreator; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
|
||
public class CreateTinyURLRequest { | ||
private final String originalURL; | ||
|
||
@JsonCreator | ||
public CreateTinyURLRequest( | ||
@JsonProperty("originalURL") String originalURL | ||
) { | ||
this.originalURL = originalURL; | ||
} | ||
|
||
public String getOriginalURL() { | ||
return originalURL; | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...va/com/victuxbb/systemdesigns/tinyurlapi/infrastructure/controller/TinyURLController.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package com.victuxbb.systemdesigns.tinyurlapi.infrastructure.controller; | ||
|
||
import com.victuxbb.systemdesigns.tinyurlapi.application.CreateShortURL; | ||
import com.victuxbb.systemdesigns.tinyurlapi.application.ShortURLRequest; | ||
import com.victuxbb.systemdesigns.tinyurlapi.domain.shorturl.ShortURL; | ||
import org.springframework.web.bind.annotation.PostMapping; | ||
import org.springframework.web.bind.annotation.RequestBody; | ||
import org.springframework.web.bind.annotation.RestController; | ||
import reactor.core.publisher.Mono; | ||
|
||
@RestController | ||
|
||
public class TinyURLController { | ||
|
||
private final CreateShortURL createShortURL; | ||
|
||
public TinyURLController(CreateShortURL createShortURL) { | ||
this.createShortURL = createShortURL; | ||
} | ||
|
||
@PostMapping(path = "/tinyURL") | ||
public Mono<ShortURL> createTinyURL(@RequestBody CreateTinyURLRequest createTinyURLRequest) { | ||
return Mono.fromCallable(() -> new ShortURLRequest(createTinyURLRequest.getOriginalURL())) | ||
.flatMap(createShortURL::create); | ||
} | ||
} |
32 changes: 32 additions & 0 deletions
32
...tuxbb/systemdesigns/tinyurlapi/infrastructure/datasource/CassandraShortURLRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package com.victuxbb.systemdesigns.tinyurlapi.infrastructure.datasource; | ||
|
||
import com.victuxbb.systemdesigns.tinyurlapi.domain.kgs.URLKey; | ||
import com.victuxbb.systemdesigns.tinyurlapi.domain.shorturl.ShortURLRepository; | ||
import com.victuxbb.systemdesigns.tinyurlapi.domain.shorturl.ShortURL; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.data.cassandra.core.ReactiveCassandraOperations; | ||
import org.springframework.stereotype.Repository; | ||
import reactor.core.publisher.Mono; | ||
import sun.reflect.generics.reflectiveObjects.NotImplementedException; | ||
|
||
@Repository | ||
public class CassandraShortURLRepository implements ShortURLRepository { | ||
|
||
private final ReactiveCassandraOperations reactiveCassandraOperations; | ||
|
||
@Autowired | ||
public CassandraShortURLRepository(ReactiveCassandraOperations reactiveCassandraOperations) { | ||
this.reactiveCassandraOperations = reactiveCassandraOperations; | ||
} | ||
|
||
@Override | ||
public Mono<ShortURL> save(ShortURL shortUrl) { | ||
return reactiveCassandraOperations.insert(new URL(shortUrl)) | ||
.map(URL::toShortURL); | ||
} | ||
|
||
@Override | ||
public Mono<Void> delete(URLKey urlKey) { | ||
return Mono.error(new NotImplementedException()); | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...om/victuxbb/systemdesigns/tinyurlapi/infrastructure/datasource/InMemoryKGSRepository.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
package com.victuxbb.systemdesigns.tinyurlapi.infrastructure.datasource; | ||
|
||
import com.victuxbb.systemdesigns.tinyurlapi.domain.kgs.KGSRepository; | ||
import com.victuxbb.systemdesigns.tinyurlapi.domain.kgs.URLKey; | ||
import org.springframework.stereotype.Repository; | ||
import reactor.core.publisher.Mono; | ||
|
||
import java.util.UUID; | ||
|
||
@Repository | ||
public class InMemoryKGSRepository implements KGSRepository { | ||
@Override | ||
public Mono<URLKey> getUniqueKey() { | ||
return Mono.just(new URLKey(UUID.randomUUID().toString())); | ||
} | ||
} |
47 changes: 47 additions & 0 deletions
47
src/main/java/com/victuxbb/systemdesigns/tinyurlapi/infrastructure/datasource/URL.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.victuxbb.systemdesigns.tinyurlapi.infrastructure.datasource; | ||
|
||
import com.victuxbb.systemdesigns.tinyurlapi.domain.shorturl.ShortURL; | ||
import org.springframework.data.cassandra.core.cql.PrimaryKeyType; | ||
import org.springframework.data.cassandra.core.mapping.Column; | ||
import org.springframework.data.cassandra.core.mapping.PrimaryKeyColumn; | ||
import org.springframework.data.cassandra.core.mapping.Table; | ||
|
||
import java.time.LocalDateTime; | ||
|
||
@Table("url") | ||
public class URL { | ||
@PrimaryKeyColumn(name = "hash", type = PrimaryKeyType.PARTITIONED) | ||
private final String hash; | ||
@Column("original_url") | ||
private final String originalURL; | ||
@Column("creation_date") | ||
private final LocalDateTime creationDate; | ||
|
||
URL(ShortURL shortUrl) { | ||
hash = shortUrl.getHash(); | ||
originalURL = shortUrl.getOriginalURL(); | ||
creationDate = shortUrl.getCreationDate(); | ||
} | ||
|
||
public URL(String hash, String originalURL, LocalDateTime creationDate) { | ||
this.hash = hash; | ||
this.originalURL = originalURL; | ||
this.creationDate = creationDate; | ||
} | ||
|
||
public String getHash() { | ||
return hash; | ||
} | ||
|
||
public String getOriginalURL() { | ||
return originalURL; | ||
} | ||
|
||
public LocalDateTime getCreationDate() { | ||
return creationDate; | ||
} | ||
|
||
ShortURL toShortURL() { | ||
return new ShortURL(hash, originalURL, creationDate); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
6 changes: 4 additions & 2 deletions
6
...inyurlapi/TinyurlapiApplicationTests.java → ...bootstrap/TinyURLAPIApplicationTests.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
...om/victuxbb/systemdesigns/tinyurlapi/infrastructure/controller/TinyURLControllerTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
package com.victuxbb.systemdesigns.tinyurlapi.infrastructure.controller; | ||
|
||
import com.fasterxml.jackson.core.JsonProcessingException; | ||
import com.fasterxml.jackson.databind.ObjectMapper; | ||
import com.victuxbb.systemdesigns.tinyurlapi.domain.kgs.URLKey; | ||
import com.victuxbb.systemdesigns.tinyurlapi.infrastructure.bootstrap.TinyURLAPIApplication; | ||
import com.victuxbb.systemdesigns.tinyurlapi.infrastructure.datasource.InMemoryKGSRepository; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.boot.test.autoconfigure.web.reactive.AutoConfigureWebTestClient; | ||
import org.springframework.boot.test.context.SpringBootTest; | ||
import org.springframework.boot.test.mock.mockito.SpyBean; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.test.context.ActiveProfiles; | ||
import org.springframework.test.context.junit4.SpringRunner; | ||
import org.springframework.test.web.reactive.server.WebTestClient; | ||
import reactor.core.publisher.Mono; | ||
|
||
import static org.mockito.Mockito.when; | ||
|
||
@RunWith(SpringRunner.class) | ||
@ActiveProfiles("test") | ||
@SpringBootTest(classes = TinyURLAPIApplication.class) | ||
@AutoConfigureWebTestClient | ||
public class TinyURLControllerTest { | ||
|
||
@Autowired | ||
private WebTestClient webTestClient; | ||
|
||
@Autowired | ||
private ObjectMapper objectMapper; | ||
|
||
@SpyBean | ||
private InMemoryKGSRepository kgsRepository; | ||
|
||
@Test | ||
public void createTinyURL() throws JsonProcessingException { | ||
when(kgsRepository.getUniqueKey()).thenReturn(Mono.just(new URLKey("/miau"))); | ||
webTestClient | ||
.post() | ||
.uri("/tinyURL") | ||
.contentType(MediaType.APPLICATION_JSON) | ||
.syncBody(objectMapper.writeValueAsString(new CreateTinyURLRequest("/yupiiiii"))) | ||
.exchange() | ||
.expectStatus().is2xxSuccessful() | ||
.expectBody() | ||
.jsonPath("$.hash", "/miau"); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
spring: | ||
data: | ||
cassandra: | ||
port: 9042 | ||
username: cassandra | ||
password: cassandra | ||
keyspace-name: tinyurl | ||
schema-action: create_if_not_exists |