Search in sources :

Example 11 with BadRequestException

use of com.yahoo.elide.core.exceptions.BadRequestException in project elide by yahoo.

the class SubscriptionWebSocketTest method testErrorInStream.

@Test
void testErrorInStream() throws IOException {
    SubscriptionWebSocket endpoint = SubscriptionWebSocket.builder().executorService(executorService).elide(elide).build();
    Book book1 = new Book();
    book1.setTitle("Book 1");
    book1.setId(1);
    Book book2 = new Book();
    book2.setTitle("Book 2");
    book2.setId(2);
    reset(dataStoreTransaction);
    when(dataStoreTransaction.getAttribute(any(), any(), any())).thenThrow(new BadRequestException("Bad Request"));
    when(dataStoreTransaction.loadObjects(any(), any())).thenReturn(new DataStoreIterableBuilder(List.of(book1, book2)).build());
    ConnectionInit init = new ConnectionInit();
    endpoint.onOpen(session);
    endpoint.onMessage(session, mapper.writeValueAsString(init));
    Subscribe subscribe = Subscribe.builder().id("1").payload(Subscribe.Payload.builder().query("subscription {book(topic: ADDED) {id title}}").build()).build();
    endpoint.onMessage(session, mapper.writeValueAsString(subscribe));
    List<String> expected = List.of("{\"type\":\"connection_ack\"}", "{\"type\":\"next\",\"id\":\"1\",\"payload\":{\"data\":{\"book\":{\"id\":\"1\",\"title\":null}},\"errors\":[{\"message\":\"Exception while fetching data (/book/title) : Bad Request\",\"locations\":[{\"line\":1,\"column\":38}],\"path\":[\"book\",\"title\"],\"extensions\":{\"classification\":\"DataFetchingException\"}}]}}", "{\"type\":\"next\",\"id\":\"1\",\"payload\":{\"data\":{\"book\":{\"id\":\"2\",\"title\":null}},\"errors\":[{\"message\":\"Exception while fetching data (/book/title) : Bad Request\",\"locations\":[{\"line\":1,\"column\":38}],\"path\":[\"book\",\"title\"],\"extensions\":{\"classification\":\"DataFetchingException\"}}]}}", "{\"type\":\"complete\",\"id\":\"1\"}");
    ArgumentCaptor<String> message = ArgumentCaptor.forClass(String.class);
    verify(remote, times(4)).sendText(message.capture());
    assertEquals(expected, message.getAllValues());
}
Also used : SubscriptionWebSocket(com.yahoo.elide.graphql.subscriptions.websocket.SubscriptionWebSocket) DataStoreIterableBuilder(com.yahoo.elide.core.datastore.DataStoreIterableBuilder) Book(example.Book) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) ConnectionInit(com.yahoo.elide.graphql.subscriptions.websocket.protocol.ConnectionInit) Subscribe(com.yahoo.elide.graphql.subscriptions.websocket.protocol.Subscribe) Test(org.junit.jupiter.api.Test) GraphQLTest(com.yahoo.elide.graphql.GraphQLTest)

Example 12 with BadRequestException

use of com.yahoo.elide.core.exceptions.BadRequestException in project elide by yahoo.

the class ConfigFile method fromId.

public static String fromId(String id) {
    String idString;
    try {
        idString = URLDecoder.decode(id, "UTF-8");
        idString = new String(Base64.getDecoder().decode(idString.getBytes()));
    } catch (IllegalArgumentException | UnsupportedEncodingException e) {
        throw new BadRequestException("Invalid ID: " + id);
    }
    int hyphenIndex = idString.lastIndexOf(".hjson-");
    String path;
    if (hyphenIndex < 0) {
        path = idString;
    } else {
        path = idString.substring(0, idString.lastIndexOf('-'));
    }
    return path;
}
Also used : UnsupportedEncodingException(java.io.UnsupportedEncodingException) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException)

Example 13 with BadRequestException

use of com.yahoo.elide.core.exceptions.BadRequestException in project elide by yahoo.

the class FilterPredicateTest method parse.

private Map<String, Set<FilterPredicate>> parse(MultivaluedMap<String, String> queryParams) {
    PredicateExtractionVisitor visitor = new PredicateExtractionVisitor();
    Map<String, FilterExpression> expressionMap;
    try {
        expressionMap = strategy.parseTypedExpression("/book", queryParams, NO_VERSION);
    } catch (ParseException e) {
        throw new BadRequestException(e.getMessage());
    }
    Map<String, Set<FilterPredicate>> returnMap = new HashMap<>();
    for (Map.Entry<String, FilterExpression> entry : expressionMap.entrySet()) {
        String typeName = entry.getKey();
        FilterExpression expression = entry.getValue();
        if (!returnMap.containsKey(typeName)) {
            returnMap.put(typeName, new HashSet<>());
        }
        returnMap.get(typeName).addAll(expression.accept(visitor));
    }
    return returnMap;
}
Also used : HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) PredicateExtractionVisitor(com.yahoo.elide.core.filter.expression.PredicateExtractionVisitor) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) ParseException(com.yahoo.elide.core.filter.dialect.ParseException) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) HashMap(java.util.HashMap) Map(java.util.Map) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) MultivaluedMap(javax.ws.rs.core.MultivaluedMap)

