Search in sources :

Example 1 with RetryOnFailure

use of com.github.noraui.cucumber.annotation.RetryOnFailure in project NoraUi by NoraUi.

the class HelloByeSteps method readBlog.

@RetryOnFailure(attempts = 3)
@Given("me any article, please. '(.*)' of '(.*)'.")
public void readBlog(String jsonArticles, String blog) throws FailureException {
    Articles articles = new Articles();
    articles.deserialize(jsonArticles);
    for (Article article : articles) {
        if ("anonymous".equals(article.getAuthor())) {
            new Result.Failure<>("anonymous", "anonymous is prohibited in demo blog!!", true, this.demoPage.getCallBack());
        } else {
            logger.info("> " + blog);
            logger.info("    > " + article.getTitle() + ": " + article.getText());
        }
    }
}
Also used : Articles(com.github.noraui.application.model.demo.Articles) Article(com.github.noraui.application.model.demo.Article) RetryOnFailure(com.github.noraui.cucumber.annotation.RetryOnFailure) Given(cucumber.api.java.en.Given) RetryOnFailure(com.github.noraui.cucumber.annotation.RetryOnFailure)

Example 2 with RetryOnFailure

use of com.github.noraui.cucumber.annotation.RetryOnFailure in project NoraUi by NoraUi.

the class StepInterceptor method invoke.

/**
 * {@inheritDoc}
 */
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
    Object result = null;
    Method m = invocation.getMethod();
    Annotation[] annotations = m.getAnnotations();
    if (annotations.length > 0) {
        Annotation stepAnnotation = annotations[annotations.length - 1];
        for (Annotation a : annotations) {
            if (a.annotationType().getName().startsWith("cucumber.api.java." + Context.getLocale().getLanguage())) {
                stepAnnotation = a;
                break;
            }
        }
        if (stepAnnotation.annotationType().isAnnotationPresent(StepDefAnnotation.class)) {
            Matcher matcher = Pattern.compile("value=(.*)\\)").matcher(stepAnnotation.toString());
            if (matcher.find()) {
                logger.info("> " + stepAnnotation.annotationType().getSimpleName() + " " + String.format(matcher.group(1).replace("(.*)", "%s").replace("[\\.|\\?]", ""), invocation.getArguments()));
            }
        }
    }
    if (m.isAnnotationPresent(RetryOnFailure.class)) {
        RetryOnFailure retryAnnotation = m.getAnnotation(RetryOnFailure.class);
        if (retryAnnotation.verbose()) {
            logger.info("NORAUI StepInterceptor invoke method " + m);
        }
        for (int i = 0; i < retryAnnotation.attempts(); i++) {
            try {
                if (retryAnnotation.verbose()) {
                    logger.info("NORAUI StepInterceptor attempt n° " + i);
                }
                return invocation.proceed();
            } catch (FailureException e) {
                if (retryAnnotation.verbose()) {
                    logger.info("NORAUI StepInterceptor Exception " + e.getMessage());
                }
                if (i == retryAnnotation.attempts() - 1) {
                    e.getFailure().fail();
                }
                Thread.sleep(retryAnnotation.unit().toMillis(retryAnnotation.delay()));
            }
        }
    } else {
        try {
            return invocation.proceed();
        } catch (FailureException e) {
            if (Modifier.isPublic(m.getModifiers())) {
                e.getFailure().fail();
            } else {
                throw e;
            }
        }
    }
    return result;
}
Also used : Matcher(java.util.regex.Matcher) Method(java.lang.reflect.Method) FailureException(com.github.noraui.exception.FailureException) StepDefAnnotation(cucumber.runtime.java.StepDefAnnotation) Annotation(java.lang.annotation.Annotation) RetryOnFailure(com.github.noraui.cucumber.annotation.RetryOnFailure)

Example 3 with RetryOnFailure

use of com.github.noraui.cucumber.annotation.RetryOnFailure in project NoraUi by NoraUi.

the class MailSteps method validActivationEmail.

/**
 * Valid activation email.
 *
 * @param mailHost
 *            example: imap.gmail.com
 * @param mailUser
 *            login of mail box
 * @param mailPassword
 *            password of mail box
 * @param firstCssQuery
 *            the first matching element
 * @param conditions
 *            list of 'expected' values condition and 'actual' values ({@link com.github.noraui.gherkin.GherkinStepCondition}).
 * @throws TechnicalException
 *             is throws if you have a technical error (format, configuration, datas, ...) in NoraUi.
 *             Exception with message and with screenshot and with exception if functional error but no screenshot and no exception if technical error.
 * @throws FailureException
 *             if the scenario encounters a functional error
 */
