Project

General

Profile

1
/**
2
 * This work was created by participants in the DataONE project, and is
3
 * jointly copyrighted by participating institutions in DataONE. For
4
 * more information on DataONE, see our web site at http://dataone.org.
5
 *
6
 *   Copyright ${year}
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *   http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 */
20
package edu.ucsb.nceas.metacat.dataone;
21

    
22
import java.io.IOException;
23
import java.io.InputStream;
24

    
25
import org.dataone.client.exception.ClientSideException;
26
import org.dataone.client.utils.ExceptionUtils;
27
import org.dataone.client.v2.impl.MultipartCNode;
28
import org.dataone.service.exceptions.BaseException;
29
import org.dataone.service.exceptions.IdentifierNotUnique;
30
import org.dataone.service.exceptions.InvalidRequest;
31
import org.dataone.service.exceptions.InvalidToken;
32
import org.dataone.service.exceptions.NotAuthorized;
33
import org.dataone.service.exceptions.NotFound;
34
import org.dataone.service.exceptions.NotImplemented;
35
import org.dataone.service.exceptions.ServiceFailure;
36
//import org.dataone.service.exceptions.IdentifierNotUnique;
37
import org.dataone.service.types.v1.Identifier;
38
import org.dataone.service.types.v1.NodeReference;
39
import org.dataone.service.types.v1.NodeType;
40
import org.dataone.service.types.v1.ReplicationStatus;
41
import org.dataone.service.types.v1.Service;
42
import org.dataone.service.types.v1.Services;
43
import org.dataone.service.types.v1.Session;
44
import org.dataone.service.types.v1.Subject;
45
import org.dataone.service.types.v1.SubjectInfo;
46
import org.dataone.service.types.v2.Node;
47
import org.dataone.service.types.v2.NodeList;
48
import org.dataone.service.types.v2.SystemMetadata;
49
import org.dataone.service.util.Constants;
50
import org.dataone.service.util.D1Url;
51

    
52
/**
53
 * MockCNode mimics a DataONE Coordinating Node, and should be used only for testing
54
 * when there is a dependency on CN services
55
 */