Example 14 with BadRequestException

use of com.yahoo.elide.core.exceptions.BadRequestException in project elide by yahoo.

the class JSONAPITableExportOperation method getRequestScope.

@Override
public RequestScope getRequestScope(TableExport export, RequestScope scope, DataStoreTransaction tx, Map<String, List<String>> additionalRequestHeaders) {
    UUID requestId = UUID.fromString(export.getRequestId());
    User user = scope.getUser();
    String apiVersion = scope.getApiVersion();
    URIBuilder uri;
    try {
        uri = new URIBuilder(export.getQuery());
    } catch (URISyntaxException e) {
        throw new BadRequestException(e.getMessage());
    }
    MultivaluedMap<String, String> queryParams = JSONAPIAsyncQueryOperation.getQueryParams(uri);
    // Call with additionalHeader alone
    if (scope.getRequestHeaders().isEmpty()) {
        return new RequestScope("", JSONAPIAsyncQueryOperation.getPath(uri), apiVersion, null, tx, user, queryParams, additionalRequestHeaders, requestId, getService().getElide().getElideSettings());
    }
    // Combine additionalRequestHeaders and existing scope's request headers
    Map<String, List<String>> finalRequestHeaders = new HashMap<String, List<String>>();
    scope.getRequestHeaders().forEach((entry, value) -> finalRequestHeaders.put(entry, value));
    // additionalRequestHeaders will override any headers in scope.getRequestHeaders()
    additionalRequestHeaders.forEach((entry, value) -> finalRequestHeaders.put(entry, value));
    return new RequestScope("", JSONAPIAsyncQueryOperation.getPath(uri), apiVersion, null, tx, user, queryParams, scope.getRequestHeaders(), requestId, getService().getElide().getElideSettings());
}
Also used : User(com.yahoo.elide.core.security.User) HashMap(java.util.HashMap) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) List(java.util.List) URISyntaxException(java.net.URISyntaxException) UUID(java.util.UUID) RequestScope(com.yahoo.elide.core.RequestScope) URIBuilder(org.apache.http.client.utils.URIBuilder)

Example 15 with BadRequestException

use of com.yahoo.elide.core.exceptions.BadRequestException in project elide by yahoo.

the class JSONAPITableExportOperation method getProjections.

@Override
public Collection<EntityProjection> getProjections(TableExport export, RequestScope scope) {
    EntityProjection projection = null;
    try {
        URIBuilder uri = new URIBuilder(export.getQuery());
        Elide elide = getService().getElide();
        projection = new EntityProjectionMaker(elide.getElideSettings().getDictionary(), scope).parsePath(JSONAPIAsyncQueryOperation.getPath(uri));
    } catch (URISyntaxException e) {
        throw new BadRequestException(e.getMessage());
    }
    return Collections.singletonList(projection);
}
Also used : EntityProjection(com.yahoo.elide.core.request.EntityProjection) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) URISyntaxException(java.net.URISyntaxException) Elide(com.yahoo.elide.Elide) EntityProjectionMaker(com.yahoo.elide.jsonapi.EntityProjectionMaker) URIBuilder(org.apache.http.client.utils.URIBuilder)

Aggregations

BadRequestException (com.yahoo.elide.core.exceptions.BadRequestException)28 PersistentResource (com.yahoo.elide.core.PersistentResource)8 Map (java.util.Map)8 RequestScope (com.yahoo.elide.core.RequestScope)7 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)7 List (java.util.List)7 EntityProjection (com.yahoo.elide.core.request.EntityProjection)5 Type (com.yahoo.elide.core.type.Type)5 HashMap (java.util.HashMap)5 Collectors (java.util.stream.Collectors)5 GraphQLTest (com.yahoo.elide.graphql.GraphQLTest)4 ConnectionContainer (com.yahoo.elide.graphql.containers.ConnectionContainer)4 ArrayList (java.util.ArrayList)4 Collection (java.util.Collection)4 Collections (java.util.Collections)4 Optional (java.util.Optional)4 Test (org.junit.jupiter.api.Test)4 Preconditions (com.google.common.base.Preconditions)3 DataStoreTransaction (com.yahoo.elide.core.datastore.DataStoreTransaction)3 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)3