Skip to content

Conversation

sagenschneider
Copy link
Contributor

I may have this wrong, as new to Cosmos DB. Thank you for putting this emulator together, as I run everything Linux. I've been using the Java client to work with this emulator successfully, however can't get the readAllItems(partitionKey) to work.

The above I believe is the fix, as there is no name attribute being provided.

However, if something in the Java client I'm missing, happy to ignore this - though let me know what might be.

Again, many thanks for putting this together.

I may have this wrong, as new to Cosmos DB.   Thank you for putting this emulator together, as I run everything Linux.  I've been using the Java client to work with this emulator successfully, however can't get the readAllItems(partitionKey) to work.

The above I believe is the fix, as there is no name attribute being provided.

However, if something in the Java client I'm missing, happy to ignore this - though let me know what might be.

Again, many thanks for putting this together.
@nkzawa
Copy link
Contributor

nkzawa commented Mar 11, 2021

Hi, thanks for your PR.

Could you share how your partition key configuration on the container and the partitionKey of readAllItems look like? I'd like to know when it has the problem.

@sagenschneider
Copy link
Contributor Author

Java test code is as follows:

		CosmosDatabase database = ...;

		// Create the container
		database.createContainerIfNotExists(TestEntity.class.getSimpleName(), "/partition");
		CosmosContainer container = database.getContainer(TestEntity.class.getSimpleName());

		// Store in container
		TestEntity entity = new TestEntity(UUID.randomUUID().toString(), "Test message");
		container.createItem(entity);

		// Retrieve all items from container (this triggers the failure, I'm believing this PR fixes)
		long itemCount = container.readAllItems(new PartitionKey(new TestEntity().getPartition()), TestEntity.class)
				.stream().count();
		assertEquals(1, itemCount, "Should have the stored item");

with entity:

	@Data
	@NoArgsConstructor
	@AllArgsConstructor
	public static class TestEntity {
		private String id;
		private String message;
		private final String partition = "SINGLE";
	}

The readAllItems results in the following request:

{"query":"SELECT * FROM c WHERE c[\"partition\"] = @pkValue","parameters":[{"name":"@pkValue","value":"SINGLE"}]}

The PR is to fix to find the partition by node.property.value from the below JSON for node:

{"type":"scalar_member_expression","object":{"type":"identifier","name":"c"},"property":{"type":"string_constant","value":"partition"},"computed":true}

Now, must admit I'm still finding my feet with Cosmos DB. So very happy to be pointed out where I might be wrong.

@sagenschneider
Copy link
Contributor Author

Note: I'm also wrapping the start/stop into a JUnit Extension / Rule so that it will start the emulator within NodeJS docker container. This can then be used for testing against. This is available at: https://github.com/officefloor/OfficeFloor/blob/master/officefloor/persistence/officenosql_cosmosdb_test

To get the certificate issues resolved to connect, had to play with the internals to make the connection work: https://github.com/officefloor/OfficeFloor/blob/master/officefloor/persistence/officenosql_cosmosdb_test/src/main/java/net/officefloor/nosql/cosmosdb/test/AbstractCosmosDbJunit.java#L194

This allows tests as follows: https://github.com/officefloor/OfficeFloor/blob/master/officefloor/tutorials/CosmosDbHttpServer/src/test/java/net/officefloor/tutorial/cosmosdbhttpserver/CosmosDbHttpServerTest.java

@nkzawa
Copy link
Contributor

nkzawa commented Mar 12, 2021

Thank you for the information. This PR makes a lot of sense 👍

Also thanks for sharing how you test Java code. Unfortunately there is no document how to use from non-node.js environment since we use only from node.js basically.
For certificates, you might be able to do like https://github.com/vercel/cosmosdb-server/pull/30/files#diff-b335630551682c19a781afebcf4d07bf978fb1f8ac04c6bf87428ed5106870f5R48 (not really tested tho)

Copy link
Contributor

@nkzawa nkzawa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

awesome!

@nkzawa nkzawa merged commit 0d3708a into vercel:master Mar 12, 2021
@nkzawa nkzawa mentioned this pull request Mar 12, 2021
@sagenschneider
Copy link
Contributor Author

Thanks for the info and thanks for merging :)

This is only for testing (never for production). I'm always hesitant of tests creating side effects. The lessening of security in this case for running tests, is for me better than changing security for the whole JVM. But as this should only run on developers machine or CI servers, then possibly not that relevant. Thanks for the additional options though - will keep in mind, as finding the Java client from Azure not have many options for controlling this (compared to other cloud provider SDKs) :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants