Permalink
Browse files

NSPSUPPORT-67

Indført konfigurerbar tærskel for, hvor mange færre autorisationer en import-fil må indeholde sammenlignet med antal autorisationer i databasen
  • Loading branch information...
1 parent 5e5022a commit f1c3d540011b4456f7d16baf2f086b1a370ec1c2 @jrftrifork jrftrifork committed Sep 12, 2012
View
@@ -0,0 +1,3 @@
+## autorisationimporter 4.2
+* NSPSUPPORT-67: Indført konfigurerbar tærskel for, hvor mange færre autorisationer en import-fil må indeholde
+ sammenlignet med antal autorisationer i databasen
View
@@ -8,3 +8,13 @@ Se https://github.com/trifork/sdm4-core/tree/sdm-core-4.0/doc
For at køre integrationstests, kræves en opsætning som beskrevet i guide til udviklere
Klon repo med ```git clone https://github.com/trifork/sdm4-autorisationimporter.git```.
+
+## Konfiguration
+Der er følgende importer-specifikke konfigurations-properties
+
+* ``spooler.autorisationimporter.max.allowed.reduction``
+ angiver hvor mange autorisationer, der må være færre end ved sidste import
+ Default-værdi: 10
+ Eksempel: Hvis der er 200.000 autorisationer i databasen, og en import-fil (fx ved en fejl)
+ indeholder 100.000 autorisationer, afvises import-filen
+
@@ -36,6 +36,7 @@
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
+import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
@@ -52,8 +53,9 @@
public class AutorisationParser implements Parser {
private static final String FILENAME_DATE_FORMAT = "yyyyMMdd";
private static final String FILE_ENCODING = "ISO8859-15";
+ public static final String FROMCLAUSE_VALID_AUTORISATIONER = "FROM Autorisation WHERE ValidFrom <= NOW() AND ValidTo > NOW();";
- @Autowired
+ @Autowired
private SLALogger slaLogger;
@Autowired
@@ -62,7 +64,10 @@
@Autowired
JdbcTemplate jdbcTemplate;
- @Override
+ @Value("${spooler.autorisationimporter.max.allowed.reduction}")
+ private int maxAllowedReduction;
+
+ @Override
public void process(File dataset) throws ParserException {
Preconditions.checkNotNull(dataset);
@@ -90,12 +95,13 @@ public void process(File dataset) throws ParserException {
for (File file : files) {
Autorisationsregisterudtraek autRegisterDataset = parse(file, currentVersion);
- persister.persistCompleteDataset(autRegisterDataset);
+ guardAgainsUnacceptableReduction(file, autRegisterDataset);
+ persister.persistCompleteDataset(autRegisterDataset);
}
// Update the table for the STS.
jdbcTemplate.execute("TRUNCATE TABLE autreg");
- jdbcTemplate.update("INSERT INTO autreg (cpr, given_name, surname, aut_id, edu_id) SELECT cpr, Fornavn, Efternavn, Autorisationsnummer, UddannelsesKode FROM Autorisation WHERE ValidFrom <= NOW() AND ValidTo > NOW();");
+ jdbcTemplate.update("INSERT INTO autreg (cpr, given_name, surname, aut_id, edu_id) SELECT cpr, Fornavn, Efternavn, Autorisationsnummer, UddannelsesKode " + FROMCLAUSE_VALID_AUTORISATIONER);
slaLogItem.setCallResultOk();
slaLogItem.store();
@@ -111,7 +117,17 @@ public void process(File dataset) throws ParserException {
}
}
- protected DateTime getDateFromFilename(String filename) {
+ private void guardAgainsUnacceptableReduction(File file, Autorisationsregisterudtraek autRegisterDataset) {
+ int currentNumberOfValidAutorisationer = jdbcTemplate.queryForInt("SELECT COUNT(AutorisationPID) " + FROMCLAUSE_VALID_AUTORISATIONER);
+ int reduction = currentNumberOfValidAutorisationer - autRegisterDataset.size();
+ if (reduction > maxAllowedReduction) {
+ throw new ParserException("Number of autorisationer in file " + file.getAbsolutePath() +
+ " is a reduction of " + reduction + " compared to the current number of active autorisationer " + currentNumberOfValidAutorisationer + " in the database. " +
+ "This is more than the threshold of " + maxAllowedReduction + ", so file is not imported");
+ }
+ }
+
+ protected DateTime getDateFromFilename(String filename) {
DateTimeFormatter formatter = DateTimeFormat.forPattern(FILENAME_DATE_FORMAT);
return formatter.parseDateTime(filename.substring(0, 8));
}
@@ -39,6 +39,8 @@
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
+import org.springframework.context.annotation.PropertySource;
+import org.springframework.context.support.PropertySourcesPlaceholderConfigurer;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@@ -48,12 +50,17 @@
import java.io.IOException;
import static org.junit.Assert.*;
+import static org.junit.matchers.JUnitMatchers.containsString;
+import static org.mockito.Matchers.any;
+import static org.mockito.Matchers.contains;
import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(loader = AnnotationConfigContextLoader.class)
public class AutParserTest {
@Configuration
+ @PropertySource({"classpath:test.properties"})
static class TestConf {
@Bean
public Parser parser() {
@@ -74,11 +81,19 @@ public Persister persister() {
public JdbcTemplate jdbcTemplate() {
return mock(JdbcTemplate.class);
}
+
+ @Bean
+ public static PropertySourcesPlaceholderConfigurer properties(){
+ return new PropertySourcesPlaceholderConfigurer();
+ }
}
@Autowired
AutorisationParser parser;
-
+
+ @Autowired
+ JdbcTemplate jdbcTemplate;
+
public static File valid;
public static File invalid;
private File validWith3Removed;
@@ -102,12 +117,16 @@ public void testParse() throws IOException {
@Test
public void doesNotAllowNumberOfAutorisationerToDecreaseMoreThanThreshold() throws IOException {
+ when(jdbcTemplate.queryForInt(any(String.class))).thenReturn(5); // der er to indgange i den fil vi importer om lidt
+
try {
- parser.parse(validWith3Removed, new DateTime());
+ parser.process(validWith3Removed.getParentFile());
fail("Expected exception from parser because number of autorisationer decreased too much");
} catch (ParserException e) {
- assertTrue(e.getMessage().equals("Number of autorisationer in file was 3 lower than the number of active autorisationer in the database. " +
- "This is more than the threshold of 1, so file is not imported"));
+ assertThat(e.getMessage(), containsString(validWith3Removed.getName()));
+ assertThat(e.getMessage(), containsString("1")); // max allowed reduction
+ assertThat(e.getMessage(), containsString("3")); // reduktionen som antal
+ assertThat(e.getMessage(), containsString("5")); // antal i databasen
}
}
@@ -1,2 +1,3 @@
test.mysql.port=3307
-test.db.name=sdm_warehouse_autorisation_test
+test.db.name=sdm_warehouse_autorisation_test
+spooler.autorisationimporter.max.allowed.reduction=1

0 comments on commit f1c3d54

Please sign in to comment.