mardi 22 septembre 2020

How to test body and properties of Micronaut controller

I am a Micronaut/Java beginner and I am trying to design some tests for my controllers. I could not find many examples online so here is my question. Below is the controller with 2 @GET requests:

@Controller("/api/v1") 
public class MyController {

private final ClientNetworkList ClientNetworkList ;

public MyController( 
ClientNetworkList ClientNetworkList,
ClientStatus ClientStatus
){
    this.ClientNetworkList = ClientNetworkList;
    this.ClientStatus = ClientStatus;

    @Get(uri = "/networkList", produces = MediaType.APPLICATION_JSON_STREAM)
    Flowable<NetworkListPackage> packagesNetworkList() { 
    return ClientNetworkList.fetchPackages();
    }

    @Get(uri = "/channels/{stringParm}/status/", produces = MediaType.APPLICATION_JSON_STREAM)
    Flowable<ChannelStatusPackage> packagesStatus(stringParm) { 
    return ClientStatus.fetchPackages(genesis);
    }
}

The java object POJOs:

@Introspected
})
public class NetworkListPackage {

    private List<NetworkList> networkList = null;
    @JsonIgnore
    private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    public List<NetworkList> getNetworkList() {
    return networkList;
    }

    public void setNetworkList(List<NetworkList> networkList) {
    this.networkList = networkList;
    }

    public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
    }

    public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
    }
}

public class NetworkList {

private String name;
private Boolean authEnabled;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    public String getName() {
    return name;
    }

    public void setName(String name) {
    this.name = name;
    }

    public Boolean getAuthEnabled() {
    return authEnabled;
    }

    public void setAuthEnabled(Boolean authEnabled) {
    this.authEnabled = authEnabled;
    }

    public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
    }

    public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
    }
}

@Introspected
public class ChannelStatusPackage {

private String chaincodeCount;
private String txCount;
private String latestBlock;
private String peerCount;
private Map<String, Object> additionalProperties = new HashMap<String, Object>();

    public String getChaincodeCount() {
    return chaincodeCount;
    }

    public void setChaincodeCount(String chaincodeCount) {
    this.chaincodeCount = chaincodeCount;
    }

    public String getTxCount() {
    return txCount;
    }

    public void setTxCount(String txCount) {
    this.txCount = txCount;
    }

    public String getLatestBlock() {
    return latestBlock;
    }

    public void setLatestBlock(String latestBlock) {
    this.latestBlock = latestBlock;
    }

    public String getPeerCount() {
    return peerCount;
    }

    public void setPeerCount(String peerCount) {
    this.peerCount = peerCount;
    }

    public Map<String, Object> getAdditionalProperties() {
    return this.additionalProperties;
    }

    public void setAdditionalProperty(String name, Object value) {
    this.additionalProperties.put(name, value);
    }
}

And the potential tests:

@MicronautTest 
class MyControllerTest {

    @Inject
    @Client("/")
    RxStreamingHttpClient client; 

    @Test
    public void verifyChannelStatusPackagesCanBeFetchedWithCompileTimeAutoGeneratedAtClient() {
        //when:
        HttpRequest request = HttpRequest.GET("/api/v1/channels/{stringParam}/status/");

        Flowable<ChannelStatusPackage> channelStatusPackageStream = client.jsonStream(request, ChannelStatusPackage.class); 
        Iterable<ChannelStatusPackage> channelStatusPackages = channelStatusPackageStream.blockingIterable();

        //then:
        //How to assert the returned body compared to the POJO?
        //How to handle the parameter in the request url?


    @Test
    public void verifyNetworkListPackagesCanBeFetchedWithCompileTimeAutoGeneratedAtClient() {
        //when:
        HttpRequest request = HttpRequest.GET("/api/v1/networkList");

        Flowable<NetworkListPackage> networkListPackageStream = client.jsonStream(request, NetworkListPackage.class); 
        Iterable<NetworkListPackage> networkListPackages = networkListPackageStream.blockingIterable();

        //then:
        //How to assert the returned body and compared to the POJO?
        //How to assert the returned properties ?
    }
}

Based on the previous code, how can I test that the returned bodies and properties of the requests matches the POJOs? What are the usual test to be carried out?

Thank you very much for helping.

Aucun commentaire:

Enregistrer un commentaire