Skip to content

provide missing configuration in DatabaseClientFactory.Bean #1020

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Oct 22, 2018

Conversation

anu3990
Copy link
Contributor

@anu3990 anu3990 commented Oct 17, 2018

So we can incorporate your pull request, please share the following:

  • What issue are you addressing with this pull request? provide missing configuration in DatabaseClientFactory.Bean #1018
  • Are you modifying the correct branch? "develop"
  • Have you run unit tests? (See CONTRIBUTING.md)
  • Version of MarkLogic Java Client API (see Readme.txt)
  • Version of MarkLogic Server (see admin gui on port 8001)
  • Java version (java -version)
  • OS and version
  • What Changed: What happened before this change? What happens without this change?

@anu3990 anu3990 requested a review from ehennum October 17, 2018 22:09
Copy link
Contributor

@ehennum ehennum left a comment

Choose a reason for hiding this comment

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

One use case for the Bean is to support dependency injection -- especially in Spring.

The approach taken is to use a zero-argument constructor for the Bean and configure the Bean entirely with setters.

The constructors added in lines 1235 - 1247 seem inconsistent with this approach. Is there another consideration motivating these constructors?

Similar to the other fields of the bean, I believe the newClient() factory on lines 1478 / 1516 should make use of the added fields.

If the Bean has an inconsistent state (for instance, ambiguous configuration for authentication), it may be necessary to throw an error. Possibly that error checking already exists in the makeSecurityContext() method -- I haven't checked.

Copy link
Contributor

@ehennum ehennum left a comment

Choose a reason for hiding this comment

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

Should there be a call to clientFactoryBean.setSecurityContext(clientFactoryContext) before returning clientFactoryBean?

Approved with this change. After rebasing, please set the labels on the issue to test and assign to Ajit.

Thanks for squaring it away.

@anu3990 anu3990 merged commit b59964e into marklogic:develop Oct 22, 2018
anu3990 added a commit that referenced this pull request Oct 22, 2018
anu3990 added a commit that referenced this pull request Oct 22, 2018
@georgeajit
Copy link

