-
Notifications
You must be signed in to change notification settings - Fork 29
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
Fix nullable fields handling with JSON source #58
Open
VictorD-Veolia
wants to merge
3
commits into
data-integrations:develop
Choose a base branch
from
NicolasLefortVeolia:feature/fix-nullable-in-json
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
109 changes: 109 additions & 0 deletions
109
src/test/java/io/cdap/plugin/http/source/common/pagination/page/JSONPageTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
/* | ||
* Copyright © 2022 Cask Data, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); you may not | ||
* use this file except in compliance with the License. You may obtain a copy of | ||
* the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations under | ||
* the License. | ||
*/ | ||
package io.cdap.plugin.http.source.common.pagination.page; | ||
|
||
import com.google.gson.JsonObject; | ||
import io.cdap.cdap.api.data.format.StructuredRecord; | ||
import io.cdap.cdap.api.data.schema.Schema; | ||
import io.cdap.plugin.http.source.batch.HttpBatchSourceConfig; | ||
import io.cdap.plugin.http.source.common.BaseHttpSourceConfig; | ||
import io.cdap.plugin.http.source.common.http.HttpResponse; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
import org.mockito.Mockito; | ||
|
||
import java.util.ArrayList; | ||
|
||
public class JSONPageTest { | ||
|
||
// The input schema | ||
private static final Schema INPUT_SCHEMA = Schema.recordOf("input", | ||
Schema.Field.of("firstName", Schema.nullableOf(Schema.of(Schema.Type.STRING))), | ||
Schema.Field.of("lastName", Schema.nullableOf(Schema.of(Schema.Type.STRING))), | ||
Schema.Field.of("mail", Schema.of(Schema.Type.STRING)), | ||
Schema.Field.of("_id", Schema.of(Schema.Type.STRING)) | ||
); | ||
|
||
private static final String JSON = "\n" + | ||
"{\n" + | ||
" \"_id\": \"the_id_value\",\n" + | ||
" \"mail\": \"[email protected]\",\n" + | ||
" \"firstName\": \"toto\",\n" + | ||
" \"lastName\": \"tata\"\n" + | ||
"}"; | ||
|
||
private static final String JSON_WITH_EMPTY = "\n" + | ||
"{\n" + | ||
" \"_id\": \"the_id_value\",\n" + | ||
" \"mail\": \"[email protected]\"\n" + | ||
"}"; | ||
|
||
static class BaseTestConfig extends HttpBatchSourceConfig { | ||
BaseTestConfig(String referenceName) { | ||
super(referenceName); | ||
this.schema = INPUT_SCHEMA.toString(); | ||
this.url = ""; | ||
this.httpMethod = "GET"; | ||
this.oauth2Enabled = "false"; | ||
this.httpErrorsHandling = "2..:Success,.*:Fail"; | ||
this.retryPolicy = "linear"; | ||
this.maxRetryDuration = 10L; | ||
this.linearRetryInterval = 1L; | ||
this.waitTimeBetweenPages = 0L; | ||
this.connectTimeout = 60; | ||
this.readTimeout = 120; | ||
this.format = "json"; | ||
this.keystoreType = "Java KeyStore (JKS)"; | ||
this.trustStoreType = "Java KeyStore (JKS)"; | ||
this.transportProtocols = "TLSv1.2"; | ||
} | ||
} | ||
|
||
@Test | ||
public void testJSONPageNominal() { | ||
HttpResponse httpResponse = Mockito.mock(HttpResponse.class); | ||
Mockito.when(httpResponse.getBody()).thenReturn(JSON); | ||
BaseTestConfig config = new BaseTestConfig("testJsonPageNominal"); | ||
JsonPage jsonPage = new JsonPage(config, httpResponse); | ||
PageEntry entry = jsonPage.next(); | ||
StructuredRecord outputRecord = entry.getRecord(); | ||
StructuredRecord expectedRecord = StructuredRecord.builder(INPUT_SCHEMA) | ||
.set("_id", "the_id_value") | ||
.set("mail", "[email protected]") | ||
.set("firstName", "toto") | ||
.set("lastName", "tata") | ||
.build(); | ||
|
||
Assert.assertEquals(expectedRecord, outputRecord); | ||
} | ||
|
||
@Test | ||
public void testJSONPageWithEmpty() { | ||
HttpResponse httpResponse = Mockito.mock(HttpResponse.class); | ||
Mockito.when(httpResponse.getBody()).thenReturn(JSON_WITH_EMPTY); | ||
BaseTestConfig config = new BaseTestConfig("testJsonPageWithEmpty"); | ||
JsonPage jsonPage = new JsonPage(config, httpResponse); | ||
PageEntry entry = jsonPage.next(); | ||
StructuredRecord outputRecord = entry.getRecord(); | ||
StructuredRecord expectedRecord = StructuredRecord.builder(INPUT_SCHEMA) | ||
.set("_id", "the_id_value") | ||
.set("mail", "[email protected]") | ||
.build(); | ||
|
||
Assert.assertEquals(expectedRecord, outputRecord); | ||
} | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This should be set to a non-snapshot version