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.Service;
41
import org.dataone.service.types.v1.Services;
42
import org.dataone.service.types.v1.Session;
43
import org.dataone.service.types.v1.Subject;
44
import org.dataone.service.types.v1.SubjectInfo;
45
import org.dataone.service.types.v2.Node;
46
import org.dataone.service.types.v2.NodeList;
47
import org.dataone.service.types.v2.SystemMetadata;
48
import org.dataone.service.util.Constants;
49
import org.dataone.service.util.D1Url;
50

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

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

    
134

    
135
    /* (non-Javadoc)
136
     * @see org.dataone.client.CNode#listSubjects(org.dataone.service.types.v1.Session, String, String, Integer, Integer)
137
     */
138
    @Override
139
    public SubjectInfo listSubjects(Session session, String query, String status, Integer start,
140
            Integer count) throws InvalidRequest, ServiceFailure, InvalidToken, NotAuthorized,
141
            NotImplemented {
142
       
143
        return null;
144
    }
145
    
146
    @Override
147
    public Node getNodeCapabilities(NodeReference nodeId) throws NotImplemented, ServiceFailure {
148
        if(nodeId != null && nodeId.getValue().equals(V1MNNODEID)) {
149
            return getTestV1MN();
150
        } else if (nodeId != null && nodeId.getValue().equals(V2MNNODEID)) {
151
            return getTestV2MN();
152
        } else {
153
            return getCapabilities();
154
        }
155
    }
156
    
157
    /*
158
     * Create a test mn in this env.
159
     */
160
    private Node getTestMN() {
161
        Node node = new Node();
162
        NodeReference nodeRef = new NodeReference();
163
        nodeRef.setValue("urn:node:test_MN-12346");
164
        node.setIdentifier(nodeRef);
165
        Subject subject = new Subject();
166
        subject.setValue("cn=" + getNodeId() + ",dc=dataone,dc=org");
167
        node.addSubject(subject );
168
        node.setType(NodeType.MN);
169
        return node;
170
    }
171
    
172
    
173
    /*
174
     * Create a v1 mn in this env
175
     */
176
    private Node getTestV1MN() {
177
        Node node = new Node();
178
        NodeReference nodeRef = new NodeReference();
179
        nodeRef.setValue(V1MNNODEID);
180
        node.setIdentifier(nodeRef);
181
        Subject subject = new Subject();
182
        subject.setValue("cn=" + V1MNNODEID + ",dc=dataone,dc=org");
183
        node.addSubject(subject );
184
        node.setType(NodeType.MN);
185
        Service service = new Service();
186
        service.setName("MNRead");
187
        service.setVersion("V1");
188
        service.setAvailable(true);
189
        Services services = new Services();
190
        services.addService(service);
191
        node.setServices(services);
192
        return node;
193
    }
194
    
195
    /*
196
     * Create a v2 mn in this env
197
     */
198
    private Node getTestV2MN() {
199
        Node node = new Node();
200
        NodeReference nodeRef = new NodeReference();
201
        nodeRef.setValue(V2MNNODEID);
202
        node.setIdentifier(nodeRef);
203
        Subject subject = new Subject();
204
        subject.setValue("cn=" + V2MNNODEID + ",dc=dataone,dc=org");
205
        node.addSubject(subject);
206
        node.setType(NodeType.MN);
207
        Service service = new Service();
208
        service.setName("MNRead");
209
        service.setVersion("V1");
210
        service.setAvailable(true);
211
        Service service2 = new Service();
212
        service2.setName("MNRead");
213
        service2.setVersion("V2");
214
        service2.setAvailable(true);
215
        Services services = new Services();
216
        services.addService(service);
217
        services.addService(service2);
218
        node.setServices(services);
219
        return node;
220
    }
221
}
(5-5/8)