use of ch.aaap.harvestclient.exception.HarvestRuntimeException in project harvest-client by 3AP-AG.
the class GsonTest method testReferenceAdapterDto.
@Test
void testReferenceAdapterDto() {
ReferenceDtoAdapter referenceDtoAdapter = new ReferenceDtoAdapter();
GsonBuilder gsonBuilder = new GsonBuilder();
Gson gson = gsonBuilder.create();
// create a token for Reference<String>, which does not have a corresponding
// ReferenceDTO class
TypeToken<?> token = TypeToken.getParameterized(Reference.class, String.class);
HarvestRuntimeException e = assertThrows(HarvestRuntimeException.class, () -> referenceDtoAdapter.create(gson, token));
assertThat(e.getCause()).isOfAnyClassIn(ClassNotFoundException.class);
}
use of ch.aaap.harvestclient.exception.HarvestRuntimeException in project harvest-client by 3AP-AG.
the class TestSetupUtil method loadConfiguration.
/**
* Try to load a local conf file first, and override values with environment
* variables if they are set.
*
* @param name
* the name of the configuration to load. Allowed characters are the
* same as for environment variables (A-Z, a-z, 0-9, _)
* @return the configuration with env overrides applied
*/
private static Config loadConfiguration(String name) {
// check for allowed characters
if (name.matches("^[A-Za-z0-9_]]")) {
throw new HarvestRuntimeException("Configuration name " + name + " contains invalid characters");
}
Config config = ConfigFactory.load(name);
String harvestAccountId = System.getenv("HARVEST_ACCOUNT_ID_" + name.toUpperCase());
String harvestAuthToken = System.getenv("HARVEST_AUTH_TOKEN_" + name.toUpperCase());
if (harvestAccountId != null) {
config = config.withValue("harvest.auth.accountId", ConfigValueFactory.fromAnyRef(harvestAccountId));
}
if (harvestAuthToken != null) {
config = config.withValue("harvest.auth.token", ConfigValueFactory.fromAnyRef(harvestAuthToken));
}
return config;
}
use of ch.aaap.harvestclient.exception.HarvestRuntimeException in project harvest-client by 3AP-AG.
the class ExpensesApiImpl method attachReceipt.
@Override
public Expense attachReceipt(Reference<Expense> expenseReference, InputStream inputStream, String fileName) throws IOException {
Path filePath = null;
try {
Path tempDirectory = Files.createTempDirectory("harvest-client");
filePath = tempDirectory.resolve(fileName);
if (!filePath.toAbsolutePath().startsWith(tempDirectory)) {
throw new HarvestRuntimeException("Invalid filename: " + fileName);
}
Files.copy(inputStream, filePath);
return attachReceipt(expenseReference, filePath.toFile());
} finally {
if (filePath != null) {
Files.delete(filePath);
}
}
}
use of ch.aaap.harvestclient.exception.HarvestRuntimeException in project harvest-client by 3AP-AG.
the class ReferenceDtoAdapter method create.
@Override
public <T> TypeAdapter<T> create(Gson gson, TypeToken<T> type) {
Class<? super T> rawType = type.getRawType();
log.trace("Called create with {}", type);
if (rawType != Reference.class) {
return null;
}
Class<?> elementClass = parseReferenceType(type);
String simpleName = elementClass.getSimpleName();
String expectedClassName = simpleName + DTO_SUFFIX;
try {
Class<?> dtoClass = Class.forName(BaseReferenceDto.class.getPackage().getName() + "." + expectedClassName);
log.trace("Got Dto class {}", dtoClass);
TypeAdapter<?> delegate = gson.getAdapter(dtoClass);
log.trace("Returning type adapter for {}", type);
return new TypeAdapter<T>() {
@Override
public void write(JsonWriter out, T value) throws IOException {
if (value == null) {
out.nullValue();
return;
}
Reference reference = (Reference) value;
out.value(reference.getId());
}
@Override
@SuppressWarnings("unchecked")
public T read(JsonReader in) throws IOException {
return (T) delegate.read(in);
}
};
} catch (ClassNotFoundException e) {
throw new HarvestRuntimeException("Could not find ReferenceDto. Reference<" + simpleName + "> needs a class named " + expectedClassName + " to existing in the same package as ReferenceDto", e);
}
}
Aggregations