summaryrefslogtreecommitdiffstats
path: root/configserver/src/test/java/com/yahoo/vespa/config/server/session/PrepareParamsTest.java
blob: 2c5de7b4334b5615092bc856761fbf6deeb7e7c6 (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
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.config.server.session;

import com.yahoo.component.Version;
import com.yahoo.config.provision.ApplicationId;
import com.yahoo.config.provision.Rotation;
import com.yahoo.config.provision.TenantName;
import com.yahoo.container.jdisc.HttpRequest;

import org.junit.Test;

import java.time.Duration;
import java.util.Optional;
import java.util.Set;

import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.Matchers.contains;
import static org.hamcrest.Matchers.containsInAnyOrder;
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertThat;
import static org.junit.Assert.assertTrue;

/**
 * @author hmusum
 */
public class PrepareParamsTest {

    @Test
    public void testCorrectParsing() {
        PrepareParams prepareParams = createParams("http://foo:19071/application/v2/", TenantName.defaultName());

        assertThat(prepareParams.getApplicationId(), is(ApplicationId.defaultId()));
        assertFalse(prepareParams.isDryRun());
        assertFalse(prepareParams.ignoreValidationErrors());
        assertThat(prepareParams.vespaVersion(), is(Optional.<String>empty()));
        assertTrue(prepareParams.getTimeoutBudget().hasTimeLeft());
        assertThat(prepareParams.rotations().size(), is(0));
    }


    static final String rotation = "rotation-042.vespa.a02.yahoodns.net";
    static final String vespaVersion = "6.37.49";
    static final String request = "http://foo:19071/application/v2/tenant/foo/application/bar?" +
            PrepareParams.DRY_RUN_PARAM_NAME + "=true&" +
            PrepareParams.IGNORE_VALIDATION_PARAM_NAME + "=false&" +
            PrepareParams.APPLICATION_NAME_PARAM_NAME + "=baz&" +
            PrepareParams.VESPA_VERSION_PARAM_NAME + "=" + vespaVersion;
   
    @Test
    public void testCorrectParsingWithRotation() {
        PrepareParams prepareParams = createParams(request + "&" +
                                                   PrepareParams.ROTATIONS_PARAM_NAME + "=" + rotation,
                                                   TenantName.from("foo"));

        assertThat(prepareParams.getApplicationId().serializedForm(), is("foo:baz:default"));
        assertTrue(prepareParams.isDryRun());
        assertFalse(prepareParams.ignoreValidationErrors());
        Version expectedVersion = Version.fromString(vespaVersion);
        assertThat(prepareParams.vespaVersion().get(), is(expectedVersion));
        assertTrue(prepareParams.getTimeoutBudget().hasTimeLeft());
        Set<Rotation> rotations = prepareParams.rotations();
        assertThat(rotations.size(), is(1));
        assertThat(rotations, contains(equalTo(new Rotation(rotation))));
    }

    @Test
    public void testCorrectParsingWithSeveralRotations() {
        String rotationTwo = "rotation-043.vespa.a02.yahoodns.net";
        String twoRotations = rotation + "," + rotationTwo;
        PrepareParams prepareParams = createParams(request + "&" +
                                      PrepareParams.ROTATIONS_PARAM_NAME + "=" + twoRotations,
                                      TenantName.from("foo"));
        Set<Rotation> rotations = prepareParams.rotations();
        assertThat(rotations, containsInAnyOrder(new Rotation(rotation), new Rotation(rotationTwo)));
    }

    // Create PrepareParams from a request (based on uri and tenant name)
    private static PrepareParams createParams(String uri, TenantName tenantName) {
        return PrepareParams.fromHttpRequest(
                HttpRequest.createTestRequest(uri, com.yahoo.jdisc.http.HttpRequest.Method.PUT),
                tenantName,
                Duration.ofSeconds(60));
    }
}