DataService implementation is done and tested. The application is now working end-2-end

This commit is contained in:
steam 2020-02-28 18:28:03 +01:00
parent 46a2e75331
commit 439c1560d8
5 changed files with 175 additions and 44 deletions

View File

@ -2,13 +2,18 @@ package click.poweronoff.satellite.service;
import click.poweronoff.satellite.domain.Feature;
import click.poweronoff.satellite.repository.JsonFileRepository;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.IOException;
import java.util.List;
import java.util.Optional;
@Component
@AllArgsConstructor
@Slf4j
public class DataServiceImpl implements DataService {
@Autowired
@ -18,16 +23,43 @@ public class DataServiceImpl implements DataService {
DataTransformationService dataTransformationService;
public Optional<List<Feature>> getAllFeatures() {
return Optional.empty();
try {
List<Feature> features = dataTransformationService.transformToFeatureList(jsonFileRepository.readAllFeatures());
if (!features.isEmpty()) {
return Optional.of(features);
}
return Optional.empty();
} catch (IOException e) {
log.error("exception on getAllFeatures() occurred, please check your app configuration");
return Optional.empty();
}
}
@Override
public Optional<Feature> getFeature(String featureId) {
return Optional.empty();
try {
List<Feature> features = dataTransformationService.transformToFeatureList(jsonFileRepository.readAllFeatures());
if (!features.isEmpty()) {
return features.stream().filter(feature -> feature.getId().equals(featureId)).findFirst();
}
return Optional.empty();
} catch (IOException e) {
log.error("exception on getFeature() and featureIo {} occurred, please check your app configuration", featureId);
return Optional.empty();
}
}
@Override
public Optional<byte[]> getPicture(String featureId) {
return Optional.empty();
try {
List<Feature> features = dataTransformationService.transformToFeatureList(jsonFileRepository.readAllFeatures());
if (!features.isEmpty()) {
return features.stream().filter(feature -> feature.getId().equals(featureId)).findFirst().filter(feature -> feature.getPicture() != null).map(Feature::getPicture);
}
return Optional.empty();
} catch (IOException e) {
log.error("exception on getPicture() and featureIo {} occurred, please check your app configuration", featureId);
return Optional.empty();
}
}
}

View File

@ -7,9 +7,10 @@ import org.apache.tomcat.util.codec.binary.Base64;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import static java.util.Arrays.stream;
@Service
public class DataTransformationService {
@ -17,7 +18,7 @@ public class DataTransformationService {
List<Feature> featuresList = new ArrayList<>();
Arrays.stream(collections).forEach(collection -> Arrays.stream(collection.getFeatures()).forEach(feature -> featuresList.add(createFeature(feature))));
stream(collections).forEach(collection -> stream(collection.getFeatures()).forEach(feature -> featuresList.add(createFeature(feature))));
return featuresList;
}
@ -29,7 +30,7 @@ public class DataTransformationService {
Long.parseLong(features.getProperties().getAcquisition().getBeginViewingDate()),
Long.parseLong(features.getProperties().getAcquisition().getEndViewingDate()),
features.getProperties().getAcquisition().getMissionName(),
Base64.decodeBase64(features.getProperties().getQuicklook().getBytes())
features.getProperties().getQuicklook() != null ? Base64.decodeBase64(features.getProperties().getQuicklook().getBytes()) : null
);
}
}

View File

