opensearch-project / opensearch-net

OpenSearch .NET Client
Apache License 2.0
102 stars 49 forks source link

Add interfaces to OpenSearchClient's namespaces API specifications (fixes #423) #646

Closed mispencer closed 4 months ago

mispencer commented 4 months ago

Description

Say you created a method using IOpenSearchClient:

public void CreateIndex(IOpenSearchClient client, string name) {
    client.Indices.Create(name);
}

Then you want to write a unit test for it. Since you don't want unit tests to depend on external services, you would likely mock out the IOpenSearchClient. So, you might write:

public void CreateIndex_Always_CreatesAnIndex() {
    // arrange
    var clientMock = new Moq.Mock<IOpenSearchClient>();
    clientMock.Setup(i => i.Indices.Create(name, null));
    var name = "Name";

    // act
    _instance.CreateIndex(client, name)

    // assert
    clientMock.Verify(i => i.Indices.Create(name, null), Times.Once);
}

But this doesn't work. IOpenSearchClient is an interface and can be mocked, but IOpenSearchClient.Indices is IndicesNamespace, a concrete class, and concrete classes are not mockable unless they have a public constructor and have visual methods - neither which apply to IndicesNamespace or any other class under OpenSearch.Client.Specification. As such, you get an error on running this test or any test that references or calls code that references any of the namespaced API endpoints.

This PR adds interfaces to all of the auto-generated classes under OpenSearch.Client.Specification, and adjusts IOpenSearchClient and OpenSearchClient to return these interfaces instead of the concrete classes. This allows mocking to work for these classes, so that users of this library can unit test methods using them.

Issues Resolved

423

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license. For more information on following Developer Certificate of Origin and signing off your commits, please check here.

mispencer commented 4 months ago

CHANGELOG and UPGRADING has been adjusted. I am not exactly sure how to layout the UPGRADING change, so feedback on the preferred text and format would be great.

IHttpNamespace has been added.