Search in sources :

Example 6 with DictionaryEntity

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());
}
Also used : Response(javax.ws.rs.core.Response) NewDictionaryEntity(io.gravitee.rest.api.model.configuration.dictionary.NewDictionaryEntity) DictionaryEntity(io.gravitee.rest.api.model.configuration.dictionary.DictionaryEntity) NewDictionaryEntity(io.gravitee.rest.api.model.configuration.dictionary.NewDictionaryEntity) Test(org.junit.Test)

Example 7 with DictionaryEntity

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));
}
Also used : Response(javax.ws.rs.core.Response) NewDictionaryEntity(io.gravitee.rest.api.model.configuration.dictionary.NewDictionaryEntity) DictionaryEntity(io.gravitee.rest.api.model.configuration.dictionary.DictionaryEntity) NewDictionaryEntity(io.gravitee.rest.api.model.configuration.dictionary.NewDictionaryEntity) Test(org.junit.Test)

Example 8 with DictionaryEntity

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();
}
Also used : UpdateDictionaryEntity(io.gravitee.rest.api.model.configuration.dictionary.UpdateDictionaryEntity) DictionaryEntity(io.gravitee.rest.api.model.configuration.dictionary.DictionaryEntity) Permissions(io.gravitee.rest.api.management.rest.security.Permissions)

Example 9 with DictionaryEntity

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);
    }
}
Also used : DictionaryEntity(io.gravitee.rest.api.model.configuration.dictionary.DictionaryEntity)

Example 10 with DictionaryEntity

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);
}
Also used : DictionaryEntity(io.gravitee.rest.api.model.configuration.dictionary.DictionaryEntity) DictionaryTriggerEntity(io.gravitee.rest.api.model.configuration.dictionary.DictionaryTriggerEntity) DictionaryProviderEntity(io.gravitee.rest.api.model.configuration.dictionary.DictionaryProviderEntity) Date(java.util.Date) Test(org.junit.Test)

Aggregations

DictionaryEntity (io.gravitee.rest.api.model.configuration.dictionary.DictionaryEntity)11 Test (org.junit.Test)6 DictionaryProviderEntity (io.gravitee.rest.api.model.configuration.dictionary.DictionaryProviderEntity)3 Date (java.util.Date)3 Permissions (io.gravitee.rest.api.management.rest.security.Permissions)2 DictionaryTriggerEntity (io.gravitee.rest.api.model.configuration.dictionary.DictionaryTriggerEntity)2 NewDictionaryEntity (io.gravitee.rest.api.model.configuration.dictionary.NewDictionaryEntity)2 UpdateDictionaryEntity (io.gravitee.rest.api.model.configuration.dictionary.UpdateDictionaryEntity)2 Response (javax.ws.rs.core.Response)2