From 3684fb2d12cab2cd8ba784995e1ab6aaaa985266 Mon Sep 17 00:00:00 2001 From: Magdalena Jedraszak Date: Tue, 12 Dec 2023 22:12:05 +0100 Subject: [PATCH] LPS-201793 Integration tests --- .../v1_0/test/ObjectEntryResourceTest.java | 219 ++++++++++++++++++ 1 file changed, 219 insertions(+) diff --git a/modules/apps/object/object-rest-test/src/testIntegration/java/com/liferay/object/rest/internal/resource/v1_0/test/ObjectEntryResourceTest.java b/modules/apps/object/object-rest-test/src/testIntegration/java/com/liferay/object/rest/internal/resource/v1_0/test/ObjectEntryResourceTest.java index 3d9e865f179084..f1953df9b5751f 100644 --- a/modules/apps/object/object-rest-test/src/testIntegration/java/com/liferay/object/rest/internal/resource/v1_0/test/ObjectEntryResourceTest.java +++ b/modules/apps/object/object-rest-test/src/testIntegration/java/com/liferay/object/rest/internal/resource/v1_0/test/ObjectEntryResourceTest.java @@ -406,6 +406,16 @@ public void setUp() throws Exception { _siteScopedObjectDefinition1, _OBJECT_FIELD_NAME_1, _OBJECT_FIELD_VALUE_1); + _siteScopedObjectDefinition2 = + ObjectDefinitionTestUtil.publishObjectDefinition( + Arrays.asList( + ObjectFieldUtil.createObjectField( + ObjectFieldConstants.BUSINESS_TYPE_TEXT, + ObjectFieldConstants.DB_TYPE_STRING, true, true, null, + RandomTestUtil.randomString(), _OBJECT_FIELD_NAME_2, + false)), + ObjectDefinitionConstants.SCOPE_SITE); + SystemObjectDefinitionManager systemObjectDefinitionManager = _systemObjectDefinitionManagerRegistry. getSystemObjectDefinitionManager("User"); @@ -454,6 +464,16 @@ public void tearDown() throws Exception { _objectRelationship5); } + if (_objectRelationship6 != null) { + _objectRelationshipLocalService.deleteObjectRelationship( + _objectRelationship6); + } + + if (_objectRelationship7 != null) { + _objectRelationshipLocalService.deleteObjectRelationship( + _objectRelationship7); + } + _objectDefinitionLocalService.deleteObjectDefinition( _objectDefinition1); _objectDefinitionLocalService.deleteObjectDefinition( @@ -464,6 +484,8 @@ public void tearDown() throws Exception { _objectDefinition4); _objectDefinitionLocalService.deleteObjectDefinition( _siteScopedObjectDefinition1); + _objectDefinitionLocalService.deleteObjectDefinition( + _siteScopedObjectDefinition2); _listTypeDefinitionLocalService.deleteListTypeDefinition( _listTypeDefinition); @@ -4829,6 +4851,8 @@ public void testPostCustomObjectEntryWithInvalidNestedCustomObjectEntries() public void testPostCustomObjectEntryWithNestedCustomObjectEntriesInManyToManyRelationship() throws Exception { + // Company scope + _objectRelationship1 = ObjectRelationshipTestUtil.addObjectRelationship( _objectDefinition1, _objectDefinition2, TestPropsValues.getUserId(), ObjectRelationshipConstants.TYPE_MANY_TO_MANY); @@ -4880,6 +4904,69 @@ public void testPostCustomObjectEntryWithNestedCustomObjectEntriesInManyToManyRe Assert.assertEquals(2, nestedObjectEntriesJSONArray.length()); + _assertObjectEntryField( + (JSONObject)nestedObjectEntriesJSONArray.get(0), + _OBJECT_FIELD_NAME_2, _NEW_OBJECT_FIELD_VALUE_1); + _assertObjectEntryField( + (JSONObject)nestedObjectEntriesJSONArray.get(1), + _OBJECT_FIELD_NAME_2, _NEW_OBJECT_FIELD_VALUE_2); + + // Site scope + + _objectRelationship6 = ObjectRelationshipTestUtil.addObjectRelationship( + _siteScopedObjectDefinition1, _siteScopedObjectDefinition2, + TestPropsValues.getUserId(), + ObjectRelationshipConstants.TYPE_MANY_TO_MANY); + + objectEntryJSONObject = JSONUtil.put( + _objectRelationship6.getName(), + _createObjectEntriesJSONArray( + new String[] {_ERC_VALUE_1, _ERC_VALUE_2}, _OBJECT_FIELD_NAME_2, + new String[] { + _NEW_OBJECT_FIELD_VALUE_1, _NEW_OBJECT_FIELD_VALUE_2 + })); + + jsonObject = HTTPTestUtil.invokeToJSONObject( + objectEntryJSONObject.toString(), + _getEndpoint( + TestPropsValues.getGroupId(), _siteScopedObjectDefinition1), + Http.Method.POST); + + Assert.assertEquals( + 0, + jsonObject.getJSONObject( + "status" + ).get( + "code" + )); + + nestedObjectEntriesJSONArray = jsonObject.getJSONArray( + _objectRelationship6.getName()); + + Assert.assertEquals(2, nestedObjectEntriesJSONArray.length()); + + _assertObjectEntryField( + (JSONObject)nestedObjectEntriesJSONArray.get(0), + _OBJECT_FIELD_NAME_2, _NEW_OBJECT_FIELD_VALUE_1); + _assertObjectEntryField( + (JSONObject)nestedObjectEntriesJSONArray.get(1), + _OBJECT_FIELD_NAME_2, _NEW_OBJECT_FIELD_VALUE_2); + + objectEntryId = jsonObject.getString("id"); + + jsonObject = HTTPTestUtil.invokeToJSONObject( + null, + StringBundler.concat( + _siteScopedObjectDefinition1.getRESTContextPath(), + StringPool.SLASH, objectEntryId, "?nestedFields=", + _objectRelationship6.getName()), + Http.Method.GET); + + nestedObjectEntriesJSONArray = jsonObject.getJSONArray( + _objectRelationship6.getName()); + + Assert.assertEquals(2, nestedObjectEntriesJSONArray.length()); + _assertObjectEntryField( (JSONObject)nestedObjectEntriesJSONArray.get(0), _OBJECT_FIELD_NAME_2, _NEW_OBJECT_FIELD_VALUE_1); @@ -4892,6 +4979,8 @@ public void testPostCustomObjectEntryWithNestedCustomObjectEntriesInManyToManyRe public void testPostCustomObjectEntryWithNestedCustomObjectEntriesInManyToOneRelationship() throws Exception { + // Company scope + _objectRelationship1 = ObjectRelationshipTestUtil.addObjectRelationship( _objectDefinition1, _objectDefinition2, TestPropsValues.getUserId(), ObjectRelationshipConstants.TYPE_ONE_TO_MANY); @@ -4945,12 +5034,76 @@ public void testPostCustomObjectEntryWithNestedCustomObjectEntriesInManyToOneRel StringUtil.removeLast( _objectDefinition1.getPKObjectFieldName(), "Id"))), _OBJECT_FIELD_NAME_1, _NEW_OBJECT_FIELD_VALUE_1); + + // Site scope + + _objectRelationship7 = ObjectRelationshipTestUtil.addObjectRelationship( + _siteScopedObjectDefinition1, _siteScopedObjectDefinition2, + TestPropsValues.getUserId(), + ObjectRelationshipConstants.TYPE_ONE_TO_MANY); + + objectEntryJSONObject = JSONUtil.put( + _objectRelationship7.getName(), + JSONFactoryUtil.createJSONObject( + JSONUtil.put( + _OBJECT_FIELD_NAME_1, _NEW_OBJECT_FIELD_VALUE_1 + ).put( + "externalReferenceCode", _ERC_VALUE_1 + ).toString())); + + jsonObject = HTTPTestUtil.invokeToJSONObject( + objectEntryJSONObject.toString(), + _getEndpoint( + TestPropsValues.getGroupId(), _siteScopedObjectDefinition2), + Http.Method.POST); + + Assert.assertEquals( + 0, + jsonObject.getJSONObject( + "status" + ).get( + "code" + )); + + _assertObjectEntryField( + jsonObject.getJSONObject( + StringBundler.concat( + "r_", _objectRelationship7.getName(), "_", + StringUtil.replaceLast( + _siteScopedObjectDefinition1.getPKObjectFieldName(), + "Id", ""))), + _OBJECT_FIELD_NAME_1, _NEW_OBJECT_FIELD_VALUE_1); + + objectEntryId = jsonObject.getString("id"); + + jsonObject = HTTPTestUtil.invokeToJSONObject( + null, + StringBundler.concat( + _siteScopedObjectDefinition2.getRESTContextPath(), + StringPool.SLASH, objectEntryId, "?nestedFields=", + StringBundler.concat( + "r_", _objectRelationship7.getName(), "_", + StringUtil.removeLast( + _siteScopedObjectDefinition1.getPKObjectFieldName(), + "Id"))), + Http.Method.GET); + + _assertObjectEntryField( + jsonObject.getJSONObject( + StringBundler.concat( + "r_", _objectRelationship7.getName(), "_", + StringUtil.removeLast( + _siteScopedObjectDefinition1.getPKObjectFieldName(), + "Id"))), + _OBJECT_FIELD_NAME_1, _NEW_OBJECT_FIELD_VALUE_1); } @Test public void testPostCustomObjectEntryWithNestedCustomObjectEntriesInOneToManyRelationship() throws Exception { + // Company scope + _objectRelationship1 = ObjectRelationshipTestUtil.addObjectRelationship( _objectDefinition1, _objectDefinition2, TestPropsValues.getUserId(), ObjectRelationshipConstants.TYPE_ONE_TO_MANY); @@ -5002,6 +5155,69 @@ public void testPostCustomObjectEntryWithNestedCustomObjectEntriesInOneToManyRel Assert.assertEquals(2, nestedObjectEntriesJSONArray.length()); + _assertObjectEntryField( + (JSONObject)nestedObjectEntriesJSONArray.get(0), + _OBJECT_FIELD_NAME_2, _NEW_OBJECT_FIELD_VALUE_1); + _assertObjectEntryField( + (JSONObject)nestedObjectEntriesJSONArray.get(1), + _OBJECT_FIELD_NAME_2, _NEW_OBJECT_FIELD_VALUE_2); + + // Site scope + + _objectRelationship7 = ObjectRelationshipTestUtil.addObjectRelationship( + _siteScopedObjectDefinition1, _siteScopedObjectDefinition2, + TestPropsValues.getUserId(), + ObjectRelationshipConstants.TYPE_ONE_TO_MANY); + + objectEntryJSONObject = JSONUtil.put( + _objectRelationship7.getName(), + _createObjectEntriesJSONArray( + new String[] {_ERC_VALUE_1, _ERC_VALUE_2}, _OBJECT_FIELD_NAME_2, + new String[] { + _NEW_OBJECT_FIELD_VALUE_1, _NEW_OBJECT_FIELD_VALUE_2 + })); + + jsonObject = HTTPTestUtil.invokeToJSONObject( + objectEntryJSONObject.toString(), + _getEndpoint( + TestPropsValues.getGroupId(), _siteScopedObjectDefinition1), + Http.Method.POST); + + Assert.assertEquals( + 0, + jsonObject.getJSONObject( + "status" + ).get( + "code" + )); + + nestedObjectEntriesJSONArray = jsonObject.getJSONArray( + _objectRelationship7.getName()); + + Assert.assertEquals(2, nestedObjectEntriesJSONArray.length()); + + _assertObjectEntryField( + (JSONObject)nestedObjectEntriesJSONArray.get(0), + _OBJECT_FIELD_NAME_2, _NEW_OBJECT_FIELD_VALUE_1); + _assertObjectEntryField( + (JSONObject)nestedObjectEntriesJSONArray.get(1), + _OBJECT_FIELD_NAME_2, _NEW_OBJECT_FIELD_VALUE_2); + + objectEntryId = jsonObject.getString("id"); + + jsonObject = HTTPTestUtil.invokeToJSONObject( + null, + StringBundler.concat( + _siteScopedObjectDefinition1.getRESTContextPath(), + StringPool.SLASH, objectEntryId, "?nestedFields=", + _objectRelationship7.getName()), + Http.Method.GET); + + nestedObjectEntriesJSONArray = jsonObject.getJSONArray( + _objectRelationship7.getName()); + + Assert.assertEquals(2, nestedObjectEntriesJSONArray.length()); + _assertObjectEntryField( (JSONObject)nestedObjectEntriesJSONArray.get(0), _OBJECT_FIELD_NAME_2, _NEW_OBJECT_FIELD_VALUE_1); @@ -7822,6 +8038,8 @@ private JSONObject _toFileEntryJSONObject( private ObjectRelationship _objectRelationship3; private ObjectRelationship _objectRelationship4; private ObjectRelationship _objectRelationship5; + private ObjectRelationship _objectRelationship6; + private ObjectRelationship _objectRelationship7; @Inject private ObjectRelationshipLocalService _objectRelationshipLocalService; @@ -7839,6 +8057,7 @@ private JSONObject _toFileEntryJSONObject( private RoleLocalService _roleLocalService; private ObjectDefinition _siteScopedObjectDefinition1; + private ObjectDefinition _siteScopedObjectDefinition2; private ObjectEntry _siteScopedObjectEntry1; @Inject