@ -1,7 +1,91 @@
package click.poweronoff.satellite.service;
//@RunWith(MockitoJUnitRunner.class)
import click.poweronoff.satellite.domain.Feature;
import click.poweronoff.satellite.repository.JsonFileRepository;
import click.poweronoff.satellite.repository.dto.FeaturesCollection;
import click.poweronoff.satellite.testutil.FeaturesCollectionCreator;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import java.io.IOException;
import static org.assertj.core.api.Assertions.assertThat;
import static org.mockito.Mockito.when;
@RunWith(MockitoJUnitRunner.class)
public class DataServiceImplTest {
@Mock
JsonFileRepository jsonFileRepository;
DataServiceImpl dataService;
DataTransformationService dataTransformationService;
@Before
public void setup() {
dataTransformationService = new DataTransformationService();
dataService = new DataServiceImpl(jsonFileRepository, dataTransformationService);
}
@Test
public void getAllFeaturesReturnsEmpty() throws IOException {
when(jsonFileRepository.readAllFeatures()).thenReturn(new FeaturesCollection[]{});
assertThat(dataService.getAllFeatures()).isEmpty();
}
@Test
public void getAllFeaturesReturnsAListOfFeatures() throws IOException {
when(jsonFileRepository.readAllFeatures()).thenReturn(FeaturesCollectionCreator.createTestFeaturesCollection());
assertThat(dataService.getAllFeatures()).isNotEmpty();
assertThat(dataService.getAllFeatures().get()).hasSize(1);
}
@Test
public void getFeatureReturnsEmpty() throws IOException {
when(jsonFileRepository.readAllFeatures()).thenReturn(new FeaturesCollection[]{});
assertThat(dataService.getFeature(FeaturesCollectionCreator.ID)).isEmpty();
}
@Test
public void getFeatureWithNonexistentIdReturnsEmpty() throws IOException {
when(jsonFileRepository.readAllFeatures()).thenReturn(FeaturesCollectionCreator.createTestFeaturesCollection());
assertThat(dataService.getFeature("non-existing-feature")).isEmpty();
}
@Test
public void getFeatureReturnFeature() throws IOException {
when(jsonFileRepository.readAllFeatures()).thenReturn(FeaturesCollectionCreator.createTestFeaturesCollection());
assertThat(dataService.getFeature(FeaturesCollectionCreator.ID)).isNotEmpty();
assertThat(dataService.getFeature(FeaturesCollectionCreator.ID)).isPresent().hasValue(new Feature(
FeaturesCollectionCreator.ID,
FeaturesCollectionCreator.TIMESTAMP,
FeaturesCollectionCreator.BEGIN_VIEWING_DATE,
FeaturesCollectionCreator.END_VIEWING_DATE,
FeaturesCollectionCreator.MISSION_NAME,
FeaturesCollectionCreator.SATELLITE.getBytes()));
}
@Test
public void getPictureReturnsEmpty() throws IOException {
when(jsonFileRepository.readAllFeatures()).thenReturn(new FeaturesCollection[]{});
assertThat(dataService.getPicture(FeaturesCollectionCreator.ID)).isEmpty();
}
@Test
public void getPictureWithNonexistentIdReturnsEmpty() throws IOException {
when(jsonFileRepository.readAllFeatures()).thenReturn(FeaturesCollectionCreator.createTestFeaturesCollection());
assertThat(dataService.getPicture("non-existing-feature")).isEmpty();
}
@Test
public void getPictureReturnsPicture() throws IOException {
when(jsonFileRepository.readAllFeatures()).thenReturn(FeaturesCollectionCreator.createTestFeaturesCollection());
assertThat(dataService.getPicture(FeaturesCollectionCreator.ID)).isPresent().hasValue(FeaturesCollectionCreator.SATELLITE.getBytes());
}
}

View File

