Can't stubbing a query with WireMock - java

The problem is that my query like:
/pc/odata/api/devices?$top=3000&$skip=0&$format=json&$expand=groups&$select=ID,Name,PrimaryIPAddress,groups/GroupPathLocation&$filter=((groups/Name%20eq%20'OI%20Inventory%20Group'))&timeout=120
can't use for stub with
HTTP ERROR 500
Problem accessing /__files/pc/odata/api/devices.
Reason:
Illegal character in query at index 138:
/not-matched?$top=3000&$skip=0&$format=json&$expand=groups&$select=ID,Name,PrimaryIPAddress,groups/GroupPathLocation&$filter=((groups/Name
eq 'OI Inventory Group'))&timeout=120
The request is valid, and app works with the same query without params.
A method for stub:
public void initialize( Integer D_ITEM_COUNT ) {
writedItemsFile( D_ITEM_COUNT );
ClasspathFileSource responseFiles = new ClasspathFileSource( "." );
dItemService = new WireMockServer( WireMockConfiguration.wireMockConfig()
.port( D_ITEMS_SERVICE_PORT )
.fileSource( responseFiles )
.enableBrowserProxying( false )
.notifier( new ConsoleNotifier( true ) ) );
dItemService.givenThat( get( urlEqualTo( D_D_ITEMS_SERVICE_ENDPOINT ) ).
willReturn( WireMock.aResponse()
.withHeader("Content-Type", "application/json")
.withBodyFile( "../" + D_ITEMS_SERVICE_RESPONSE_FILE + FILE_EXTENSION )
.withStatus( HttpStatus.SC_OK ) ) );
}

Related

How to implement bulk updateOne with mongodb in java

I'm trying to implement bulk update for a list of objects. But I'm getting the following error
java.lang.IllegalArgumentException: Invalid BSON field name bookingId
at org.bson.AbstractBsonWriter.writeName(AbstractBsonWriter.java:532)
at org.bson.codecs.BsonDocumentCodec.encode(BsonDocumentCodec.java:114)
at org.bson.codecs.BsonDocumentCodec.encode(BsonDocumentCodec.java:41)
at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:60)
at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:29)
at com.mongodb.operation.BulkWriteBatch$WriteRequestEncoder.encode(BulkWriteBatch.java:398)
at com.mongodb.operation.BulkWriteBatch$WriteRequestEncoder.encode(BulkWriteBatch.java:377)
at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:63)
at org.bson.codecs.BsonDocumentWrapperCodec.encode(BsonDocumentWrapperCodec.java:29)
The implementation for the updates are as follows:
public void _deleteAll( ArrayList<T> pojos ) {
Assert.notNull( pojos, "Entity must not be null!" );
BulkOperations ops = mongoTemplate.bulkOps( BulkOperations.BulkMode.ORDERED, className, collectionName );
for ( T pojo : pojos ) {
System.out.println( ( ( BaseEntity ) pojo ).get_id() );
( ( BaseEntity ) pojo ).setIsDeleted( true );
Query query = new Query( Criteria.where( "_id" ).is( ( ( BaseEntity ) pojo ).get_id() ) );
Document document = Document.parse( pojo.toString() ); //toString() returns JSON string
Update update = Update.fromDocument( document );
ops.updateOne( query, update );
}
ops.execute();
}
Second approach with WriteModel
public void _deleteAll( ArrayList<T> pojos ) {
MongoCollection<Document> collection = mongoTemplate.getCollection( collectionName );
List<WriteModel<Document>> writes = new ArrayList<>();
for ( T pojo : pojos ) {
( ( BaseEntity ) pojo ).setIsDeleted( true );
writes.add(
new UpdateOneModel<Document>(
new Document().append( "_id", ( ( BaseEntity ) pojo ).get_id() ), Document.parse( pojo.toString() )
)
);
}
collection.bulkWrite( writes );
}
The JSON data is similar to this:
{
"_id" : ObjectId("5d81d87ac290c518433e66d9"),
"bookingId" : 100344391,
"body" : "{\"title\”:\"Some title\”,\”booking_room_id\":null,\”message\”:\"some message\”}",
“nestedObject" : {
“someKey" : “some value"
},
"created_at" : ISODate("2019-09-18T07:10:50.067Z"),
"updated_at" : ISODate("2019-09-18T07:28:16.062Z"),
"isDeleted" : false,
"_class" : "com.test.mongo_test.entity.mongo.SomePojo"
}
Found the answer here. Basically what I had to do was add the query command $set in the document.
writes.add(
new UpdateOneModel<>(
new Document( "_id", new ObjectId( id ) ),
new Document( "$set", Document.parse( pojo.toString() ) )
)
);

