/*******************************************************************************
 * Copyright (c) 2017 Red Hat Inc and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Red Hat Inc - initial API and implementation
 *******************************************************************************/
package org.eclipse.kapua.service.device.steps;

import static org.eclipse.kapua.locator.KapuaLocator.getInstance;

import org.eclipse.kapua.commons.util.ThrowingRunnable;
import org.eclipse.kapua.service.authentication.AuthenticationService;
import org.eclipse.kapua.service.authentication.LoginCredentials;
import org.eclipse.kapua.service.device.registry.Device;
import org.eclipse.kapua.service.device.registry.DeviceRegistryService;
import org.eclipse.kapua.service.user.User;
import org.eclipse.kapua.service.user.UserService;
import org.junit.Assert;

public final class With {

    private With() {
    }

    public static void withLogin(final LoginCredentials credentials, final ThrowingRunnable runnable) throws Exception {
        final AuthenticationService service = getInstance().getService(AuthenticationService.class);

        try {
            service.login(credentials);
            runnable.run();
        } finally {
            service.logout();
        }
    }

    public static void withUserAccount(final String accountName, final ThrowingConsumer<User> consumer) throws Exception {
        final UserService userService = getInstance().getService(UserService.class);
        final User account = userService.findByName(accountName);

        if (account == null) {
            Assert.fail("Unable to find account: " + accountName);
            return;
        }

        consumer.accept(account);
    }

    public static void withDevice(final User account, final String clientId, final ThrowingConsumer<Device> consumer) throws Exception {
        DeviceRegistryService service = getInstance().getService(DeviceRegistryService.class);
        Device device = service.findByClientId(account.getId(), clientId);
        if (device == null) {
            throw new IllegalStateException(String.format("Unable to find device '%s' for account '%s'", clientId, account.getId()));
        }

        consumer.accept(device);
    }
}

@@ -1793,127 +1793,96 @@
     
-if (account == null) %7B%0A            Assert.fail(%22Unable to find account: %22 + accountName);%0A            return;%0A        %7D
+Assert.assertNotNull(String.format(%22Account %25s should be found%22, accountName), account);
 %0A%0A  
@@ -2231,181 +2231,75 @@
 d);%0A
-        if (device == null) %7B%0A            throw new IllegalStateException(String.format(%22Unable to find device '%25s' for account '%25s'%22, clientId, account.getId()));%0A        %7D
+%0A        Assert.assertNotNull(%22Device should not be null%22, device);
 %0A%0A  

Use asserts in order to work with Wait.assertFor