aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/test/java/com/yahoo/search/result/DefaultErrorHitTestCase.java
blob: 42095e263dd4e7197ec94fc792fa3b763d234c43 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.search.result;

import static org.junit.jupiter.api.Assertions.*;

import java.util.Iterator;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

/**
 * @author Steinar Knutsen
 */
public class DefaultErrorHitTestCase {

    private static final String SOURCE = "nalle";
    DefaultErrorHit de;

    @BeforeEach
    public void setUp() throws Exception {
        de = new DefaultErrorHit(SOURCE, ErrorMessage.createUnspecifiedError("DefaultErrorHitTestCase"));
    }

    @Test
    void testSetSourceTakeTwo() {
        assertEquals(SOURCE, de.getSource());
        de.setSource(null);
        assertNull(de.getSource());
        de.setSource("bamse");
        assertEquals("bamse", de.getSource());
        de.addError(ErrorMessage.createBackendCommunicationError("blblbl"));
        final Iterator<ErrorMessage> errorIterator = de.errorIterator();
        assertEquals(SOURCE, errorIterator.next().getSource());
        assertEquals("bamse", errorIterator.next().getSource());
    }

    @Test
    void testToString() {
        assertEquals("Error: Source 'nalle': 5: Unspecified error: DefaultErrorHitTestCase", de.toString());
    }

    @Test
    void testSetMainError() {
        ErrorMessage e = ErrorMessage.createBackendCommunicationError("abc");
        assertNull(e.getSource());
        de.addError(e);
        assertEquals(SOURCE, e.getSource());
        boolean caught = false;
        try {
            new DefaultErrorHit(SOURCE, (ErrorMessage) null);
        } catch (NullPointerException ex) {
            caught = true;
        }
        assertTrue(caught);

        caught = false;
        try {
            de.addError(null);
        } catch (NullPointerException ex) {
            caught = true;
        }
        assertTrue(caught);
    }

    @Test
    void testAddError() {
        ErrorMessage e = ErrorMessage.createBackendCommunicationError("ljkhlkjh");
        assertNull(e.getSource());
        de.addError(e);
        assertEquals(SOURCE, e.getSource());
        e = ErrorMessage.createBadRequest("kdjfhsdkfhj");
        de.addError(e);
        int i = 0;
        for (Iterator<ErrorMessage> errors = de.errorIterator(); errors.hasNext(); errors.next()) {
            ++i;
        }
        assertEquals(3, i);
    }

    @Test
    void testAddErrors() {
        DefaultErrorHit other = new DefaultErrorHit("abc", ErrorMessage.createBadRequest("sdasd"));
        de.addErrors(other);
        int i = 0;
        for (Iterator<ErrorMessage> errors = de.errorIterator(); errors.hasNext(); errors.next()) {
            ++i;
        }
        assertEquals(2, i);
        other = new DefaultErrorHit("abd", ErrorMessage.createEmptyDocsums("uiyoiuy"));
        other.addError(ErrorMessage.createNoAnswerWhenPingingNode("xzvczx"));
        de.addErrors(other);
        i = 0;
        for (Iterator<ErrorMessage> errors = de.errorIterator(); errors.hasNext(); errors.next()) {
            ++i;
        }
        assertEquals(4, i);
    }

    @Test
    void testHasOnlyErrorCode() {
        assertTrue(de.hasOnlyErrorCode(com.yahoo.container.protect.Error.UNSPECIFIED.code));
        assertFalse(de.hasOnlyErrorCode(com.yahoo.container.protect.Error.BACKEND_COMMUNICATION_ERROR.code));

        de.addError(ErrorMessage.createUnspecifiedError("dsfsdfs"));
        assertTrue(de.hasOnlyErrorCode(com.yahoo.container.protect.Error.UNSPECIFIED.code));
        assertEquals(com.yahoo.container.protect.Error.UNSPECIFIED.code, de.errors().iterator().next().getCode());

        de.addError(ErrorMessage.createBackendCommunicationError("dsfsdfsd"));
        assertFalse(de.hasOnlyErrorCode(com.yahoo.container.protect.Error.UNSPECIFIED.code));
    }

}