Writing a Jagged Array in HDF5 using the Java Native Library

I have tried numerous ways and followed some of the examples that are scattered around the web on how to write a jagged array (an array of arrays that may be of differing lengths) in HDF5.
Most of the examples are in C and rather low-level. Anyhow I can't seem to get it working and I just looked at the C-source code and it pretty much says that any variable-length datatypes that are not strings are not supported (if I understood correctly).
My miserable dysfunctional code (as is):
public void WIP_createVLenFloatDataSet( List<? extends Number> floats ) throws Exception
{
String group = "/test";
long groupId = createGroupIfNotExist( group );
MDataQualifier qualifier = new MDataQualifierImpl( group, "float", "0.0.0" );
long datasetId = openDataSet( qualifier );
long heapType = H5.H5Tcopy( MDataType.FLOAT_ARRAY.getHDFType() );
heapType = H5.H5Tvlen_create( heapType );
// heapType = H5.H5Tarray_create( heapType, 1, new long[]{1} );
if( !exists( datasetId ) )
{
long[] maxDims = new long[]{ HDF5Constants.H5S_UNLIMITED };
long dataspaceId = H5.H5Screate_simple( 1, new long[]{ 1 }, null );
// Create the dataset.
long datasetId1 = -1;
try
{
if( exists( m_fileId ) && exists( dataspaceId ) && exists( heapType ) )
{
long creationProperties = H5.H5Pcreate( HDF5Constants.H5P_DATASET_CREATE );
H5.H5Pset_chunk( creationProperties, /*ndims*/1, new long[]{ 1 } );
datasetId1 = H5.H5Dcreate( groupId, qualifier.getVersionedName(), heapType, dataspaceId, H5P_DEFAULT, creationProperties, H5P_DEFAULT );
// H5.H5Pclose( creationProperties );
}
}
catch( Exception e )
{
LOG.error( "Problems creating the dataset: " + e.getMessage(), e );
}
datasetId = datasetId1;
if( exists( datasetId ) )
{
// flushIfNecessary();
LOG.trace( "Wrote empty dataset {}", qualifier.getVersionedName() );
}
}
List<? extends Number> data = ( List<? extends Number> )floats;
// H5.H5Dwrite( datasetId, heapType, dataspaceId, memSpaceId, HDF5Constants.H5P_DEFAULT, Floats.toArray( data) );
ByteBuffer bb = ByteBuffer.allocate( data.size() * 4 );
floats.forEach( f -> bb.putFloat( f.floatValue() ) );
// H5.H5Dwrite( datasetId, heapType, H5S_ALL, H5S_ALL, H5P_DEFAULT, Floats.toArray( data ) );
H5.H5Dwrite( datasetId, heapType, H5S_ALL, H5S_ALL, H5P_DEFAULT, bb.array() );
}
Has anyone done this before and can at least confirm that it's not possible?
The most I can get out of HDF5 is the message "buf does not support variable length type".
Apparently the "glue code" of the JNI wrapper doesn't support this. If you want to use this feature you either have to implement your own JNI or wait for a newer version. The official JNI code is open source and can be found here.

Null Pointer Exception when replacing character " with no text then casting value to Float

I am writing a java program fetching JSON data through HTTP GET methods and it returns the following after I navigate the object tree:
{
"year":"2015",
"period":"M03",
"periodName":"March",
"value":"141178",
"footnotes":[{}]
}
Now I want to take value and caste it to a float, I tried to do this like such:
JSONParser parser = new JSONParser();
try
{
JSONObject
BLSemployment = ( JSONObject ) parser.parse( _RDATA );
BLSemployment = ( ( JSONObject ) BLSemployment.get( "Results" ) );
JSONArray
BLSemploymentseries = ( ( JSONArray ) BLSemployment.get( "series" ) );
BLSemployment = ( ( JSONObject ) BLSemploymentseries.get( 0 ) );
BLSemploymentseries = ( ( JSONArray ) BLSemployment.get( "data" ) );
for( int i = 0; i < 12; i++ )
{
BLSemployment = ( (JSONObject) BLSemploymentseries.get( i ) );
HistoricalNonFarmPayrollData[i] = Float.parseFloat( JSONValue.toJSONString( BLSemployment.get( "value" ) ).replace( "\"" , "" ) );
HistoricalNonFarmPayrollYear[i] = JSONValue.toJSONString( BLSemployment.get( "year" ) );
HistoricalNonFarmPayrollMonth[i] = JSONValue.toJSONString( BLSemployment.get( "periodName" ) );
}
}
catch ( ParseException pe )
{
System.out.println( pe );
}
However Now I get the error:
Exception in thread "main" java.lang.NullPointerException
at BLSFramework.getNonFarmPayrolls(playground.java:354)
at playground.main(playground.java:27)
RESOLVED: occasionally BLS website doesn't send data for periods of time and I was coding during that period of time.

