Skip to content

Commit

Permalink
json-merge-patch, add cadl ranch test (#2550)
Browse files Browse the repository at this point in the history
* json-merge-patch, add cadl ranch test

* refine code

* refactor

* Update JsonMergePatchClientTest.java
  • Loading branch information
haolingdong-msft authored Feb 7, 2024
1 parent 121ef26 commit d938a13
Showing 1 changed file with 69 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.

package com.payload.jsonmergepatch;


import com.payload.jsonmergepatch.models.InnerModel;
import com.payload.jsonmergepatch.models.Resource;
import com.payload.jsonmergepatch.models.ResourcePatch;
import org.junit.jupiter.api.Test;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class JsonMergePatchClientTest {

private final JsonMergePatchClient client = new JsonMergePatchClientBuilder().buildClient();

@Test
public void createAndUpdateResource() {
// create resource
Resource resource = buildResource();
client.createResource(resource);
// update resource
ResourcePatch resourcePatch = buildResourcePatchWithNullProperties();
client.updateResource(resourcePatch);
}

@Test
public void updateOptionalResource() {
ResourcePatch resourcePatch = buildResourcePatchWithNullProperties();
client.updateOptionalResource(resourcePatch);
}

private static Resource buildResource() {
InnerModel innerModel = new InnerModel();
innerModel.setName("InnerMadge");
innerModel.setDescription("innerDesc");
Map<String, InnerModel> map = new HashMap<>();
map.put("key", innerModel);
List<InnerModel> array = Arrays.asList(innerModel);
Resource resource = new Resource("Madge");
resource.setArray(array);
resource.setMap(map);
resource.setDescription("desc");
resource.setIntValue(1);
resource.setFloatValue(1.1);
resource.setInnerModel(innerModel);
resource.setIntArray(Arrays.asList(1, 2, 3));
return resource;
}

private static ResourcePatch buildResourcePatchWithNullProperties() {
ResourcePatch resourcePatch = new ResourcePatch();
resourcePatch.setDescription(null);
resourcePatch.setMap(new HashMap<>());
resourcePatch.getMap().put("key", new InnerModel().setDescription(null));
resourcePatch.getMap().put("key2", null);
resourcePatch.setArray(null);
resourcePatch.setInnerModel(null);
resourcePatch.setIntValue(null);
resourcePatch.setFloatValue(null);
resourcePatch.setInnerModel(null);
resourcePatch.setIntArray(null);
return resourcePatch;
}
}

0 comments on commit d938a13

Please sign in to comment.