From e059586243eb0eaeb37dd975eae1ed7743e69b37 Mon Sep 17 00:00:00 2001 From: myrle-krantz Date: Fri, 19 May 2017 11:16:21 +0200 Subject: [PATCH] Tightening scope of tenant context. --- .../src/main/java/io/mifos/rhythm/AbstractRhythmTest.java | 6 +++--- .../src/main/java/io/mifos/rhythm/TestBeats.java | 4 ++-- .../service/internal/service/BeatPublisherService.java | 8 +++----- 3 files changed, 8 insertions(+), 10 deletions(-) diff --git a/component-test/src/main/java/io/mifos/rhythm/AbstractRhythmTest.java b/component-test/src/main/java/io/mifos/rhythm/AbstractRhythmTest.java index d3b8a4d..f01ec42 100644 --- a/component-test/src/main/java/io/mifos/rhythm/AbstractRhythmTest.java +++ b/component-test/src/main/java/io/mifos/rhythm/AbstractRhythmTest.java @@ -88,7 +88,7 @@ public Logger logger() { private final static TestEnvironment testEnvironment = new TestEnvironment(APP_NAME); private final static CassandraInitializer cassandraInitializer = new CassandraInitializer(); private final static MariaDBInitializer mariaDBInitializer = new MariaDBInitializer(); - private final static TenantDataStoreContextTestRule tenantDataStoreContext = TenantDataStoreContextTestRule.forRandomTenantName(cassandraInitializer, mariaDBInitializer); + final static TenantDataStoreContextTestRule tenantDataStoreContext = TenantDataStoreContextTestRule.forRandomTenantName(cassandraInitializer, mariaDBInitializer); @ClassRule public static TestRule orderClassRules = RuleChain @@ -139,7 +139,7 @@ Beat createBeat(final String applicationName, final String beatIdentifier) throw beat.setAlignmentHour(now.getHour()); final LocalDateTime expectedBeatTimestamp = getExpectedBeatTimestamp(now, beat.getAlignmentHour()); - Mockito.doReturn(true).when(beatPublisherServiceSpy).publishBeat(Matchers.eq(applicationName), Matchers.eq(beatIdentifier), + Mockito.doReturn(true).when(beatPublisherServiceSpy).publishBeat(Matchers.eq(beatIdentifier), Matchers.eq(tenantDataStoreContext.getTenantName()), Matchers.eq(applicationName), AdditionalMatchers.or(Matchers.eq(expectedBeatTimestamp), Matchers.eq(getNextTimeStamp(expectedBeatTimestamp)))); this.testSubject.createBeat(applicationName, beat); @@ -149,7 +149,7 @@ Beat createBeat(final String applicationName, final String beatIdentifier) throw Mockito.verify(beatPublisherServiceSpy, Mockito.timeout(2_000).atLeastOnce()) .checkBeatForPublish(anyObject(), eq(beatIdentifier), anyString(), eq(applicationName), eq(beat.getAlignmentHour()), anyObject()); - Mockito.verify(beatPublisherServiceSpy, Mockito.times(1)).publishBeat(applicationName, beatIdentifier, expectedBeatTimestamp); + Mockito.verify(beatPublisherServiceSpy, Mockito.times(1)).publishBeat(beatIdentifier, tenantDataStoreContext.getTenantName(), applicationName, expectedBeatTimestamp); return beat; } diff --git a/component-test/src/main/java/io/mifos/rhythm/TestBeats.java b/component-test/src/main/java/io/mifos/rhythm/TestBeats.java index 38a5dcd..5649fda 100644 --- a/component-test/src/main/java/io/mifos/rhythm/TestBeats.java +++ b/component-test/src/main/java/io/mifos/rhythm/TestBeats.java @@ -90,7 +90,7 @@ public void shouldRetryBeatPublishIfFirstAttemptFails() throws InterruptedExcept final LocalDateTime expectedBeatTimestamp = getExpectedBeatTimestamp(now, beat.getAlignmentHour()); - Mockito.when(beatPublisherServiceSpy.publishBeat(appName, beatId, expectedBeatTimestamp)).thenReturn(false, false, true); + Mockito.when(beatPublisherServiceSpy.publishBeat(beatId, tenantDataStoreContext.getTenantName(), appName, expectedBeatTimestamp)).thenReturn(false, false, true); this.testSubject.createBeat(appName, beat); @@ -99,6 +99,6 @@ public void shouldRetryBeatPublishIfFirstAttemptFails() throws InterruptedExcept Mockito.verify(super.beatPublisherServiceSpy, Mockito.timeout(10_000).atLeast(3)) .checkBeatForPublish(anyObject(), eq(beatId), anyString(), eq(appName), eq(beat.getAlignmentHour()), anyObject()); - Mockito.verify(beatPublisherServiceSpy, Mockito.times(3)).publishBeat(appName, beatId, expectedBeatTimestamp); + Mockito.verify(beatPublisherServiceSpy, Mockito.times(3)).publishBeat(beatId, tenantDataStoreContext.getTenantName(), appName, expectedBeatTimestamp); } } \ No newline at end of file diff --git a/service/src/main/java/io/mifos/rhythm/service/internal/service/BeatPublisherService.java b/service/src/main/java/io/mifos/rhythm/service/internal/service/BeatPublisherService.java index 11d15b1..4530a6b 100644 --- a/service/src/main/java/io/mifos/rhythm/service/internal/service/BeatPublisherService.java +++ b/service/src/main/java/io/mifos/rhythm/service/internal/service/BeatPublisherService.java @@ -60,7 +60,7 @@ public BeatPublisherService( } @SuppressWarnings("WeakerAccess") //Access is public for spying in component test. - public boolean publishBeat(final String applicationName, final String beatIdentifier, final LocalDateTime timestamp) { + public boolean publishBeat(final String beatIdentifier, final String tenantIdentifier, final String applicationName, final LocalDateTime timestamp) { final BeatPublish beatPublish = new BeatPublish(beatIdentifier, DateConverter.toIsoString(timestamp)); logger.info("Attempting publish {} with timestamp {} under user {}.", beatPublish, timestamp, properties.getUser()); @@ -71,7 +71,7 @@ public boolean publishBeat(final String applicationName, final String beatIdenti final ServiceInstance beatListenerService = applicationsByName.get(0); final BeatListener beatListener = apiFactory.create(BeatListener.class, beatListenerService.getUri().toString()); - try { + try (final AutoTenantContext ignored = new AutoTenantContext(tenantIdentifier)) { beatListener.publishBeat(beatPublish); return true; } @@ -87,14 +87,12 @@ public Optional checkBeatForPublish( final String applicationName, final Integer alignmentHour, final LocalDateTime nextBeat) { - try (final AutoTenantContext ignored = new AutoTenantContext(tenantIdentifier)) { return getTimesNeedingEvents(nextBeat, now, alignmentHour) .filter(x -> x.isAfter(now) - || !publishBeat(applicationName, beatIdentifier, x)) + || !publishBeat(beatIdentifier, tenantIdentifier, applicationName, x)) .findFirst(); - } } static Stream getTimesNeedingEvents(