diff options
author | waynedunican <wayne.dunican@est.tech> | 2020-07-07 17:51:42 +0100 |
---|---|---|
committer | waynedunican <wayne.dunican@est.tech> | 2020-07-17 09:44:03 +0100 |
commit | 4752a9ede61f2df4e67ec18f6c53cdcd0c35885f (patch) | |
tree | 29374ab930d63747cd9fff119d598184930a056d /context | |
parent | 28eced32d2acdf731f7782e14f37bf2ea8c98019 (diff) |
Replace try/catch with assertj - apex-pdp
Replaced try/catch blocks in apex-pdp with assertj assertions. Part II
of changes
Issue-ID: POLICY-2451
Change-Id: I29823054fbeb6d1a287d1234859aaf2d4609ef6d
Signed-off-by: waynedunican <wayne.dunican@est.tech>
Diffstat (limited to 'context')
-rw-r--r-- | context/context-management/src/test/java/org/onap/policy/apex/context/impl/ContextAlbumImplTest.java | 212 |
1 files changed, 63 insertions, 149 deletions
diff --git a/context/context-management/src/test/java/org/onap/policy/apex/context/impl/ContextAlbumImplTest.java b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/ContextAlbumImplTest.java index 51802919c..5aaa8a38a 100644 --- a/context/context-management/src/test/java/org/onap/policy/apex/context/impl/ContextAlbumImplTest.java +++ b/context/context-management/src/test/java/org/onap/policy/apex/context/impl/ContextAlbumImplTest.java @@ -21,10 +21,10 @@ package org.onap.policy.apex.context.impl; +import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertTrue; -import static org.junit.Assert.fail; import java.util.LinkedHashMap; import java.util.Map; @@ -33,14 +33,12 @@ import org.junit.BeforeClass; import org.junit.Test; import org.onap.policy.apex.context.ContextAlbum; import org.onap.policy.apex.context.ContextException; -import org.onap.policy.apex.context.ContextRuntimeException; import org.onap.policy.apex.context.Distributor; import org.onap.policy.apex.context.impl.distribution.jvmlocal.JvmLocalDistributor; import org.onap.policy.apex.context.impl.schema.java.JavaSchemaHelperParameters; import org.onap.policy.apex.context.parameters.ContextParameterConstants; import org.onap.policy.apex.context.parameters.ContextParameters; import org.onap.policy.apex.context.parameters.SchemaParameters; -import org.onap.policy.apex.model.basicmodel.concepts.ApexRuntimeException; import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey; import org.onap.policy.apex.model.basicmodel.concepts.AxConcept; import org.onap.policy.apex.model.basicmodel.concepts.AxReferenceKey; @@ -93,42 +91,19 @@ public class ContextAlbumImplTest { @Test public void testNullsOnConstructor() { - try { - new ContextAlbumImpl(null, null, null); - fail("this test should throw an exception"); - } catch (IllegalArgumentException e) { - assertEquals("Context album definition may not be null", e.getMessage()); - } catch (ContextException e) { - fail("this test should throw an IllegalArgumentException"); - } - - try { - new ContextAlbumImpl(new AxContextAlbum(), null, null); - fail("this test should throw an exception"); - } catch (IllegalArgumentException e) { - assertEquals("Distributor may not be null", e.getMessage()); - } catch (ContextException e) { - fail("this test should throw an IllegalArgumentException"); - } - - try { - new ContextAlbumImpl(new AxContextAlbum(), new JvmLocalDistributor(), null); - fail("this test should throw an exception"); - } catch (IllegalArgumentException e) { - assertEquals("Album map may not be null", e.getMessage()); - } catch (ContextException e) { - fail("this test should throw an IllegalArgumentException"); - } - - try { - new ContextAlbumImpl(new AxContextAlbum(), new JvmLocalDistributor(), new LinkedHashMap<String, Object>()); - fail("this test should throw an exception"); - } catch (ApexRuntimeException e) { - assertEquals("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas " - + "not found in model service", e.getMessage()); - } catch (ContextException e) { - fail("this test should throw an ApexRuntimeException"); - } + assertThatThrownBy(() -> new ContextAlbumImpl(null, null, null)) + .hasMessage("Context album definition may not be null"); + + assertThatThrownBy(() -> new ContextAlbumImpl(new AxContextAlbum(), null, null)) + .hasMessage("Distributor may not be null"); + + assertThatThrownBy(() -> new ContextAlbumImpl(new AxContextAlbum(), new JvmLocalDistributor(), null)) + .hasMessage("Album map may not be null"); + + assertThatThrownBy(() -> new ContextAlbumImpl(new AxContextAlbum(), new JvmLocalDistributor(), + new LinkedHashMap<String, Object>())) + .hasMessage("Model for org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas " + + "not found in model service"); } @Test @@ -142,107 +117,63 @@ public class ContextAlbumImplTest { AxContextAlbum axContextAlbum = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy", true, AxArtifactKey.getNullKey()); - try { - new ContextAlbumImpl(axContextAlbum, new JvmLocalDistributor(), new LinkedHashMap<String, Object>()); - fail("this test should throw an exception"); - } catch (ContextException e) { - assertEquals("could not initiate schema management for context album AxContextAlbum", - e.getMessage().substring(0, 69)); - } + assertThatThrownBy(() -> new ContextAlbumImpl(axContextAlbum, new JvmLocalDistributor(), + new LinkedHashMap<String, Object>())) + .hasMessageContaining("could not initiate schema management for context album AxContextAlbum"); axContextAlbum.setItemSchema(simpleStringSchema.getKey()); Distributor distributor = new JvmLocalDistributor(); distributor.init(axContextAlbum.getKey()); ContextAlbum album = new ContextAlbumImpl(axContextAlbum, distributor, new LinkedHashMap<String, Object>()); - AxContextAlbum axContextAlbumRo = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy", - false, simpleStringSchema.getKey()); - ContextAlbum albumRo = new ContextAlbumImpl(axContextAlbumRo, distributor, new LinkedHashMap<String, Object>()); - assertEquals("TestContextAlbum", album.getName()); assertEquals("TestContextAlbum:0.0.1", album.getKey().getId()); assertEquals("TestContextAlbum:0.0.1", album.getAlbumDefinition().getId()); assertEquals("SimpleStringSchema:0.0.1", album.getSchemaHelper().getSchema().getId()); - try { - album.containsKey(null); - fail("test should throw an exception"); - } catch (ContextRuntimeException e) { - assertEquals("null values are illegal on method parameter \"key\"", e.getMessage()); - } + assertThatThrownBy(() -> album.containsKey(null)) + .hasMessage("null values are illegal on method parameter \"key\""); assertEquals(false, album.containsKey("Key0")); - try { - album.containsValue(null); - fail("test should throw an exception"); - } catch (ContextRuntimeException e) { - assertEquals("null values are illegal on method parameter \"value\"", e.getMessage()); - } + assertThatThrownBy(() -> album.containsValue(null)) + .hasMessage("null values are illegal on method parameter \"value\""); assertEquals(false, album.containsValue("some value")); - try { - album.get(null); - fail("test should throw an exception"); - } catch (ContextRuntimeException e) { - assertEquals("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for get()", e.getMessage()); - } - - try { - album.put(null, null); - fail("test should throw an exception"); - } catch (ContextRuntimeException e) { - assertEquals("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for put()", e.getMessage()); - } - - try { - album.put("KeyNull", null); - fail("test should throw an exception"); - } catch (ContextRuntimeException e) { - assertEquals("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"KeyNull\" for put()", - e.getMessage()); - } - - try { - albumRo.put("KeyReadOnly", "A value for a Read Only Album"); - fail("test should throw an exception"); - } catch (ContextRuntimeException e) { - assertEquals("album \"TestContextAlbum:0.0.1\" put() not allowed on read only albums " - + "for key=\"KeyReadOnly\", value=\"A value for a Read Only Album", e.getMessage()); - } + assertThatThrownBy(() -> album.get(null)) + .hasMessage("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for get()"); + + assertThatThrownBy(() -> album.put(null, null)) + .hasMessage("album \"TestContextAlbum:0.0.1\" null keys are illegal on keys for put()"); + + assertThatThrownBy(() -> album.put("KeyNull", null)) + .hasMessage("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"KeyNull\"" + + " for put()"); + + AxContextAlbum axContextAlbumRo = new AxContextAlbum(new AxArtifactKey("TestContextAlbum", "0.0.1"), "Policy", + false, simpleStringSchema.getKey()); + ContextAlbum albumRo = new ContextAlbumImpl(axContextAlbumRo, distributor, new LinkedHashMap<String, Object>()); + + assertThatThrownBy(() -> albumRo.put("KeyReadOnly", "A value for a Read Only Album")) + .hasMessage("album \"TestContextAlbum:0.0.1\" put() not allowed on read only albums " + + "for key=\"KeyReadOnly\", value=\"A value for a Read Only Album"); Map<String, Object> putAllData = new LinkedHashMap<>(); putAllData.put("AllKey0", "vaue of AllKey0"); putAllData.put("AllKey1", "vaue of AllKey1"); putAllData.put("AllKey2", "vaue of AllKey2"); - try { - albumRo.putAll(putAllData); - fail("test should throw an exception"); - } catch (ContextRuntimeException e) { - assertEquals("album \"TestContextAlbum:0.0.1\" putAll() not allowed on read only albums", e.getMessage()); - } - - try { - albumRo.remove("AllKey0"); - fail("test should throw an exception"); - } catch (ContextRuntimeException e) { - assertEquals("album \"TestContextAlbum:0.0.1\" remove() not allowed " - + "on read only albums for key=\"AllKey0\"", e.getMessage()); - } - - try { - album.remove(null); - fail("test should throw an exception"); - } catch (ContextRuntimeException e) { - assertEquals("null values are illegal on method parameter \"keyID\"", e.getMessage()); - } - - try { - albumRo.clear(); - fail("test should throw an exception"); - } catch (ContextRuntimeException e) { - assertEquals("album \"TestContextAlbum:0.0.1\" clear() not allowed on read only albums", e.getMessage()); - } + assertThatThrownBy(() -> albumRo.putAll(putAllData)) + .hasMessage("album \"TestContextAlbum:0.0.1\" putAll() not allowed on read only albums"); + + assertThatThrownBy(() -> albumRo.remove("AllKey0")) + .hasMessage("album \"TestContextAlbum:0.0.1\" remove() not allowed " + + "on read only albums for key=\"AllKey0\""); + + assertThatThrownBy(() -> album.remove(null)) + .hasMessage("null values are illegal on method parameter \"keyID\""); + + assertThatThrownBy(albumRo::clear) + .hasMessage("album \"TestContextAlbum:0.0.1\" clear() not allowed on read only albums"); // The following locking tests pass because the locking protects access to Key0 across all // copies of the distributed album whether the key exists or not @@ -286,13 +217,8 @@ public class ContextAlbumImplTest { // The flush() operation fails because the distributor is not initialized with the album which // is fine for unit test - try { - album.flush(); - fail("test should throw an exception"); - } catch (ContextException e) { - assertEquals("map flush failed, supplied map is null", e.getMessage()); - } - + assertThatThrownBy(album::flush) + .hasMessage("map flush failed, supplied map is null"); assertEquals(1, album.size()); assertEquals(false, album.isEmpty()); @@ -302,14 +228,9 @@ public class ContextAlbumImplTest { album.putAll(putAllData); putAllData.put("AllKey3", null); - try { - album.putAll(putAllData); - fail("test should throw an exception"); - } catch (ContextRuntimeException e) { - assertEquals("album \"TestContextAlbum:0.0.1\" null values are illegal on key \"AllKey3\" for put()", - e.getMessage()); - } - + assertThatThrownBy(() -> album.putAll(putAllData)) + .hasMessage("album \"TestContextAlbum:0.0.1\" null values are illegal on key " + + "\"AllKey3\" for put()"); assertEquals("New value of Key0", album.remove("Key0")); album.clear(); @@ -353,15 +274,13 @@ public class ContextAlbumImplTest { otherAlbum.put("Key", 123); assertNotEquals(album, otherAlbum); - try { - otherAlbum.put("Key", "BadValue"); - fail("test should throw an exception here"); - } catch (ContextRuntimeException cre) { - assertEquals("Failed to set context value for key \"Key\" in album \"TestContextAlbum:0.0.1\": " + assertThatThrownBy(() -> { + ContextAlbumImpl otherAlbumBad = new ContextAlbumImpl(axContextAlbum, distributor, + new LinkedHashMap<String, Object>()); + otherAlbumBad.put("Key", "BadValue"); + }).hasMessage("Failed to set context value for key \"Key\" in album \"TestContextAlbum:0.0.1\": " + "TestContextAlbum:0.0.1: object \"BadValue\" of class \"java.lang.String\" " - + "not compatible with class \"java.lang.Integer\"", cre.getMessage()); - } - + + "not compatible with class \"java.lang.Integer\""); AxContextAlbum otherAxContextAlbum = new AxContextAlbum(new AxArtifactKey("OtherTestContextAlbum", "0.0.1"), "Policy", true, AxArtifactKey.getNullKey()); @@ -369,13 +288,8 @@ public class ContextAlbumImplTest { otherAlbum = new ContextAlbumImpl(otherAxContextAlbum, distributor, new LinkedHashMap<String, Object>()); assertNotEquals(album, otherAlbum); - try { - album.flush(); - fail("test should throw an exception here"); - } catch (ContextException ce) { - assertEquals("map flush failed, supplied map is null", ce.getMessage()); - } - + assertThatThrownBy(album::flush) + .hasMessage("map flush failed, supplied map is null"); AxContextAlbums albums = new AxContextAlbums(); ModelService.registerModel(AxContextAlbums.class, albums); albums.getAlbumsMap().put(axContextAlbum.getKey(), axContextAlbum); |