-
-
Notifications
You must be signed in to change notification settings - Fork 595
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for attribute converters
Signed-off-by: nscuro <[email protected]>
- Loading branch information
Showing
2 changed files
with
179 additions
and
0 deletions.
There are no files selected for viewing
79 changes: 79 additions & 0 deletions
79
...va/org/dependencytrack/persistence/converter/OrganizationalContactsJsonConverterTest.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,79 @@ | ||
/* | ||
* This file is part of Dependency-Track. | ||
* | ||
* 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright (c) Steve Springett. All Rights Reserved. | ||
*/ | ||
package org.dependencytrack.persistence.converter; | ||
|
||
import org.dependencytrack.model.OrganizationalContact; | ||
import org.junit.Test; | ||
|
||
import java.util.List; | ||
|
||
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class OrganizationalContactsJsonConverterTest { | ||
|
||
@Test | ||
public void testConvertToDatastore() { | ||
final var contact = new OrganizationalContact(); | ||
contact.setName("Foo"); | ||
contact.setEmail("[email protected]"); | ||
contact.setPhone("123456789"); | ||
|
||
assertThatJson(new OrganizationalContactsJsonConverter().convertToDatastore(List.of(contact))) | ||
.isEqualTo(""" | ||
[ | ||
{ | ||
"name": "Foo", | ||
"email": "[email protected]", | ||
"phone": "123456789" | ||
} | ||
] | ||
"""); | ||
} | ||
|
||
@Test | ||
public void testConvertToAttribute() { | ||
final List<OrganizationalContact> contacts = new OrganizationalContactsJsonConverter().convertToAttribute(""" | ||
[ | ||
{ | ||
"name": "Foo", | ||
"email": "[email protected]", | ||
"phone": "123456789" | ||
} | ||
] | ||
"""); | ||
|
||
assertThat(contacts).satisfiesExactly(contact -> { | ||
assertThat(contact.getName()).isEqualTo("Foo"); | ||
assertThat(contact.getEmail()).isEqualTo("[email protected]"); | ||
assertThat(contact.getPhone()).isEqualTo("123456789"); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testConvertToDatastoreNull() { | ||
assertThat(new OrganizationalContactsJsonConverter().convertToDatastore(null)).isNull(); | ||
} | ||
|
||
@Test | ||
public void testConvertToAttributeNull() { | ||
assertThat(new OrganizationalContactsJsonConverter().convertToAttribute(null)).isNull(); | ||
} | ||
|
||
} |
100 changes: 100 additions & 0 deletions
100
...java/org/dependencytrack/persistence/converter/OrganizationalEntityJsonConverterTest.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,100 @@ | ||
/* | ||
* This file is part of Dependency-Track. | ||
* | ||
* 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
* Copyright (c) Steve Springett. All Rights Reserved. | ||
*/ | ||
package org.dependencytrack.persistence.converter; | ||
|
||
import org.dependencytrack.model.OrganizationalContact; | ||
import org.dependencytrack.model.OrganizationalEntity; | ||
import org.junit.Test; | ||
|
||
import java.util.List; | ||
|
||
import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson; | ||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class OrganizationalEntityJsonConverterTest { | ||
|
||
@Test | ||
public void testConvertToDatastore() { | ||
final var contact = new OrganizationalContact(); | ||
contact.setName("Foo"); | ||
contact.setEmail("[email protected]"); | ||
contact.setPhone("123456789"); | ||
|
||
final var entity = new OrganizationalEntity(); | ||
entity.setName("foo"); | ||
entity.setUrls(new String[]{"https://example.com"}); | ||
entity.setContacts(List.of(contact)); | ||
|
||
assertThatJson(new OrganizationalEntityJsonConverter().convertToDatastore(entity)) | ||
.isEqualTo(""" | ||
{ | ||
"name": "foo", | ||
"urls": [ | ||
"https://example.com" | ||
], | ||
"contacts": [ | ||
{ | ||
"name": "Foo", | ||
"email": "[email protected]", | ||
"phone": "123456789" | ||
} | ||
] | ||
} | ||
"""); | ||
} | ||
|
||
@Test | ||
public void testConvertToAttribute() { | ||
final OrganizationalEntity entity = new OrganizationalEntityJsonConverter().convertToAttribute(""" | ||
{ | ||
"name": "foo", | ||
"urls": [ | ||
"https://example.com" | ||
], | ||
"contacts": [ | ||
{ | ||
"name": "Foo", | ||
"email": "[email protected]", | ||
"phone": "123456789" | ||
} | ||
] | ||
} | ||
"""); | ||
|
||
assertThat(entity).isNotNull(); | ||
assertThat(entity.getName()).isEqualTo("foo"); | ||
assertThat(entity.getUrls()).containsOnly("https://example.com"); | ||
assertThat(entity.getContacts()).satisfiesExactly(contact -> { | ||
assertThat(contact.getName()).isEqualTo("Foo"); | ||
assertThat(contact.getEmail()).isEqualTo("[email protected]"); | ||
assertThat(contact.getPhone()).isEqualTo("123456789"); | ||
}); | ||
} | ||
|
||
@Test | ||
public void testConvertToDatastoreNull() { | ||
assertThat(new OrganizationalEntityJsonConverter().convertToDatastore(null)).isNull(); | ||
} | ||
|
||
@Test | ||
public void testConvertToAttributeNull() { | ||
assertThat(new OrganizationalEntityJsonConverter().convertToAttribute(null)).isNull(); | ||
} | ||
|
||
} |