copy db operation with mongo java driver when auth is enabled

I need to do a copydb operation using mongo java driver. This is my code to do that
String nonce = mongo.getDB("admin").command(new BasicDBObject("copydbgetnonce","1")).get("nonce").toString();
String username = "admin";
String password = "password";
String key = md5(nonce + username + md5(username + ":mongo:" + password));
DBObject copyOp = new BasicDBObject("copydb", "1").
append("fromdb" , "db1").
append("todb" , "db2").
append("username" , username).
append("nonce" , nonce).
append("key" , key);
mongo.getDB("admin").command(copyOp);
It was working when authentication is disabled on the server. With the authentication it fails with unauthorized result.
{ "serverUsed" : "localhost/127.0.0.1:27017" , "ok" : 0.0 , "errmsg" : "unauthorized"}
I can assure that password and username is correct. Please point me to the correct direction .
PS my mongod instance is tokumx 1.5
Thanks.
Does this work for you?
final DBObject cmd = new BasicDBObject( );
cmd.put( "copydb", "1" );
cmd.put( "slaveOk", true );
cmd.put( "fromdb", "db1" );
cmd.put( "todb", "db2" );
cmd.put( "fromhost", "fromHost" );
BasicDBObject nonceCmd = new BasicDBObject( );
nonceCmd.put( "copydbgetnonce", 1 );
nonceCmd.put( "fromhost", "fromHost" );
final CommandResult nonceResult = mongo.getDB( "admin" ).command( nonceCmd );
final String nonce = nonceResult.getString( "nonce" );
final byte[ ] innerHex =( getUserName( ) + ":mongo:" + String.valueOf( getPassword( ) ) ).getBytes( );
final byte[ ] outerHex = ( nonce + getUserName( ) + Util.hexMD5( innerHex ) ).getBytes( );
cmd.put( "username", getUserName( ) );
cmd.put( "nonce", nonce );
cmd.put( "key", Util.hexMD5( outerHex ) );
final CommandResult res = mongo.getDB( "admin" ).command( cmd );
if ( !res.ok( ) )
{
throw res.getException( ) ;
}

javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated when trying to session.getPeerCertificateChain()

I am pretty new to working with http/certs/etc. I have my own hostnameVerifier set up for an apache httpclient. (Trying to allow FQDN to not match as long as cert FQDN is still internal to our domain - not the best I know - but better than nothing)
It works fine for most of the servers I have tested with, but there are a couple where I get
javax.net.ssl.SSLPeerUnverifiedException: peer not authenticated
when I try to
session.getPeerCertificateChain().
I am able to view the certificate just fine when I go to the same link in firefox.
Below is the full code for the method.
#Override
public void verify( String arg0, SSLSocket arg1 ) throws IOException
{
String certFQDN = null;
SSLSession session = arg1.getSession();
javax.security.cert.X509Certificate[] certs = session.getPeerCertificateChain();
if ( certs.length > 0 )
{
String name = certs[ 0 ].getSubjectDN().getName();
for ( String s : name.split( "," ) )
{
String part[] = s.split( "=" );
if ( part[ 0 ].trim().equals( "CN" ) )
{
certFQDN = part[ 1 ].trim();
}
}
}
else
{
throw new IOException( "Could not find certificate chain." );
}
if ( !certFQDN.substring( certFQDN.length() - 11 ).equals( ".domain.com" ) )
{
throw new SSLException( "Not an internal host: " + certFQDN );
}
}
#Override public void verify( String arg0, SSLSocket arg1 ) throws IOException {
arg1.getSession();
javax.security.cert.X509Certificate[] certs = session.getPeerCertificateChain();
].getSubjectDN().getName();
) )
);
].trim().equals( "CN" ) )
].trim();
not find certificate chain." );
certFQDN.length() - 11 ).equals( ".domain.com" ) )
internal host: " + certFQDN );
}

Categories