-
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.
- Loading branch information
Showing
16 changed files
with
785 additions
and
31 deletions.
There are no files selected for viewing
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
55 changes: 55 additions & 0 deletions
55
.../src/main/java/com/avojak/webapp/aws/p2/repository/service/cache/LoadingCacheFactory.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,55 @@ | ||
package com.avojak.webapp.aws.p2.repository.service.cache; | ||
|
||
import com.avojak.webapp.aws.p2.repository.model.project.Project; | ||
import com.avojak.webapp.aws.p2.repository.service.configuration.ServiceProperties; | ||
import com.google.common.base.Ticker; | ||
import com.google.common.cache.CacheBuilder; | ||
import com.google.common.cache.LoadingCache; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import java.util.Map; | ||
|
||
import static com.google.common.base.Preconditions.checkNotNull; | ||
|
||
/** | ||
* Factory class to create new instances of {@link LoadingCache}. | ||
*/ | ||
@Component | ||
public class LoadingCacheFactory { | ||
|
||
private final ProjectCacheLoader cacheLoader; | ||
private final Ticker ticker; | ||
private final ServiceProperties properties; | ||
|
||
/** | ||
* Constructor. | ||
* | ||
* @param cacheLoader | ||
* The {@link ProjectCacheLoader}. Cannot be null. | ||
* @param ticker | ||
* The {@link Ticker}. Cannot be null. | ||
* @param properties | ||
* The {@link ServiceProperties}. Cannot be null. | ||
*/ | ||
@Autowired | ||
public LoadingCacheFactory(final ProjectCacheLoader cacheLoader, final Ticker ticker, | ||
final ServiceProperties properties) { | ||
this.cacheLoader = checkNotNull(cacheLoader, "cacheLoader cannot be null"); | ||
this.ticker = checkNotNull(ticker, "ticker cannot be null"); | ||
this.properties = checkNotNull(properties, "properties cannot be null"); | ||
} | ||
|
||
/** | ||
* Creates a new instance of {@link LoadingCache}. | ||
* | ||
* @return The new, non-null {@link LoadingCache}. | ||
*/ | ||
public LoadingCache<Boolean, Map<String, Project>> create() { | ||
return CacheBuilder.newBuilder() | ||
.expireAfterWrite(properties.getCacheExpirationDuration(), properties.getCacheExpirationUnits()) | ||
.ticker(ticker) | ||
.build(cacheLoader); | ||
} | ||
|
||
} |
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
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
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
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
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
25 changes: 25 additions & 0 deletions
25
...ain/java/com/avojak/webapp/aws/p2/repository/service/impl/InputStreamResourceFactory.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,25 @@ | ||
package com.avojak.webapp.aws.p2.repository.service.impl; | ||
|
||
import com.amazonaws.services.s3.model.S3ObjectInputStream; | ||
import org.springframework.core.io.InputStreamResource; | ||
import org.springframework.stereotype.Component; | ||
|
||
/** | ||
* Factory class to create instances of {@link InputStreamResource}. | ||
*/ | ||
@Component | ||
public class InputStreamResourceFactory { | ||
|
||
/** | ||
* Creates a new input stream resource for the given S3 object input stream. | ||
* | ||
* @param s3ObjectInputStream | ||
* The {@link S3ObjectInputStream}. | ||
* | ||
* @return The new, non-null {@link InputStreamResource}. | ||
*/ | ||
public InputStreamResource create(final S3ObjectInputStream s3ObjectInputStream) { | ||
return new InputStreamResource(s3ObjectInputStream); | ||
} | ||
|
||
} |
54 changes: 54 additions & 0 deletions
54
.../test/java/com/avojak/webapp/aws/p2/repository/service/cache/LoadingCacheFactoryTest.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,54 @@ | ||
package com.avojak.webapp.aws.p2.repository.service.cache; | ||
|
||
import com.avojak.webapp.aws.p2.repository.service.configuration.ServiceProperties; | ||
import com.google.common.base.Ticker; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.junit.runner.RunWith; | ||
import org.mockito.Mock; | ||
import org.mockito.junit.MockitoJUnitRunner; | ||
|
||
import java.util.concurrent.TimeUnit; | ||
|
||
import static org.junit.Assert.assertNotNull; | ||
|
||
/** | ||
* Test class for {@link LoadingCacheFactory}. | ||
*/ | ||
@RunWith(MockitoJUnitRunner.class) | ||
public class LoadingCacheFactoryTest { | ||
|
||
@Mock | ||
private ProjectCacheLoader projectCacheLoader; | ||
|
||
@Mock | ||
private Ticker ticker; | ||
|
||
private ServiceProperties properties; | ||
|
||
@Before | ||
public void setup() { | ||
properties = new ServiceProperties(30000L, TimeUnit.SECONDS, "{0}", "{0}", "{0}", "p2.avojak.com"); | ||
} | ||
|
||
@Test(expected = NullPointerException.class) | ||
public void testConstructor_NullCacheLoader() { | ||
new LoadingCacheFactory(null, ticker, properties); | ||
} | ||
|
||
@Test(expected = NullPointerException.class) | ||
public void testConstructor_NullTicker() { | ||
new LoadingCacheFactory(projectCacheLoader, null, properties); | ||
} | ||
|
||
@Test(expected = NullPointerException.class) | ||
public void testConstructor_NullServiceProperties() { | ||
new LoadingCacheFactory(projectCacheLoader, ticker, null); | ||
} | ||
|
||
@Test | ||
public void testCreate() { | ||
assertNotNull(new LoadingCacheFactory(projectCacheLoader, ticker, properties).create()); | ||
} | ||
|
||
} |
Oops, something went wrong.