use of io.gravitee.gateway.handlers.api.definition.Api in project gravitee-gateway by gravitee-io.
the class SyncManagerTest method test_shouldUndeployIfLastEventIsUnpublishAPI.
@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 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<>(Collections.singletonList(mockEvent), 0, 0, 1), new Page<>(Collections.singletonList(mockEvent2), 0, 0, 1));
when(apiRepository.findAll()).thenReturn(Collections.singleton(api));
syncManager.refresh();
when(apiManager.apis()).thenReturn(Collections.singleton(mockApi));
when(apiRepository.findAll()).thenReturn(Collections.singleton(api));
syncManager.refresh();
verify(apiManager).deploy(mockApi);
verify(apiManager, never()).update(any(Api.class));
verify(apiManager).undeploy(mockApi.getId());
}
use of io.gravitee.gateway.handlers.api.definition.Api in project gravitee-gateway by gravitee-io.
the class EndpointDiscoveryConsulVerticle method startWatch.
private void startWatch(Api api) {
EndpointDiscoveryService discoveryService = api.getServices().get(EndpointDiscoveryService.class);
if (api.isEnabled() && discoveryService != null && discoveryService.getProvider() == EndpointDiscoveryProvider.CONSUL) {
LOGGER.info("Add Consul.io support for API id[{}] name[{}]", api.getId(), api.getName());
ConsulEndpointDiscoveryConfiguration providerConfiguration = (ConsulEndpointDiscoveryConfiguration) discoveryService.getConfiguration();
URI consulUri = URI.create(providerConfiguration.getUrl());
ConsulClientOptions options = new ConsulClientOptions().setHost(consulUri.getHost()).setPort((consulUri.getPort() != -1) ? consulUri.getPort() : CONSUL_DEFAULT_PORT).setDc(providerConfiguration.getDc()).setAclToken(providerConfiguration.getAcl());
if (HTTPS_SCHEME.equalsIgnoreCase(consulUri.getScheme())) {
// SSL is not configured but the endpoint scheme is HTTPS so let's enable the SSL on Vert.x HTTP client
// automatically
options.setSsl(true).setTrustAll(true);
}
LOGGER.info("Consul.io configuration: endpoint[{}] dc[{}] acl[{}]", consulUri.toString(), options.getDc(), options.getAclToken());
Watch<ServiceEntryList> watch = Watch.service(providerConfiguration.getService(), vertx, options);
watch.setHandler(event -> {
if (event.succeeded()) {
LOGGER.debug("Receiving a Consul.io watch event for service: name[{}]", providerConfiguration.getService());
List<ServiceEntry> entries = event.nextResult().getList();
// Handle new services or updated services
for (ServiceEntry service : event.nextResult().getList()) {
Service service1 = service.getService();
service1.setNodeAddress(service.getNode().getAddress());
handleRegisterService(api, service1);
}
// Handle de-registered services
if (event.prevResult() != null) {
List<ServiceEntry> oldEntries = event.prevResult().getList();
if (oldEntries.size() > entries.size()) {
// Select only de-registered services
oldEntries.removeAll(entries);
for (ServiceEntry oldEntry : oldEntries) {
handleDeregisterService(api, oldEntry.getService());
}
}
}
} else {
LOGGER.info("Unexpected error while watching services catalog", event.cause());
}
}).start();
this.watches.put(api.getId(), watch);
}
}
use of io.gravitee.gateway.handlers.api.definition.Api in project gravitee-gateway by gravitee-io.
the class SyncManagerTest method test_shouldUpdateIfLastEventIsStopAPI.
@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 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<>(Collections.singletonList(mockEvent), 0, 0, 1));
when(apiRepository.findAll()).thenReturn(Collections.singleton(api));
when(apiManager.apis()).thenReturn(Collections.singleton(mockApi));
syncManager.refresh();
when(eventRepository.search(any(EventCriteria.class), any(Pageable.class))).thenReturn(new Page<>(events, 0, 0, 2));
when(apiRepository.findAll()).thenReturn(Collections.singleton(api2));
when(apiManager.get(api.getId())).thenReturn(mockApi);
syncManager.refresh();
verify(apiManager).deploy(mockApi);
verify(apiManager).update(mockApi);
verify(apiManager, never()).undeploy(any(String.class));
}
use of io.gravitee.gateway.handlers.api.definition.Api in project gravitee-gateway by gravitee-io.
the class SyncManagerTest method test_shouldUpdateIfLastEventIsStartAPI.
@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 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<>(Collections.singletonList(mockEvent), 0, 0, 1));
when(apiRepository.findAll()).thenReturn(Collections.singleton(api));
syncManager.refresh();
when(eventRepository.search(any(EventCriteria.class), any(Pageable.class))).thenReturn(new Page<>(events, 0, 0, 2));
when(apiRepository.findAll()).thenReturn(Collections.singleton(api2));
when(apiManager.get(api.getId())).thenReturn(mockApi);
syncManager.refresh();
verify(apiManager).deploy(mockApi);
verify(apiManager).update(mockApi);
verify(apiManager, never()).undeploy(any(String.class));
}
use of io.gravitee.gateway.handlers.api.definition.Api in project gravitee-gateway by gravitee-io.
the class SyncManagerTest method test_twiceWithTwoApis_apiToUpdate.
@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 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<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.findAll()).thenReturn(Collections.singleton(api));
syncManager.refresh();
when(apiRepository.findAll()).thenReturn(Collections.singleton(api2));
when(apiManager.get(api.getId())).thenReturn(mockApi);
syncManager.refresh();
verify(apiManager).deploy(mockApi);
verify(apiManager).update(mockApi);
verify(apiManager, never()).undeploy(any(String.class));
}
Aggregations