protected boolean exists(final Location location, final String uri) { try { final ClientResponse response = getNexusClient().uri(uri).head(); if (!ClientResponse.Status.OK.equals(response.getClientResponseStatus())) { if (ClientResponse.Status.NOT_FOUND.equals(response.getClientResponseStatus())) { return false; } throw getNexusClient() .convert( new ContextAwareUniformInterfaceException(response) { @Override public String getMessage(final int status) { if (status == Response.Status.NOT_FOUND.getStatusCode()) { return String.format("Inexistent path: %s", location); } return null; } }); } return true; } catch (ClientHandlerException e) { throw getNexusClient().convert(e); } }
protected void download(final Location location, final String uri, final OutputStream target) throws IOException { try { final ClientResponse response = getNexusClient().uri(uri).get(ClientResponse.class); if (!ClientResponse.Status.OK.equals(response.getClientResponseStatus())) { throw getNexusClient() .convert( new ContextAwareUniformInterfaceException(response) { @Override public String getMessage(final int status) { if (status == Response.Status.NOT_FOUND.getStatusCode()) { return String.format("Inexistent path: %s", location); } return null; } }); } try { IOUtil.copy(response.getEntityInputStream(), target); } finally { response.close(); } } catch (ClientHandlerException e) { throw getNexusClient().convert(e); } }
/** * Attaches specified volume to the specified initiator on the host. It is a synchronous * operation. * * @param volumeId the volume id * @param initiator the initiator * @param host the host * @return the volume attachment response * @throws Exception the exception */ public VolumeAttachResponse attachVolume( String volumeId, String initiator, String[] wwpns, String host) throws Exception { _log.info("CinderApi - start attachVolume"); Gson gson = new Gson(); VolumeAttachRequest volumeAttach = new VolumeAttachRequest(); if (initiator != null) { volumeAttach.initializeConnection.connector.initiator = initiator; } else if (wwpns != null) { volumeAttach.initializeConnection.connector.wwpns = Arrays.copyOf(wwpns, wwpns.length); } volumeAttach.initializeConnection.connector.host = host; String volumeAttachmentUri = endPoint.getBaseUri() + String.format( CinderConstants.URI_VOLUME_ACTION, new Object[] {endPoint.getCinderTenantId(), volumeId}); _log.debug("attaching volume to initiator with uri {}", volumeAttachmentUri); String json = gson.toJson(volumeAttach); _log.info("attaching volume with body {}", json); ClientResponse js_response = getClient().postWithHeader(URI.create(volumeAttachmentUri), json); String s = js_response.getEntity(String.class); _log.debug("Got the response {}", s); VolumeAttachResponse response = null; _log.debug("Response status {}", String.valueOf(js_response.getStatus())); if (js_response.getStatus() == ClientResponse.Status.OK.getStatusCode()) { // This means volume attachment request accepted and being processed (Synch opr) try { response = gson.fromJson(s, VolumeAttachResponse.class); } catch (JsonSyntaxException ex) { /** * Some drivers return 'target_wwn' as a string list but some returns it as string. We * observed in practice that IBM SVC returning string which could be a faulty one. In such a * case, we capture the response string in an Alternate Java object and return the details * in default response object. */ VolumeAttachResponseAlt altResponse = gson.fromJson(s, VolumeAttachResponseAlt.class); response = getResponseInVolumeAttachResponseFormat(altResponse); } } else { throw CinderException.exceptions.volumeAttachFailed(s); } _log.info("CinderApi - end attachVolume"); return response; }
/** * Returns the list of snapshots on the end point * * <p>It is a synchronous operation. * * @return */ public SnapshotListResponse listSnapshots() { _log.info("CinderApi - start listSnapshots"); SnapshotListResponse listRes = null; String listSnapshotsUri = endPoint.getBaseUri() + String.format( CinderConstants.URI_LIST_SNAPSHOTS, new Object[] {endPoint.getCinderTenantId()}); ClientResponse js_response = getClient().get(URI.create(listSnapshotsUri)); _log.debug( "uri {} : Response status {}", listSnapshotsUri, String.valueOf(js_response.getStatus())); if (js_response.getStatus() == ClientResponse.Status.OK.getStatusCode()) { String jsonString = js_response.getEntity(String.class); listRes = new Gson().fromJson(jsonString, SnapshotListResponse.class); } _log.info("CinderApi - end listSnapshots"); return listRes; }
@Test public void getBriefRecordsByBBOX_allIntersect() throws SAXException, IOException, FactoryException, TransformException { Client client = mock(Client.class); when(suite.getAttribute(SuiteAttribute.CLIENT.getName())).thenReturn(client); ClientResponse rsp = mock(ClientResponse.class); when(client.handle(any(ClientRequest.class))).thenReturn(rsp); when(rsp.getStatus()).thenReturn(ClientResponse.Status.OK.getStatusCode()); Document rspEntity = docBuilder.parse(this.getClass().getResourceAsStream("/rsp/GetRecordsResponse-full.xml")); BasicSearchTests spy = Mockito.spy(new BasicSearchTests()); Mockito.doReturn(rspEntity) .when(spy) .getResponseEntityAsDocument(any(ClientResponse.class), anyString()); spy.initCommonFixture(testContext); // BOX2D(32.5 -117.6, 34 -115) with CRS EPSG:4326 spy.setExtent(buildEnvelope(1)); spy.setGetEndpoint(URI.create("http://localhost/csw/v3")); spy.getBriefRecordsByBBOX(); }
@Test public void getBriefRecordsByBBOX_noneIntersect() throws SAXException, IOException, FactoryException, TransformException { thrown.expect(AssertionError.class); thrown.expectMessage("The envelopes do not intersect"); Client client = mock(Client.class); when(suite.getAttribute(SuiteAttribute.CLIENT.getName())).thenReturn(client); ClientResponse rsp = mock(ClientResponse.class); when(client.handle(any(ClientRequest.class))).thenReturn(rsp); when(rsp.getStatus()).thenReturn(ClientResponse.Status.OK.getStatusCode()); Document rspEntity = docBuilder.parse(this.getClass().getResourceAsStream("/rsp/GetRecordsResponse-full.xml")); BasicSearchTests spy = Mockito.spy(new BasicSearchTests()); Mockito.doReturn(rspEntity) .when(spy) .getResponseEntityAsDocument(any(ClientResponse.class), anyString()); spy.initCommonFixture(testContext); // BOX2D(472944 5363287, 516011 5456383) with CRS EPSG:32610 spy.setExtent(buildEnvelope(2)); spy.setGetEndpoint(URI.create("http://localhost/csw/v3")); spy.getBriefRecordsByBBOX(); }
@SuppressWarnings({"deprecation", "rawtypes"}) @Test public void testCreateGetUpdateAndDeleteKeyword() throws InterruptedException, JSONException { String accessToken = getAccessToken(this.client1ClientId, this.client1ClientSecret, this.client1RedirectUri); assertNotNull(accessToken); Address address = new Address(); address.setCountry(new Country(Iso3166Country.CR)); address.setVisibility(Visibility.PUBLIC); // Create ClientResponse response = memberV2ApiClient.createAddress(user1OrcidId, address, accessToken); assertNotNull(response); assertEquals(ClientResponse.Status.CREATED.getStatusCode(), response.getStatus()); Map map = response.getMetadata(); assertNotNull(map); assertTrue(map.containsKey("Location")); List resultWithPutCode = (List) map.get("Location"); String location = resultWithPutCode.get(0).toString(); Long putCode = Long.valueOf(location.substring(location.lastIndexOf('/') + 1)); // Get all and verify response = memberV2ApiClient.viewAddresses(user1OrcidId, accessToken); assertEquals(Response.Status.OK.getStatusCode(), response.getStatus()); Addresses addresses = response.getEntity(Addresses.class); assertNotNull(addresses); assertNotNull(addresses.getAddress()); assertEquals(2, addresses.getAddress().size()); boolean foundUS = false; boolean foundCR = false; for (Address add : addresses.getAddress()) { assertEquals(Visibility.PUBLIC, add.getVisibility()); assertNotNull(add.getCountry()); assertNotNull(add.getCountry().getValue()); if (add.getCountry().getValue().equals(Iso3166Country.US)) { foundUS = true; } else if (add.getCountry().getValue().equals(Iso3166Country.CR)) { foundCR = true; } } assertTrue(foundUS); assertTrue(foundCR); // Get it response = memberV2ApiClient.viewAddress(user1OrcidId, putCode, accessToken); assertNotNull(response); address = response.getEntity(Address.class); assertNotNull(address); assertNotNull(address.getSource()); assertEquals(client1ClientId, address.getSource().retrieveSourcePath()); assertNotNull(address.getCountry()); assertNotNull(address.getCountry().getValue()); assertEquals(Iso3166Country.CR, address.getCountry().getValue()); assertEquals(Visibility.PUBLIC, address.getVisibility()); // Update address.getCountry().setValue(Iso3166Country.PA); response = memberV2ApiClient.updateAddress(user1OrcidId, address, accessToken); assertNotNull(response); assertEquals(ClientResponse.Status.OK.getStatusCode(), response.getStatus()); response = memberV2ApiClient.viewAddress(user1OrcidId, putCode, accessToken); assertNotNull(response); Address updatedAddress = response.getEntity(Address.class); assertNotNull(updatedAddress); assertNotNull(updatedAddress.getCountry()); assertEquals(Iso3166Country.PA, updatedAddress.getCountry().getValue()); assertEquals(address.getPutCode(), updatedAddress.getPutCode()); // Delete response = memberV2ApiClient.deleteAddress(user1OrcidId, putCode, accessToken); assertNotNull(response); assertEquals(ClientResponse.Status.NO_CONTENT.getStatusCode(), response.getStatus()); // Check it was deleted testGetAddressWithMembersAPI(); }