Tests added:

 @Test
  public void testDatabaseClientFactoryBean() throws IOException, ParserConfigurationException, SAXException, XpathException, KeyManagementException,
  NoSuchAlgorithmException
  {
	  DatabaseClient client = null;
	  try {
		  DatabaseClientFactory.Bean clientFactoryBean = new DatabaseClientFactory.Bean();
		  clientFactoryBean.setHost(getRestAppServerHostName());
		  clientFactoryBean.setPort(getRestAppServerPort());
		  clientFactoryBean.setConnectionType(getConnType());
		  SecurityContext secContext = new DatabaseClientFactory.DigestAuthContext("rest-admin", "x");

		  clientFactoryBean.setSecurityContext(secContext);
		  client = clientFactoryBean.newClient();

		  String docId[] = { "/foo/test/myFoo1.txt", "/foo/test/myFoo2.txt", "/foo/test/myFoo3.txt" };

		  TextDocumentManager docMgr = client.newTextDocumentManager();
		  DocumentWriteSet writeset = docMgr.newWriteSet();

		  writeset.add(docId[0], new StringHandle().with("This is so foo1"));
		  writeset.add(docId[1], new StringHandle().with("This is so foo2"));
		  writeset.add(docId[2], new StringHandle().with("This is so foo3"));

		  docMgr.write(writeset);
		  assertEquals("Text document write difference", "This is so foo1", docMgr.read(docId[0], new StringHandle()).get());
		  assertEquals("Text document write difference", "This is so foo2", docMgr.read(docId[1], new StringHandle()).get());
		  assertEquals("Text document write difference", "This is so foo3", docMgr.read(docId[2], new StringHandle()).get());
		  docMgr.delete(docId[0], docId[1], docId[2]);
	  } catch (ResourceNotFoundException e) {
		e.printStackTrace();
	}
	  finally {
		  client.release();
	  }
  }
  
  // Verify that DatabaseClient from Bean handles transactions
  @Test
  public void testDBClientFactoryBeanTransaction() throws Exception {
	  DatabaseClient client = null;

	  String filename = "facebook-10443244874876159931";
	  DatabaseClientFactory.Bean clientFactoryBean = new DatabaseClientFactory.Bean();
	  clientFactoryBean.setHost(getRestAppServerHostName());
	  clientFactoryBean.setPort(getRestAppServerPort());
	  clientFactoryBean.setConnectionType(getConnType());
	  SecurityContext secContext = new DatabaseClientFactory.DigestAuthContext("rest-writer", "x");

	  clientFactoryBean.setSecurityContext(secContext);
	  client = clientFactoryBean.newClient();
	 
	  DocumentMetadataHandle metadataHandle = new DocumentMetadataHandle();
	  DocumentMetadataHandle readMetadataHandle = new DocumentMetadataHandle();
	  DocumentMetadataValues metadatavalues = readMetadataHandle.getMetadataValues();
	  Transaction t1 = null;
	  Transaction t2 = null;
	  metadataHandle.getMetadataValues().add("key1", "value1");
	  metadataHandle.getMetadataValues().add("key2", "value2");
	  metadataHandle.getMetadataValues().add("key3", "value3");

	  TextDocumentManager docMgr = client.newTextDocumentManager();
	  String uri = "/trx-jsonhandle-metadatavalues/";
	  String docId = uri + filename;
	  FileInputStream fis = null;
	  Scanner scanner = null;
	  String readContent;
	  File file = null;

	  try {
		  file = new File("src/test/java/com/marklogic/client/functionaltest/data/" + filename);
		  fis = new FileInputStream(file);
		  scanner = new Scanner(fis).useDelimiter("\\Z");
		  readContent = scanner.next();
	  } finally {
		  fis.close();
		  scanner.close();
	  }
	  StringHandle contentHandle = new StringHandle();
	  contentHandle.set(readContent);
	  // write the doc
	  docMgr.writeAs(docId, metadataHandle, contentHandle);
	  DocumentUriTemplate template = docMgr.newDocumentUriTemplate("Text").withDirectory("/trx-jsonhandle-metadatavalues-template/");

	  try {
		  // Trx with metadata values rollback scenario
		  t1 = client.openTransaction();
		  metadataHandle.getMetadataValues().add("keyTrx1", "valueTrx1");
		  docMgr.writeMetadata(docId, metadataHandle, t1);
		  docMgr.readMetadata(docId, readMetadataHandle, t1);
		  assertTrue(" metadata doesnot contain expected string valueTrx1", metadatavalues.containsValue("valueTrx1"));
		  t1.rollback();
		  docMgr.readMetadata(docId, readMetadataHandle);
		  metadatavalues = readMetadataHandle.getMetadataValues();
		  assertFalse(" metadata  contains unexpected string valueTrx1", metadatavalues.containsValue("valueTrx1"));

		  // Trx with metadata values commit scenario
		  t2 = client.openTransaction();
		  metadataHandle.getMetadataValues().add("keyTrx2", "valueTrx2");
		  DocumentDescriptor desc = docMgr.create(template, metadataHandle, contentHandle, t2);
		  String docId1 = desc.getUri();
		  docMgr.read(docId1, readMetadataHandle, contentHandle, t2);
		  assertTrue(" metadata doesnot contain expected string valueTrx2", metadatavalues.containsValue("valueTrx2"));
		  t2.commit();
		  docMgr.readAs(docId1, readMetadataHandle, String.class);
		  metadatavalues = readMetadataHandle.getMetadataValues();
		  assertTrue(" metadata doesnot contains  string 'valueTrx2' after trx commit", metadatavalues.containsValue("valueTrx2"));
		  waitForPropertyPropagate();

		  t1 = t2 = null;
	  } catch (Exception e) {
		  e.printStackTrace();
	  } finally {
		  if (t1 != null) {
			  t1.rollback();
			  t1 = null;

		  } else if (t2 != null) {
			  t2.rollback();
			  t2 = null;
		  }
		  client.release();
	  }
  }

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.

3 participants