Skip to content

Commit

Permalink
Merge 60ecc2e into a3bbc6d
Browse files Browse the repository at this point in the history
  • Loading branch information
allanmckenzie committed Nov 2, 2020
2 parents a3bbc6d + 60ecc2e commit 04b16cc
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 252 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ All notable changes to this project will be documented in this file, which follo
on [Keep a CHANGELOG](http://keepachangelog.com/). This project adheres to
[Semantic Versioning](http://semver.org/).

## [7.2.0-M1] - 2020-10-28
## [7.2.0-M2] - 2020-11-02
### Added
- Support for fetching features from azure with a dummy implementation for the framework

## [7.2.0-M1] - 2020-10-28
### Added
- Added support for FeatureControl toggling by annotating service component
handler methods with @FeatureControl
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,28 +1,45 @@
package uk.gov.justice.services.core.featurecontrol.local;

import static java.lang.String.format;
import static java.util.Optional.empty;
import static java.util.Optional.of;

import java.io.File;
import java.net.URL;
import java.util.Optional;

import javax.inject.Inject;

import org.slf4j.Logger;

public class LocalFeatureFileLocator {

public static final String FEATURE_CONTROL_FILE_NAME = "feature-control.yaml";

@Inject
private ClasspathLocalFeatureFileLocator classpathLocalFeatureFileLocator;
private WildflyDeploymentDirectoryLocator wildflyDeploymentDirectoryLocator;

@Inject
private FileToUrlConverter fileToUrlConverter;

@Inject
private WildflyDeploymentDirLocalFeatureFileLocator wildflyDeploymentDirLocalFeatureFileLocator;
private Logger logger;

public Optional<URL> findLocalFeatureFileLocation(final String featureControlFileName) {

final File deploymentDirectory = wildflyDeploymentDirectoryLocator.getDeploymentDirectory().toFile();

public Optional<URL> findLocalFeatureFileLocation() {
if (deploymentDirectory.exists()) {

final Optional<URL> classpathUrl = classpathLocalFeatureFileLocator.findLocalFeatureFileLocation(FEATURE_CONTROL_FILE_NAME);
final File file = new File(deploymentDirectory, featureControlFileName);

if (classpathUrl.isPresent()) {
return classpathUrl;
if (file.exists()) {
logger.warn(format("Feature control file found in wildfly deployment directory: '%s'", file.getAbsolutePath()));
return of(fileToUrlConverter.toUrl(file));
}
} else {
logger.error(format("wildfly deployment dir '%s; does not exist", deploymentDirectory.getAbsolutePath()));
}

return wildflyDeploymentDirLocalFeatureFileLocator.findLocalFeatureFileLocation(FEATURE_CONTROL_FILE_NAME);
return empty();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

public class LocalFeatureStore {

public static final String FEATURE_CONTROL_FILE_NAME = "feature-control.yaml";

@Inject
private YamlParser yamlParser;

Expand All @@ -27,7 +29,7 @@ public class LocalFeatureStore {

public Optional<Feature> lookup(final String featureName) {

final Optional<URL> url = localFeatureFileLocator.findLocalFeatureFileLocation();
final Optional<URL> url = localFeatureFileLocator.findLocalFeatureFileLocation(FEATURE_CONTROL_FILE_NAME);

if (url.isPresent()) {

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package uk.gov.justice.services.core.featurecontrol.remote;

import uk.gov.justice.services.core.featurecontrol.domain.Feature;

import java.util.List;

public interface AzureFeatureFetcher {

List<Feature> fetchFeatures();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package uk.gov.justice.services.core.featurecontrol.remote;

import static java.util.Collections.emptyList;

import uk.gov.justice.services.core.featurecontrol.domain.Feature;

import java.util.List;

import javax.enterprise.context.ApplicationScoped;
import javax.enterprise.inject.Default;

@Default
@ApplicationScoped
public class DefaultAzureFeatureFetcher implements AzureFeatureFetcher {

@Override
public List<Feature> fetchFeatures() {

return emptyList();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package uk.gov.justice.services.core.featurecontrol.remote;

import static java.util.Collections.emptyList;

import uk.gov.justice.services.core.featurecontrol.domain.Feature;

import java.util.List;

import javax.annotation.Priority;
import javax.enterprise.inject.Alternative;
import javax.inject.Singleton;

@Singleton
@Alternative
@Priority(100)
public class DummyAzureFeatureFetcher implements AzureFeatureFetcher {

@Override
public List<Feature> fetchFeatures() {

return emptyList();
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,83 +1,83 @@
package uk.gov.justice.services.core.featurecontrol.local;

import static java.util.Optional.empty;
import static java.util.Optional.of;
import static java.nio.file.Paths.get;
import static org.hamcrest.CoreMatchers.endsWith;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.CoreMatchers.notNullValue;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.junit.Assert.fail;
import static org.mockito.Mockito.verifyZeroInteractions;
import static org.mockito.Mockito.when;
import static uk.gov.justice.services.core.featurecontrol.local.LocalFeatureFileLocator.FEATURE_CONTROL_FILE_NAME;
import static uk.gov.justice.services.core.featurecontrol.local.LocalFeatureStore.FEATURE_CONTROL_FILE_NAME;

import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Path;
import java.util.Optional;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Spy;
import org.mockito.runners.MockitoJUnitRunner;
import org.slf4j.Logger;


@RunWith(MockitoJUnitRunner.class)
public class LocalFeatureFileLocatorTest {

@Spy
private FileToUrlConverter fileToUrlConverter = new FileToUrlConverter();

@Mock
private ClasspathLocalFeatureFileLocator classpathLocalFeatureFileLocator;
private WildflyDeploymentDirectoryLocator wildflyDeploymentDirectoryLocator;

@Mock
private WildflyDeploymentDirLocalFeatureFileLocator wildflyDeploymentDirLocalFeatureFileLocator;
private Logger logger;

@InjectMocks
private LocalFeatureFileLocator localFeatureFileLocator;

@Test
public void shouldReturnTheFileLocationFromTheClasspathIfItExists() throws Exception {
public void shouldGetTheUrlOfAFileInTheWildflyDeploymentDirectory() throws Exception {

final URL classpathUrl = aRealUrlBecauseWeCannotMockTheDamnThings();
when(classpathLocalFeatureFileLocator.findLocalFeatureFileLocation(FEATURE_CONTROL_FILE_NAME)).thenReturn(of(classpathUrl));
final Path aDirectory = directoryOfFileOnClasspath(FEATURE_CONTROL_FILE_NAME);
when(wildflyDeploymentDirectoryLocator.getDeploymentDirectory()).thenReturn(aDirectory);

final Optional<URL> localFeatureFileLocation = localFeatureFileLocator.findLocalFeatureFileLocation();
final Optional<URL> localFeatureFileLocation = localFeatureFileLocator.findLocalFeatureFileLocation(FEATURE_CONTROL_FILE_NAME);

if (localFeatureFileLocation.isPresent()) {
assertThat(localFeatureFileLocation.get(), is(classpathUrl));
assertThat(localFeatureFileLocation.get().getFile(), endsWith("/" + FEATURE_CONTROL_FILE_NAME));
} else {
fail();
}

verifyZeroInteractions(wildflyDeploymentDirLocalFeatureFileLocator);
}

@Test
public void shouldReturnTheFileLocationFromWildflyDeploymentDirectoryIfTheClasspathFileDoesNotExists() throws Exception {
public void shouldReturnEmptyIfTheFileCannotBeFoundInTheWildflyDeploymentDirectory() throws Exception {

final Optional<URL> classpathUrl = empty();
final Path aDirectory = directoryOfFileOnClasspath("json/envelope.json");
when(wildflyDeploymentDirectoryLocator.getDeploymentDirectory()).thenReturn(aDirectory);

final URL wildflyDeploymentDirUrl = aRealUrlBecauseWeCannotMockTheDamnThings();
when(classpathLocalFeatureFileLocator.findLocalFeatureFileLocation(FEATURE_CONTROL_FILE_NAME)).thenReturn(classpathUrl);
when(wildflyDeploymentDirLocalFeatureFileLocator.findLocalFeatureFileLocation(FEATURE_CONTROL_FILE_NAME)).thenReturn(of(wildflyDeploymentDirUrl));
final Optional<URL> localFeatureFileLocation = localFeatureFileLocator.findLocalFeatureFileLocation();
final Optional<URL> localFeatureFileLocation = localFeatureFileLocator.findLocalFeatureFileLocation(FEATURE_CONTROL_FILE_NAME);

if (localFeatureFileLocation.isPresent()) {
assertThat(localFeatureFileLocation.get(), is(wildflyDeploymentDirUrl));
} else {
fail();
}
assertThat(localFeatureFileLocation.isPresent(), is(false));
}

@Test
public void shouldReturnEmptyIfNotFoundOnClasspathNorWildflyDeploymentDirectory() throws Exception {
public void shouldReturnEmptyIfTheWildflyDeploymentDirectoryDoesNotExist() throws Exception {

when(classpathLocalFeatureFileLocator.findLocalFeatureFileLocation(FEATURE_CONTROL_FILE_NAME)).thenReturn(empty());
when(wildflyDeploymentDirLocalFeatureFileLocator.findLocalFeatureFileLocation(FEATURE_CONTROL_FILE_NAME)).thenReturn(empty());
final Path aDirectory = get("/this/directory-does-not-exist");
when(wildflyDeploymentDirectoryLocator.getDeploymentDirectory()).thenReturn(aDirectory);

assertThat(localFeatureFileLocator.findLocalFeatureFileLocation().isPresent(), is(false));
final Optional<URL> localFeatureFileLocation = localFeatureFileLocator.findLocalFeatureFileLocation(FEATURE_CONTROL_FILE_NAME);

assertThat(localFeatureFileLocation.isPresent(), is(false));
}

private URL aRealUrlBecauseWeCannotMockTheDamnThings() {
final URL url = getClass().getClassLoader().getResource("feature-control.yaml");
assertThat(url, is(notNullValue()));
return url;
private Path directoryOfFileOnClasspath(final String fileOnClasspath) throws URISyntaxException {

final URL localFeatureFileLocation = getClass().getClassLoader().getResource(fileOnClasspath);

return get(localFeatureFileLocation.toURI()).getParent().toAbsolutePath();
}
}
Loading

0 comments on commit 04b16cc

Please sign in to comment.