-
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.
test: utvidet med test klasser som validerer riktig oppsett og følgin… (
#255)
- Loading branch information
Showing
20 changed files
with
739 additions
and
78 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
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
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
155 changes: 155 additions & 0 deletions
155
src/main/java/no/nav/familie/inntektsmelding/server/jackson/IndexClasses.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,155 @@ | ||
package no.nav.familie.inntektsmelding.server.jackson; | ||
|
||
import java.io.IOException; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.net.URL; | ||
import java.nio.file.Files; | ||
import java.nio.file.Paths; | ||
import java.nio.file.StandardOpenOption; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.concurrent.ConcurrentHashMap; | ||
import java.util.concurrent.ConcurrentMap; | ||
import java.util.function.Predicate; | ||
|
||
import org.jboss.jandex.AnnotationTarget.Kind; | ||
import org.jboss.jandex.ClassInfo; | ||
import org.jboss.jandex.DotName; | ||
import org.jboss.jandex.Index; | ||
import org.jboss.jandex.IndexReader; | ||
import org.jboss.jandex.Indexer; | ||
import org.slf4j.Logger; | ||
|
||
/** | ||
* Henter persistert index (hvis generert) eller genererer index for angitt location (typisk matcher en jar/war fil). | ||
*/ | ||
public class IndexClasses { | ||
private static final Logger log = org.slf4j.LoggerFactory.getLogger(IndexClasses.class); | ||
|
||
private static final ConcurrentMap<URI, IndexClasses> INDEXES = new ConcurrentHashMap<>(); | ||
|
||
private final URI scanLocation; | ||
private final String jandexIndexFileName; | ||
|
||
private IndexClasses(URI location) { | ||
this(location, "jandex.idx"); | ||
} | ||
|
||
private IndexClasses(URI scanLocation, String jandexIndexFileName) { | ||
this.scanLocation = scanLocation; | ||
this.jandexIndexFileName = jandexIndexFileName; | ||
} | ||
|
||
public static IndexClasses getIndexFor(final URI location) { | ||
return INDEXES.computeIfAbsent(location, IndexClasses::new); | ||
} | ||
|
||
public Index getIndex() { | ||
|
||
if ("file".equals(scanLocation.getScheme()) && scanLocation.getSchemeSpecificPart().contains("/target/")) { | ||
// må regenerere index fra fil system i IDE ved å scanne dir, ellers kan den mulig være utdatert (når kjører Jetty i IDE f.eks) | ||
return scanIndexFromFilesystem(scanLocation); | ||
} else { | ||
return getPersistedJandexIndex(scanLocation); | ||
} | ||
|
||
} | ||
|
||
private Index scanIndexFromFilesystem(URI location) { | ||
try { | ||
var indexer = new Indexer(); | ||
var source = Paths.get(location); | ||
try (var paths = Files.walk(source)) { | ||
paths.filter(Files::isRegularFile).forEach(f -> { | ||
var fileName = f.getFileName(); | ||
if (fileName != null && fileName.toString().endsWith(".class")) { | ||
try (var newInputStream = Files.newInputStream(f, StandardOpenOption.READ)) { | ||
indexer.index(newInputStream); | ||
} catch (IOException e) { | ||
throw new IllegalStateException("Fikk ikke indeksert klasse " + f + ", kan ikke scanne klasser", e); | ||
} | ||
} | ||
}); | ||
} | ||
return indexer.complete(); | ||
} catch (IOException e) { | ||
throw new IllegalStateException("Fikk ikke lest path " + location + ", kan ikke scanne klasser", e); | ||
} | ||
} | ||
|
||
// fra pre-generert index, slipper runtime scanning for raskere startup | ||
private Index getPersistedJandexIndex(URI location) { | ||
var jandexIdxUrl = getJandexIndexUrl(location); | ||
|
||
try (var jandexStream = jandexIdxUrl.openStream()) { | ||
var reader = new IndexReader(jandexStream); | ||
return reader.read(); | ||
} catch (IOException e) { | ||
throw new IllegalStateException("Fikk ikke lest jandex index, kan ikke scanne klasser", e); | ||
} | ||
} | ||
|
||
private URL getJandexIndexUrl(URI location) { | ||
var uriString = location.toString(); | ||
var classLoaders = new LinkedHashSet<ClassLoader>(); | ||
classLoaders.add(getClass().getClassLoader()); | ||
classLoaders.add(Thread.currentThread().getContextClassLoader()); | ||
|
||
return classLoaders.stream().flatMap(cl -> { | ||
try { | ||
return Collections.list(cl.getResources("META-INF/" + jandexIndexFileName)).stream(); | ||
} catch (IOException e2) { | ||
throw new IllegalArgumentException("Kan ikke lese jandex index fil", e2); | ||
} | ||
}).filter(url -> { | ||
try { | ||
return String.valueOf(url.toURI()).startsWith(uriString) || String.valueOf(url.toURI().getSchemeSpecificPart()).startsWith(uriString); | ||
} catch (URISyntaxException e1) { | ||
throw new IllegalArgumentException("Kan ikke scanne URI", e1); | ||
} | ||
}).findFirst().orElseThrow(() -> new IllegalStateException("Fant ikke jandex index for location=" + location)); | ||
} | ||
|
||
public List<Class<?>> getClassesWithAnnotation(Class<?> annotationClass) { | ||
|
||
var search = DotName.createSimple(annotationClass.getName()); | ||
var annotations = getIndex().getAnnotations(search); | ||
|
||
List<Class<?>> jsonTypes = new ArrayList<>(); | ||
for (var annotation : annotations) { | ||
if (Kind.CLASS.equals(annotation.target().kind())) { | ||
var className = annotation.target().asClass().name().toString(); | ||
try { | ||
jsonTypes.add(Class.forName(className)); | ||
} catch (ClassNotFoundException e) { | ||
log.error("Kan ikke finne klasse i Classpath, som funnet i Jandex index", e);// NOSONAR | ||
} | ||
} | ||
} | ||
|
||
return jsonTypes; | ||
} | ||
|
||
public List<Class<?>> getClasses(Predicate<ClassInfo> predicate, Predicate<Class<?>> classPredicate) { | ||
var knownClasses = getIndex().getKnownClasses(); | ||
|
||
List<Class<?>> cls = new ArrayList<>(); | ||
for (var ci : knownClasses) { | ||
var className = ci.name().toString(); | ||
try { | ||
if (predicate.test(ci)) { | ||
var c = Class.forName(className); | ||
if (classPredicate.test(c)) { | ||
cls.add(c); | ||
} | ||
} | ||
} catch (ClassNotFoundException e) { | ||
log.error("Kan ikke finne klasse i Classpath, som funnet i Jandex index", e);// NOSONAR | ||
} | ||
} | ||
return cls; | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
src/main/resources/db/postgres/defaultDS/V1.0_015__fix_test_skjema_anmerkninger.sql
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 @@ | ||
|
||
COMMENT ON COLUMN forespoersel.FORSTE_UTTAKSDATO IS 'Dat med første frævarsdag.'; | ||
COMMENT ON COLUMN forespoersel.STATUS IS 'Status på forespørselen.'; | ||
|
||
ALTER TABLE forespoersel RENAME CONSTRAINT "forespoersel_pkey" TO "pk_foresporsel_id"; | ||
ALTER TABLE refusjon_endring RENAME CONSTRAINT "refusjon_endring_pkey" TO "pk_refusjon_endring_id"; | ||
ALTER TABLE bortfalt_naturalytelse RENAME CONSTRAINT "bortfalt_naturalytelse_pkey" TO "pk_bortfalt_naturalytelse_id"; | ||
ALTER TABLE endringsaarsak RENAME CONSTRAINT "endringsaarsak_pkey" TO "pk_endringsaarsak_id"; | ||
ALTER TABLE kontaktperson RENAME CONSTRAINT "kontaktperson_pkey" TO "pk_kontaktperson_id"; | ||
ALTER TABLE inntektsmelding RENAME CONSTRAINT "inntektsmelding_pkey" TO "pk_inntektsmelding_id"; | ||
|
||
DROP INDEX idx_bortfalt_naturalytelse_primary_key; | ||
DROP INDEX idx_endringsaarsak_primary_key; | ||
DROP INDEX idx_forespoersel_primary_key; | ||
DROP INDEX idx_inntektsmelding_primary_key; | ||
DROP INDEX idx_refusjon_endring_primary_key; | ||
DROP INDEX idx_kontaktperson_primary_key; | ||
|
||
CREATE INDEX idx_kontaktperson_inntektsmelding_id ON kontaktperson(inntektsmelding_id); | ||
CREATE INDEX idx_bortfalt_naturalytelse_inntektsmelding_id ON bortfalt_naturalytelse(inntektsmelding_id); | ||
CREATE INDEX idx_endringsaarsak_inntektsmelding_id ON endringsaarsak(inntektsmelding_id); | ||
CREATE INDEX idx_refusjon_endring_inntektsmelding_id ON refusjon_endring(inntektsmelding_id); | ||
|
||
ALTER INDEX idx_forespoersel_uuid RENAME TO "uidx_forespoersel_uuid"; | ||
|
Oops, something went wrong.