summaryrefslogtreecommitdiffstats
path: root/controller-server/src/test/java/com/yahoo/vespa/hosted/controller/integration/ZoneApiMock.java
blob: 9e08e774744a1ebabe0fc38ddf8eaf898eadf75b (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
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.hosted.controller.integration;

import com.yahoo.config.provision.CloudName;
import com.yahoo.config.provision.Environment;
import com.yahoo.config.provision.RegionName;
import com.yahoo.config.provision.SystemName;
import com.yahoo.config.provision.zone.ZoneApi;
import com.yahoo.config.provision.zone.ZoneId;

import java.util.Objects;

/**
 * @author hakonhall
 */
public class ZoneApiMock implements ZoneApi {

    private final SystemName systemName;
    private final ZoneId id;
    private final ZoneId virtualId;
    private final CloudName cloudName;
    private final String cloudNativeRegionName;

    public static Builder newBuilder() { return new Builder(); }

    private ZoneApiMock(SystemName systemName, ZoneId id, ZoneId virtualId, CloudName cloudName, String cloudNativeRegionName) {
        this.systemName = systemName;
        this.id = id;
        this.virtualId = virtualId;
        this.cloudName = cloudName;
        this.cloudNativeRegionName = cloudNativeRegionName;
        if (virtualId != null && virtualId.equals(id)) {
            throw new IllegalArgumentException("Virtual ID cannot be equal to zone ID: " + id);
        }
    }

    public static ZoneApiMock fromId(String id) {
        return newBuilder().withId(id).build();
    }

    public static ZoneApiMock from(Environment environment, RegionName region) {
        return newBuilder().with(ZoneId.from(environment, region)).build();
    }

    public static ZoneApiMock from(ZoneId zone) {
        return newBuilder().with(zone).build();
    }

    @Override
    public SystemName getSystemName() { return systemName; }

    @Override
    public ZoneId getId() { return id; }

    @Override
    public ZoneId getVirtualId() {
        return virtualId == null ? getId() : virtualId;
    }

    @Override
    public CloudName getCloudName() { return cloudName; }

    @Override
    public String getCloudNativeRegionName() { return cloudNativeRegionName; }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        ZoneApiMock that = (ZoneApiMock) o;
        return id.equals(that.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    public static class Builder {

        private SystemName systemName = SystemName.defaultSystem();
        private ZoneId id = ZoneId.defaultId();
        private ZoneId virtualId ;
        private CloudName cloudName = CloudName.defaultName();
        private String cloudNativeRegionName = id.region().value();

        public Builder with(ZoneId id) {
            this.id = id;
            return this;
        }

        public Builder withSystem(SystemName systemName) {
            this.systemName = systemName;
            return this;
        }

        public Builder withId(String id) {
            return with(ZoneId.from(id));
        }

        public Builder withVirtualId(ZoneId virtualId) {
            this.virtualId = virtualId;
            return this;
        }

        public Builder withVirtualId(String virtualId) {
            return withVirtualId(ZoneId.from(virtualId));
        }

        public Builder with(CloudName cloudName) {
            this.cloudName = cloudName;
            return this;
        }

        public Builder withCloud(String cloud) { return with(CloudName.from(cloud)); }

        public Builder withCloudNativeRegionName(String cloudRegionName) {
            this.cloudNativeRegionName = cloudRegionName;
            return this;
        }

        public ZoneApiMock build() {
            return new ZoneApiMock(systemName, id, virtualId, cloudName, cloudNativeRegionName);
        }
    }

}