use of io.gravitee.rest.api.model.configuration.dictionary.DictionaryEntity in project gravitee-management-rest-api by gravitee-io.
the class DictionariesResourceTest method shouldNotCreateDictionary_emptyName.
@Test
public void shouldNotCreateDictionary_emptyName() {
reset(dictionaryService);
final NewDictionaryEntity newDictionaryEntity = new NewDictionaryEntity();
newDictionaryEntity.setName(null);
newDictionaryEntity.setType(DictionaryType.MANUAL);
DictionaryEntity returnedDictionary = new DictionaryEntity();
returnedDictionary.setId("my-dictionary");
doReturn(returnedDictionary).when(dictionaryService).create(any());
final Response response = envTarget().request().post(Entity.json(new NewDictionaryEntity()));
assertEquals(HttpStatusCode.BAD_REQUEST_400, response.getStatus());
}
use of io.gravitee.rest.api.model.configuration.dictionary.DictionaryEntity in project gravitee-management-rest-api by gravitee-io.
the class DictionariesResourceTest method shouldCreateApi.
@Test
public void shouldCreateApi() {
reset(dictionaryService);
final NewDictionaryEntity newDictionaryEntity = new NewDictionaryEntity();
newDictionaryEntity.setDescription("description");
newDictionaryEntity.setName("my-dictionary-name");
newDictionaryEntity.setType(DictionaryType.MANUAL);
DictionaryEntity returnedDictionary = new DictionaryEntity();
returnedDictionary.setId("my-dictionary");
doReturn(returnedDictionary).when(dictionaryService).create(any());
final Response response = envTarget().request().post(Entity.json(newDictionaryEntity));
assertEquals(HttpStatusCode.CREATED_201, response.getStatus());
assertEquals(envTarget().path("my-dictionary").getUri().toString(), response.getHeaders().getFirst(HttpHeaders.LOCATION));
}
use of io.gravitee.rest.api.model.configuration.dictionary.DictionaryEntity in project gravitee-management-rest-api by gravitee-io.
the class DictionaryResource method doLifecycleAction.
@POST
@Produces(MediaType.APPLICATION_JSON)
@ApiOperation(value = "Manage the dictionary's lifecycle", notes = "User must have the DICTIONARY[LIFECYCLE] permission to use this service")
@ApiResponses({ @ApiResponse(code = 200, message = "Dictionary state updated"), @ApiResponse(code = 500, message = "Internal server error") })
@Permissions({ @Permission(value = RolePermission.ENVIRONMENT_DICTIONARY, acls = RolePermissionAction.UPDATE) })
public Response doLifecycleAction(@Context HttpHeaders headers, @ApiParam(required = true, allowableValues = "START, STOP") @QueryParam("action") LifecycleActionParam action, @PathParam("dictionary") String dictionary) {
DictionaryEntity dictionaryEntity = dictionaryService.findById(dictionary);
if (dictionaryEntity.getType() == DictionaryType.DYNAMIC) {
switch(action.getAction()) {
case START:
checkLifecycle(dictionaryEntity, action.getAction());
dictionaryEntity = dictionaryService.start(dictionary);
break;
case STOP:
checkLifecycle(dictionaryEntity, action.getAction());
dictionaryEntity = dictionaryService.stop(dictionary);
break;
default:
dictionaryEntity = null;
break;
}
return Response.ok(dictionaryEntity).tag(Long.toString(dictionaryEntity.getUpdatedAt().getTime())).lastModified(dictionaryEntity.getUpdatedAt()).build();
}
return Response.status(Response.Status.BAD_REQUEST).entity("A manual dictionary can not be started/stopped manually").build();
}
use of io.gravitee.rest.api.model.configuration.dictionary.DictionaryEntity in project gravitee-management-rest-api by gravitee-io.
the class DictionaryManager method start.
/**
* Starts or restarts a dictionary if it has been updated since last start.
*
* @param dictionaryId the id of the dictionary to start.
*/
public void start(String dictionaryId) {
try {
final DictionaryEntity dictionary = dictionaryService.findById(dictionaryId);
final DictionaryEntity startedDictionary = dictionaries.get(dictionary.getId());
if (startedDictionary == null) {
// The dictionary is not yet started, start it.
eventManager.publishEvent(DictionaryEvent.START, dictionary);
} else if (needRestart(dictionary, startedDictionary)) {
// The dictionary is already started but has been updated, force a restart.
eventManager.publishEvent(DictionaryEvent.RESTART, dictionary);
}
dictionaries.put(dictionary.getId(), dictionary);
} catch (Exception e) {
logger.error("Error occurred when trying to start the dictionary [{}].", dictionaryId, e);
}
}
use of io.gravitee.rest.api.model.configuration.dictionary.DictionaryEntity in project gravitee-management-rest-api by gravitee-io.
the class DictionaryManagerTest method shouldRestartTriggerChanged.
@Test
public void shouldRestartTriggerChanged() {
final DictionaryProviderEntity provider = new DictionaryProviderEntity();
provider.setConfiguration(JsonNodeFactory.instance.nullNode());
final DictionaryTriggerEntity trigger = new DictionaryTriggerEntity();
trigger.setRate(1);
trigger.setUnit(TimeUnit.SECONDS);
final DictionaryEntity dictionary = new DictionaryEntity();
dictionary.setUpdatedAt(new Date());
dictionary.setProvider(provider);
dictionary.setId(DICTIONARY_ID);
dictionary.setTrigger(trigger);
final DictionaryTriggerEntity triggerUpdated = new DictionaryTriggerEntity();
triggerUpdated.setRate(1);
triggerUpdated.setUnit(TimeUnit.MINUTES);
final DictionaryEntity dictionaryUpdated = new DictionaryEntity();
dictionaryUpdated.setUpdatedAt(new Date(ZonedDateTime.now().plusSeconds(60).toInstant().toEpochMilli()));
dictionaryUpdated.setProvider(provider);
dictionaryUpdated.setId(DICTIONARY_ID);
dictionaryUpdated.setTrigger(triggerUpdated);
when(dictionaryService.findById(DICTIONARY_ID)).thenReturn(dictionary).thenReturn(dictionaryUpdated);
cut.start(DICTIONARY_ID);
cut.start(DICTIONARY_ID);
verify(eventManager, times(1)).publishEvent(eq(DictionaryEvent.START), eq(dictionary));
verify(eventManager, times(1)).publishEvent(eq(DictionaryEvent.RESTART), eq(dictionary));
verifyNoMoreInteractions(eventManager);
}
Aggregations