SyncManager { public void refresh() { logger.debug("Synchronization #{} started at {}", counter.incrementAndGet(), Instant.now().toString()); logger.debug("Refreshing gateway state..."); long nextLastRefreshAt = System.currentTimeMillis(); try { synchronizeApis(nextLastRefreshAt); } catch (Exception ex) { logger.error("An error occurs while synchronizing APIs", ex); } try { synchronizeDictionaries(nextLastRefreshAt); } catch (Exception ex) { logger.error("An error occurs while synchronizing dictionaries", ex); } lastRefreshAt = nextLastRefreshAt; logger.debug("Synchronization #{} ended at {}", counter.get(), Instant.now().toString()); }  void refresh(); void setApiRepository(ApiRepository apiRepository); void setEventRepository(EventRepository eventRepository); void setApiManager(ApiManager apiManager); long getLastRefreshAt(); long getCounter();  }
@Test(expected = IllegalArgumentException.class) @Ignore public void shouldNotDeployBecauseWrongConfiguration() throws Exception { io.gravitee.repository.management.model.Api api = new RepositoryApiBuilder().id("api-test").updatedAt(new Date()).definition("test").build(); final io.gravitee.definition.model.Api mockApi = mockApi(api); when(gatewayConfiguration.shardingTags()).thenReturn(Optional.of(Arrays.asList("test", "!test"))); when(apiRepository.search(null, new ApiFieldExclusionFilter.Builder().excludeDefinition().excludePicture().build())).thenReturn(singletonList(api)); when(apiManager.apis()).thenReturn(Collections.singleton(new Api(mockApi))); final Event mockEvent = mockEvent(api, EventType.PUBLISH_API); when(eventRepository.search( eq(new EventCriteria.Builder() .types(EventType.PUBLISH_API, EventType.UNPUBLISH_API, EventType.START_API, EventType.STOP_API) .property(Event.EventProperties.API_ID.getValue(), api.getId()) .build()), any() )).thenReturn((new Page(singletonList(mockEvent), 0, 0, 0))); syncManager.refresh(); }
@Test public void test_not_deployApiWithoutEvent() throws Exception { io.gravitee.repository.management.model.Api api = new RepositoryApiBuilder().id("api-test").updatedAt(new Date()).definition("test").build(); final io.gravitee.definition.model.Api mockApi = mockApi(api); when(eventRepository.search( any(EventCriteria.class), any(Pageable.class) )).thenReturn(new Page<>(Collections.emptyList(), 0, 0, 0)); when(apiRepository.search(null, new ApiFieldExclusionFilter.Builder().excludeDefinition().excludePicture().build())).thenReturn(singletonList(api)); syncManager.refresh(); verify(apiManager, never()).deploy(new Api(mockApi)); verify(apiManager, never()).update(any(Api.class)); verify(apiManager, never()).undeploy(any(String.class)); }
@Test public void test_deployOnlyOneApiWithTwoApisAndOneEvent() throws Exception { io.gravitee.repository.management.model.Api api = new RepositoryApiBuilder().id("api-test").updatedAt(new Date()).definition("test").build(); io.gravitee.repository.management.model.Api api2 = new RepositoryApiBuilder().id("api-test-2").updatedAt(new Date()).definition("test2").build(); final io.gravitee.definition.model.Api mockApi = mockApi(api); final List<io.gravitee.repository.management.model.Api> apis = new ArrayList<>(); apis.add(api); apis.add(api2); final Event mockEvent = mockEvent(api, EventType.PUBLISH_API); when(eventRepository.search( any(EventCriteria.class), any(Pageable.class) )).thenReturn( new Page<>(Collections.emptyList(), 0, 0, 0), new Page<>(singletonList(mockEvent), 0, 0, 1)); when(apiRepository.search(null, new ApiFieldExclusionFilter.Builder().excludeDefinition().excludePicture().build())).thenReturn(apis); syncManager.refresh(); verify(apiManager).deploy(argThat(api1 -> api1.getId().equals(mockApi.getId()))); verify(apiManager, never()).update(any(Api.class)); verify(apiManager, never()).undeploy(any(String.class)); }
@Test public void test_shouldUndeployIfLastEventIsUnpublishAPI() throws Exception { io.gravitee.repository.management.model.Api api = new RepositoryApiBuilder().id("api-test").updatedAt(new Date()).definition("test").build(); final io.gravitee.definition.model.Api mockApi = mockApi(api); final Event mockEvent = mockEvent(api, EventType.PUBLISH_API); final Event mockEvent2 = mockEvent(api, EventType.UNPUBLISH_API); when(eventRepository.search( any(EventCriteria.class), any(Pageable.class) )).thenReturn(new Page<>(singletonList(mockEvent), 0, 0, 1), new Page<>(singletonList(mockEvent2), 0, 0, 1)); when(apiRepository.search(null, new ApiFieldExclusionFilter.Builder().excludeDefinition().excludePicture().build())).thenReturn(singletonList(api)); syncManager.refresh(); when(eventRepository.search( any(EventCriteria.class) )).thenReturn(singletonList(mockEvent2)); syncManager.refresh(); verify(apiManager).deploy(new Api(mockApi)); verify(apiManager, never()).update(any(Api.class)); verify(apiManager).undeploy(mockApi.getId()); }
@Test public void test_shouldUpdateIfLastEventIsStartAPI() throws Exception { io.gravitee.repository.management.model.Api api = new RepositoryApiBuilder() .id("api-test") .updatedAt(new Date()) .definition("test") .lifecycleState(LifecycleState.STOPPED) .build(); Instant updateDateInst = api.getUpdatedAt().toInstant().plus(Duration.ofHours(1)); io.gravitee.repository.management.model.Api api2 = new RepositoryApiBuilder() .id("api-test") .updatedAt(Date.from(updateDateInst)) .definition("test") .lifecycleState(LifecycleState.STARTED) .build(); final io.gravitee.definition.model.Api mockApi = mockApi(api); mockApi(api2); final Event mockEvent = mockEvent(api, EventType.PUBLISH_API); final Event mockEvent2 = mockEvent(api2, EventType.START_API); List<Event> events = new ArrayList<Event>(); events.add(mockEvent); events.add(mockEvent2); when(eventRepository.search( any(EventCriteria.class), any(Pageable.class) )).thenReturn(new Page<>(singletonList(mockEvent), 0, 0, 1)); when(apiRepository.search(null, new ApiFieldExclusionFilter.Builder().excludeDefinition().excludePicture().build())).thenReturn(singletonList(api)); syncManager.refresh(); final Api apiDefinition = new Api(mockApi); apiDefinition.setEnabled(api.getLifecycleState() == LifecycleState.STARTED); apiDefinition.setDeployedAt(api.getDeployedAt()); when(apiManager.get(api.getId())).thenReturn(apiDefinition); when(eventRepository.search( any(EventCriteria.class) )).thenReturn(singletonList(mockEvent2)); syncManager.refresh(); verify(apiManager).deploy(new Api(mockApi)); verify(apiManager).update(new Api(mockApi)); verify(apiManager, never()).undeploy(any(String.class)); }
@Test public void test_shouldUpdateIfLastEventIsStopAPI() throws Exception { io.gravitee.repository.management.model.Api api = new RepositoryApiBuilder() .id("api-test") .updatedAt(new Date()) .definition("test") .lifecycleState(LifecycleState.STARTED) .build(); Instant updateDateInst = api.getUpdatedAt().toInstant().plus(Duration.ofHours(1)); io.gravitee.repository.management.model.Api api2 = new RepositoryApiBuilder() .id("api-test") .updatedAt(Date.from(updateDateInst)) .definition("test") .lifecycleState(LifecycleState.STOPPED) .build(); final io.gravitee.definition.model.Api mockApi = mockApi(api); mockApi(api2); final Event mockEvent = mockEvent(api, EventType.PUBLISH_API); final Event mockEvent2 = mockEvent(api2, EventType.STOP_API); List<Event> events = new ArrayList<Event>(); events.add(mockEvent); events.add(mockEvent2); when(eventRepository.search( any(EventCriteria.class), any(Pageable.class) )).thenReturn(new Page<>(singletonList(mockEvent), 0, 0, 1)); when(apiRepository.search(null, new ApiFieldExclusionFilter.Builder().excludeDefinition().excludePicture().build())).thenReturn(singletonList(api)); syncManager.refresh(); when(eventRepository.search( any(EventCriteria.class) )).thenReturn(singletonList(mockEvent)); final Api apiDefinition = new Api(mockApi); apiDefinition.setEnabled(api.getLifecycleState() == LifecycleState.STARTED); apiDefinition.setDeployedAt(api.getDeployedAt()); when(apiManager.get(api.getId())).thenReturn(apiDefinition); syncManager.refresh(); verify(apiManager).deploy(new Api(mockApi)); verify(apiManager).update(new Api(mockApi)); verify(apiManager, never()).undeploy(any(String.class)); }
@Test public void test_empty() throws TechnicalException { when(apiRepository.search(null, new ApiFieldExclusionFilter.Builder().excludeDefinition().excludePicture().build())).thenReturn(emptyList()); syncManager.refresh(); verify(apiManager, never()).deploy(any(Api.class)); verify(apiManager, never()).update(any(Api.class)); verify(apiManager, never()).undeploy(any(String.class)); }
@Test public void test_newApi() throws Exception { io.gravitee.repository.management.model.Api api = new RepositoryApiBuilder().id("api-test").updatedAt(new Date()).definition("test").build(); final io.gravitee.definition.model.Api mockApi = mockApi(api); final Event mockEvent = mockEvent(api, EventType.PUBLISH_API); when(eventRepository.search( any(EventCriteria.class), any(Pageable.class) )).thenReturn(new Page<>(singletonList(mockEvent), 0, 0, 1)); when(apiRepository.search(null, new ApiFieldExclusionFilter.Builder().excludeDefinition().excludePicture().build())).thenReturn(singletonList(api)); syncManager.refresh(); verify(apiManager).deploy(new Api(mockApi)); verify(apiManager, never()).update(any(Api.class)); verify(apiManager, never()).undeploy(any(String.class)); }
@Test public void test_twiceWithSameApi() throws Exception { io.gravitee.repository.management.model.Api api = new RepositoryApiBuilder().id("api-test").updatedAt(new Date()).definition("test").build(); final io.gravitee.definition.model.Api mockApi = mockApi(api); final Event mockEvent = mockEvent(api, EventType.PUBLISH_API); when(eventRepository.search( any(EventCriteria.class), any(Pageable.class) )).thenReturn(new Page<>(singletonList(mockEvent), 0, 0, 1)); when(apiRepository.search(null, new ApiFieldExclusionFilter.Builder().excludeDefinition().excludePicture().build())).thenReturn(singletonList(api)); when(apiManager.get(api.getId())).thenReturn(null); syncManager.refresh(); final Api apiDefinition = new Api(mockApi); apiDefinition.setEnabled(api.getLifecycleState() == LifecycleState.STARTED); apiDefinition.setDeployedAt(api.getDeployedAt()); syncManager.refresh(); verify(apiManager).deploy(new Api(mockApi)); verify(apiManager, never()).update(any(Api.class)); verify(apiManager, never()).undeploy(any(String.class)); }
@Test public void test_twiceWithTwoApis() throws Exception { io.gravitee.repository.management.model.Api api = new RepositoryApiBuilder().id("api-test").updatedAt(new Date()).definition("test").build(); final io.gravitee.definition.model.Api mockApi = mockApi(api); final Event mockEvent = mockEvent(api, EventType.PUBLISH_API); when(eventRepository.search( any(EventCriteria.class), any(Pageable.class) )).thenReturn(new Page<>(singletonList(mockEvent), 0, 0, 1)); when(apiRepository.search(null, new ApiFieldExclusionFilter.Builder().excludeDefinition().excludePicture().build())).thenReturn(singletonList(api)); syncManager.refresh(); io.gravitee.repository.management.model.Api api2 = new RepositoryApiBuilder().id("api-test-2").updatedAt(new Date()).definition("test2").build(); final io.gravitee.definition.model.Api mockApi2 = mockApi(api2); final Event mockEvent2 = mockEvent(api2, EventType.PUBLISH_API); List<Event> events = new ArrayList<>(); events.add(mockEvent2); when(eventRepository.search( any(EventCriteria.class) )).thenReturn(events); syncManager.refresh(); verify(apiManager, times(2)).deploy(argThat(api1 -> api1.getId().equals(mockApi.getId()) || api2.getId().equals(mockApi2.getId()))); verify(apiManager, never()).update(any(Api.class)); verify(apiManager, never()).undeploy(any(String.class)); }
@Test public void test_twiceWithTwoApis_apiToRemove() throws Exception { io.gravitee.repository.management.model.Api api = new RepositoryApiBuilder().id("api-test").updatedAt(new Date()).definition("test").build(); final io.gravitee.definition.model.Api mockApi = mockApi(api); final Event mockEvent = mockEvent(api, EventType.PUBLISH_API); when(eventRepository.search( any(EventCriteria.class), any(Pageable.class) )).thenReturn(new Page<>(singletonList(mockEvent), 0, 0, 1)); when(apiRepository.search(null, new ApiFieldExclusionFilter.Builder().excludeDefinition().excludePicture().build())).thenReturn(singletonList(api)); syncManager.refresh(); io.gravitee.repository.management.model.Api api2 = new RepositoryApiBuilder().id("api-test-2").updatedAt(new Date()).definition("test2").build(); final io.gravitee.definition.model.Api mockApi2 = mockApi(api2); final Event mockEvent2 = mockEvent(api2, EventType.PUBLISH_API); when(eventRepository.search( any(EventCriteria.class) )).thenReturn(Collections.singletonList(mockEvent)); syncManager.refresh(); verify(apiManager, times(2)).deploy(argThat(api1 -> api1.getId().equals(mockApi.getId()) || api2.getId().equals(mockApi2.getId()))); verify(apiManager, never()).update(any(Api.class)); verify(apiManager, never()).undeploy(api.getId()); verify(apiManager, never()).undeploy(api2.getId()); }
@Test public void test_twiceWithTwoApis_apiToUpdate() throws Exception { io.gravitee.repository.management.model.Api api = new RepositoryApiBuilder().id("api-test").updatedAt(new Date()).definition("test").build(); Instant updateDateInst = api.getUpdatedAt().toInstant().plus(Duration.ofHours(1)); io.gravitee.repository.management.model.Api api2 = new RepositoryApiBuilder().id("api-test").updatedAt(Date.from(updateDateInst)).definition("test2").build(); final io.gravitee.definition.model.Api mockApi = mockApi(api); mockApi(api2); final Event mockEvent = mockEvent(api, EventType.PUBLISH_API); final Event mockEvent2 = mockEvent(api2, EventType.PUBLISH_API); List<Event> events = new ArrayList<>(); events.add(mockEvent); events.add(mockEvent2); when(eventRepository.search( any(EventCriteria.class), any(Pageable.class) )).thenReturn(new Page<>(events, 0, 0, 1)); when(apiRepository.search(null, new ApiFieldExclusionFilter.Builder().excludeDefinition().excludePicture().build())).thenReturn(singletonList(api)); syncManager.refresh(); when(eventRepository.search( any(EventCriteria.class) )).thenReturn(Collections.singletonList(mockEvent2)); final Api apiDefinition = new Api(mockApi); apiDefinition.setEnabled(api.getLifecycleState() == LifecycleState.STARTED); apiDefinition.setDeployedAt(api.getDeployedAt()); when(apiManager.get(api.getId())).thenReturn(apiDefinition); syncManager.refresh(); verify(apiManager).deploy(apiDefinition); verify(apiManager).update(apiDefinition); verify(apiManager, never()).undeploy(any(String.class)); }
@Test public void test_twiceWithTwoApis_api_noUpdate() throws Exception { io.gravitee.repository.management.model.Api api = new RepositoryApiBuilder().id("api-test").updatedAt(new Date()).definition("test").build(); io.gravitee.repository.management.model.Api api2 = new RepositoryApiBuilder().id("api-test").updatedAt(api.getUpdatedAt()).definition("test").build(); final io.gravitee.definition.model.Api mockApi = mockApi(api); final Event mockEvent = mockEvent(api, EventType.PUBLISH_API); final Event mockEvent2 = mockEvent(api2, EventType.PUBLISH_API); List<Event> events = new ArrayList<Event>(); events.add(mockEvent); events.add(mockEvent2); when(eventRepository.search( any(EventCriteria.class), any(Pageable.class) )).thenReturn(new Page<>(events, 0, 0, 1)); when(apiRepository.search(null, new ApiFieldExclusionFilter.Builder().excludeDefinition().excludePicture().build())).thenReturn(singletonList(api)); syncManager.refresh(); when(eventRepository.search( any(EventCriteria.class) )).thenReturn(Collections.singletonList(mockEvent2)); syncManager.refresh(); verify(apiManager, times(2)).deploy(new Api(mockApi)); verify(apiManager, never()).update(any(Api.class)); verify(apiManager, never()).undeploy(any(String.class)); }
@Test public void test_not_deployApiWithTagExclusion() throws Exception { io.gravitee.repository.management.model.Api api = new RepositoryApiBuilder().id("api-test").updatedAt(new Date()).definition("test").build(); final io.gravitee.definition.model.Api mockApi = mockApi(api); mockApi.setTags(new HashSet<>(Arrays.asList(new String[]{"test"}))); when(gatewayConfiguration.shardingTags()).thenReturn(Optional.of(singletonList("!test"))); when(apiRepository.search(null, new ApiFieldExclusionFilter.Builder().excludeDefinition().excludePicture().build())).thenReturn(singletonList(api)); final Event mockEvent = mockEvent(api, EventType.PUBLISH_API); when(eventRepository.search( any(EventCriteria.class), any(Pageable.class) )).thenReturn(new Page<>(singletonList(mockEvent), 0, 0, 1)); syncManager.refresh(); verify(apiManager, never()).deploy(new Api(mockApi)); verify(apiManager, never()).update(any(Api.class)); }
@Test public void test_deployApiWithTagInclusionExclusion() throws Exception { io.gravitee.repository.management.model.Api api = new RepositoryApiBuilder().id("api-test").updatedAt(new Date()).definition("test").build(); final io.gravitee.definition.model.Api mockApi = mockApi(api); when(gatewayConfiguration.shardingTags()).thenReturn(Optional.of(Arrays.asList("!test", "toto"))); when(apiRepository.search(null, new ApiFieldExclusionFilter.Builder().excludeDefinition().excludePicture().build())).thenReturn(singletonList(api)); final Event mockEvent = mockEvent(api, EventType.PUBLISH_API); when(eventRepository.search( any(EventCriteria.class), any(Pageable.class) )).thenReturn(new Page<>(singletonList(mockEvent), 0, 0, 1)); syncManager.refresh(); verify(apiManager).deploy(new Api(mockApi)); verify(apiManager, never()).update(any(Api.class)); verify(apiManager, never()).undeploy(any(String.class)); }
@Test public void test_not_deployApiWithoutTag() throws Exception { io.gravitee.repository.management.model.Api api = new RepositoryApiBuilder().id("api-test").updatedAt(new Date()).definition("test").build(); final io.gravitee.definition.model.Api mockApi = mockApi(api); when(gatewayConfiguration.shardingTags()).thenReturn(Optional.of(Arrays.asList("test", "toto"))); when(apiRepository.search(null, new ApiFieldExclusionFilter.Builder().excludeDefinition().excludePicture().build())).thenReturn(singletonList(api)); final Event mockEvent = mockEvent(api, EventType.PUBLISH_API); when(eventRepository.search( any(EventCriteria.class), any(Pageable.class) )).thenReturn(new Page<>(singletonList(mockEvent), 0, 0, 1)); syncManager.refresh(); verify(apiManager, never()).deploy(new Api(mockApi)); verify(apiManager, never()).update(any(Api.class)); }