@ -2,27 +2,16 @@
package click.poweronoff.satellite.service;
import click.poweronoff.satellite.domain.Feature;
import click.poweronoff.satellite.repository.dto.Acquisition;
import click.poweronoff.satellite.repository.dto.Features;
import click.poweronoff.satellite.repository.dto.FeaturesCollection;
import click.poweronoff.satellite.repository.dto.Properties;
import click.poweronoff.satellite.testutil.FeaturesCollectionCreator;
import org.junit.Before;
import org.junit.Test;
import java.util.Base64;
import java.util.List;
import static org.assertj.core.api.Assertions.assertThat;
public class DataTransformationServiceTest {
private static final String ID = "id";
private static final long TIMESTAMP = 271770505L;
private static final long BEGIN_VIEWING_DATE = 271770509L;
private static final long END_VIEWING_DATE = 271770512L;
private static final String MISSION_NAME = "cool-mission";
private static final String SATELLITE = "satellite";
DataTransformationService dataTransformationService;
@ -34,32 +23,14 @@ public class DataTransformationServiceTest {
@Test
public void transformToFeatureListShouldTransformToFeatureList() {
List<Feature> objectUnderTest = dataTransformationService.transformToFeatureList(createTestFeaturesCollection());
List<Feature> objectUnderTest = dataTransformationService.transformToFeatureList(FeaturesCollectionCreator.createTestFeaturesCollection());
assertThat(objectUnderTest).isNotEmpty();
assertThat(objectUnderTest).hasSize(1);
assertThat(objectUnderTest).containsOnly(new Feature(ID, TIMESTAMP, BEGIN_VIEWING_DATE, END_VIEWING_DATE, MISSION_NAME, SATELLITE.getBytes()));
}
private FeaturesCollection[] createTestFeaturesCollection() {
Acquisition acquisition = new Acquisition();
acquisition.setBeginViewingDate(Long.toString(BEGIN_VIEWING_DATE));
acquisition.setEndViewingDate(Long.toString(END_VIEWING_DATE));
acquisition.setMissionName(MISSION_NAME);
Properties properties = new Properties();
properties.setId(ID);
properties.setQuicklook(new String(Base64.getEncoder().encode("satellite".getBytes())));
properties.setTimestamp(Long.toString(TIMESTAMP));
properties.setAcquisition(acquisition);
Features features = new Features();
features.setProperties(properties);
FeaturesCollection featuresCollection = new FeaturesCollection();
featuresCollection.setFeatures(new Features[]{features});
return new FeaturesCollection[]{featuresCollection};
assertThat(objectUnderTest).containsOnly(new Feature(FeaturesCollectionCreator.ID,
FeaturesCollectionCreator.TIMESTAMP,
FeaturesCollectionCreator.BEGIN_VIEWING_DATE,
FeaturesCollectionCreator.END_VIEWING_DATE,
FeaturesCollectionCreator.MISSION_NAME,
FeaturesCollectionCreator.SATELLITE.getBytes()));
}
}

View File

@ -0,0 +1,43 @@
package click.poweronoff.satellite.testutil;
import click.poweronoff.satellite.repository.dto.Acquisition;
import click.poweronoff.satellite.repository.dto.Features;
import click.poweronoff.satellite.repository.dto.FeaturesCollection;
import click.poweronoff.satellite.repository.dto.Properties;
import java.util.Base64;
public class FeaturesCollectionCreator {
public static final String ID = "id";
public static final long TIMESTAMP = 271770505L;
public static final long BEGIN_VIEWING_DATE = 271770509L;
public static final long END_VIEWING_DATE = 271770512L;
public static final String MISSION_NAME = "cool-mission";
public static final String SATELLITE = "satellite";
public static FeaturesCollection[] createTestFeaturesCollection() {
Acquisition acquisition = new Acquisition();
acquisition.setBeginViewingDate(Long.toString(BEGIN_VIEWING_DATE));
acquisition.setEndViewingDate(Long.toString(END_VIEWING_DATE));
acquisition.setMissionName(MISSION_NAME);
Properties properties = new Properties();
properties.setId(ID);
properties.setQuicklook(new String(Base64.getEncoder().encode(SATELLITE.getBytes())));
properties.setTimestamp(Long.toString(TIMESTAMP));
properties.setAcquisition(acquisition);
Features features = new Features();
features.setProperties(properties);
FeaturesCollection featuresCollection = new FeaturesCollection();
featuresCollection.setFeatures(new Features[]{features});
return new FeaturesCollection[]{featuresCollection};
}
}