56
public class MockCNode extends MultipartCNode {
57
    
58
    public final static String V1MNNODEID= "urn:node:test_MN-v1";
59
    public final static String V2MNNODEID= "urn:node:test_MN-v2";
60

    
61
    /**
62
     * See superclass for documentation
63
     */
64
    public MockCNode() throws ClientSideException, IOException {
65
        super(null);
66
        
67
    }
68
    
69
    @Override
70
	public NodeList listNodes() throws NotImplemented, ServiceFailure {
71
		NodeList list = new NodeList();
72
		list.addNode(getCapabilities());
73
		list.addNode(getTestMN());
74
		list.addNode(getTestV1MN());
75
		list.addNode(getTestV2MN());
76
		list.addNode(getReplicationSourceV2MN());
77
		return list;
78
	}
79
    
80
    @Override
81
	public Node getCapabilities() throws NotImplemented, ServiceFailure {
82
		Node node = new Node();
83
		node.setIdentifier(getNodeId());
84
		Subject subject = new Subject();
85
		subject.setValue("cn=" + getNodeId() + ",dc=dataone,dc=org");
86
		node.addSubject(subject );
87
		node.setType(getNodeType());
88
		return node;
89
	}
90
    
91
    @Override
92
	public NodeReference getNodeId() {
93
		NodeReference nodeRef = new NodeReference();
94
		nodeRef.setValue("urn:node:MockCNode");
95
		return nodeRef ;
96
	}
97
    
98
    @Override
99
	public NodeType getNodeType() {
100
		return NodeType.CN;
101
	}
102
    
103
    @Override
104
	public String getNodeBaseServiceUrl() {
105
		return "https//:foo.dataone.org";
106
	}
107
    
108
    /**
109
     * No records exist in the Mock CNode - indicates such
110
     */
111
    @Override
112
    public SystemMetadata getSystemMetadata(Session session, Identifier pid)
113
        throws InvalidToken, NotImplemented, ServiceFailure, NotAuthorized, NotFound {
114
        throw new NotFound("0000", "MockCNode does not contain any records");
115
    }
116
    
117
    /**
118
     * Always return true that the reservation exists
119
     */
120
    @Override
121
    public boolean hasReservation(Session session, Subject subject, Identifier pid) 
122
    	throws InvalidToken, ServiceFailure, NotFound,
123
        NotAuthorized, NotImplemented {
124
    	// always return true
125
        return true;
126
    }
127
    
128
    
129
    @Override
130
    public SubjectInfo getSubjectInfo(Session session, Subject subject)
131
    throws ServiceFailure, NotAuthorized, NotImplemented, NotFound, InvalidToken {
132
        
133
        return null;
134
    }
135

    
136

    
137
    /* (non-Javadoc)
138
     * @see org.dataone.client.CNode#listSubjects(org.dataone.service.types.v1.Session, String, String, Integer, Integer)
139
     */
140
    @Override
141
    public SubjectInfo listSubjects(Session session, String query, String status, Integer start,
142
            Integer count) throws InvalidRequest, ServiceFailure, InvalidToken, NotAuthorized,
143
            NotImplemented {
144
       
145
        return null;
146
    }
147
    
148
    @Override
149
    public Node getNodeCapabilities(NodeReference nodeId) throws NotImplemented, ServiceFailure {
150
        if(nodeId != null && nodeId.getValue().equals(V1MNNODEID)) {
151
            return getTestV1MN();
152
        } else if (nodeId != null && nodeId.getValue().equals(V2MNNODEID)) {
153
            return getTestV2MN();
154
        } else if (nodeId != null && nodeId.getValue().equals(MockReplicationMNode.NODE_ID)) {
155
            return getReplicationSourceV2MN();
156
        } else {
157
            return getCapabilities();
158
        }
159
    }
160
    
161
    /*
162
     * Create a test mn in this env.
163
     */
164
    private Node getTestMN() {
165
        Node node = new Node();
166
        NodeReference nodeRef = new NodeReference();
167
        nodeRef.setValue("urn:node:test_MN-12346");
168
        node.setIdentifier(nodeRef);
169
        Subject subject = new Subject();
170
        subject.setValue("cn=" + getNodeId() + ",dc=dataone,dc=org");
171
        node.addSubject(subject );
172
        node.setType(NodeType.MN);
173
        return node;
174
    }
175
    
176
    
177
    /*
178
     * Create a v1 mn in this env
179
     */
180
    private Node getTestV1MN() {
181
        Node node = new Node();
182
        NodeReference nodeRef = new NodeReference();
183
        nodeRef.setValue(V1MNNODEID);
184
        node.setIdentifier(nodeRef);
185
        Subject subject = new Subject();
186
        subject.setValue("cn=" + V1MNNODEID + ",dc=dataone,dc=org");
187
        node.addSubject(subject );
188
        node.setType(NodeType.MN);
189
        Service service = new Service();
190
        service.setName("MNRead");
191
        service.setVersion("V1");
192
        service.setAvailable(true);
193
        Services services = new Services();
194
        services.addService(service);
195
        node.setServices(services);
196
        return node;
197
    }
198
    
199
    /*
200
     * Create a v2 mn in this env
201
     */
202
    private Node getTestV2MN() {
203
        Node node = new Node();
204
        NodeReference nodeRef = new NodeReference();
205
        nodeRef.setValue(V2MNNODEID);
206
        node.setIdentifier(nodeRef);
207
        Subject subject = new Subject();
208
        subject.setValue("cn=" + V2MNNODEID + ",dc=dataone,dc=org");
209
        node.addSubject(subject);
210
        node.setType(NodeType.MN);
211
        Service service = new Service();
212
        service.setName("MNRead");
213
        service.setVersion("V1");
214
        service.setAvailable(true);
215
        Service service2 = new Service();
216
        service2.setName("MNRead");
217
        service2.setVersion("V2");
218
        service2.setAvailable(true);
219
        Services services = new Services();
220
        services.addService(service);
221
        services.addService(service2);
222
        node.setServices(services);
223
        return node;
224
    }
225
    
226
    
227
    /*
228
     * Create a v2 mn in this env
229
     */
230
    private Node getReplicationSourceV2MN() {
231
        Node node = new Node();
232
        NodeReference nodeRef = new NodeReference();
233
        nodeRef.setValue(MockReplicationMNode.NODE_ID);
234
        node.setIdentifier(nodeRef);
235
        Subject subject = new Subject();
236
        subject.setValue("cn=" + MockReplicationMNode.NODE_ID + ",dc=dataone,dc=org");
237
        node.addSubject(subject);
238
        node.setType(NodeType.MN);
239
        Service service = new Service();
240
        service.setName("MNRead");
241
        service.setVersion("V1");
242
        service.setAvailable(true);
243
        Service service2 = new Service();
244
        service2.setName("MNRead");
245
        service2.setVersion("V2");
246
        service2.setAvailable(true);
247
        Services services = new Services();
248
        services.addService(service);
249
        services.addService(service2);
250
        node.setServices(services);
251
        return node;
252
    }
253
    
254
    @Override
255
    public boolean setReplicationStatus(Session session, Identifier pid,
256
            NodeReference nodeRef, ReplicationStatus status, BaseException failure)
257
                    throws ServiceFailure, NotImplemented, InvalidToken, NotAuthorized,
258
                    InvalidRequest, NotFound {
259
        return true;
260
    }
261
}
(6-6/11)