@RetryOnFailure(attempts = 3, delay = 60)
@Conditioned
@Et("Je valide le mail d'activation '(.*)'[\\.|\\?]")
@And("I valid activation email '(.*)'[\\.|\\?]")
public void validActivationEmail(String mailHost, String mailUser, String mailPassword, String senderMail, String subjectMail, String firstCssQuery, List<GherkinStepCondition> conditions) throws FailureException, TechnicalException {
    RestTemplate restTemplate = createRestTemplate();
    try {
        Properties props = System.getProperties();
        props.setProperty("mail.store.protocol", "imap");
        Session session = Session.getDefaultInstance(props, null);
        Store store = session.getStore("imaps");
        store.connect(mailHost, mailUser, mailPassword);
        Folder inbox = store.getFolder("Inbox");
        inbox.open(Folder.READ_ONLY);
        SearchTerm filterA = new FlagTerm(new Flags(Flags.Flag.SEEN), false);
        SearchTerm filterB = new FromTerm(new InternetAddress(senderMail));
        SearchTerm filterC = new SubjectTerm(subjectMail);
        SearchTerm[] filters = { filterA, filterB, filterC };
        SearchTerm searchTerm = new AndTerm(filters);
        Message[] messages = inbox.search(searchTerm);
        for (Message message : messages) {
            Document doc = Jsoup.parse(getTextFromMessage(message));
            Element link = doc.selectFirst(firstCssQuery);
            HttpHeaders headers = new HttpHeaders();
            HttpEntity<String> entity = new HttpEntity<>(headers);
            ResponseEntity<String> response = restTemplate.exchange(link.attr("href"), HttpMethod.GET, entity, String.class);
            if (!response.getStatusCode().equals(HttpStatus.OK)) {
                new Result.Failure<>("", Messages.format(Messages.getMessage(Messages.FAIL_MESSAGE_MAIL_ACTIVATION), subjectMail), false, Context.getCallBack(Callbacks.RESTART_WEB_DRIVER));
            }
        }
    } catch (Exception e) {
        new Result.Failure<>("", Messages.format(Messages.getMessage(Messages.FAIL_MESSAGE_MAIL_ACTIVATION), subjectMail), false, Context.getCallBack(Callbacks.RESTART_WEB_DRIVER));
    }
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) InternetAddress(javax.mail.internet.InternetAddress) Message(javax.mail.Message) HttpEntity(org.springframework.http.HttpEntity) Element(org.jsoup.nodes.Element) Store(javax.mail.Store) Properties(java.util.Properties) Folder(javax.mail.Folder) Document(org.jsoup.nodes.Document) Result(com.github.noraui.exception.Result) AndTerm(javax.mail.search.AndTerm) FlagTerm(javax.mail.search.FlagTerm) RetryOnFailure(com.github.noraui.cucumber.annotation.RetryOnFailure) Flags(javax.mail.Flags) SearchTerm(javax.mail.search.SearchTerm) SubjectTerm(javax.mail.search.SubjectTerm) MessagingException(javax.mail.MessagingException) FailureException(com.github.noraui.exception.FailureException) IOException(java.io.IOException) TechnicalException(com.github.noraui.exception.TechnicalException) RestTemplate(org.springframework.web.client.RestTemplate) FromTerm(javax.mail.search.FromTerm) Session(javax.mail.Session) Conditioned(com.github.noraui.cucumber.annotation.Conditioned) And(cucumber.api.java.en.And) RetryOnFailure(com.github.noraui.cucumber.annotation.RetryOnFailure) Et(cucumber.api.java.fr.Et)

Aggregations

RetryOnFailure (com.github.noraui.cucumber.annotation.RetryOnFailure)3 FailureException (com.github.noraui.exception.FailureException)2 Article (com.github.noraui.application.model.demo.Article)1 Articles (com.github.noraui.application.model.demo.Articles)1 Conditioned (com.github.noraui.cucumber.annotation.Conditioned)1 Result (com.github.noraui.exception.Result)1 TechnicalException (com.github.noraui.exception.TechnicalException)1 And (cucumber.api.java.en.And)1 Given (cucumber.api.java.en.Given)1 Et (cucumber.api.java.fr.Et)1 StepDefAnnotation (cucumber.runtime.java.StepDefAnnotation)1 IOException (java.io.IOException)1 Annotation (java.lang.annotation.Annotation)1 Method (java.lang.reflect.Method)1 Properties (java.util.Properties)1 Matcher (java.util.regex.Matcher)1 Flags (javax.mail.Flags)1 Folder (javax.mail.Folder)1 Message (javax.mail.Message)1 MessagingException (javax.mail.MessagingException)1