Set Page Restrictions in Confluence macros

I create my macro and I try to set page permission.
During the macro insertion, the restrictions are set in the EDIT + VIEW (Why? I ONLY WANTED “VIEW”!!!), after the page is published I get an error.

Here is my simple code in the execute() method:

ContentEntityObject contentEntityObject = conversionContext.getEntity();
ContentPermission permission = ContentPermission
                .createGroupPermission(ContentPermission.VIEW_PERMISSION, "testspace_k3");
        contentPermissionManager.addContentPermission(permission, pageManager.getPage(contentEntityObject.getId()));

There are entries in the log:

– url: /display/testspace/test | page: 3080260 | traceId: 68dac98d21017ece | userName: admin | referer: http://localhost:8090/pages/resumedraft.action?draftId=3080260&draftShareId=006aa5fe-759e-4152-9813-451632c3f721& | action: viewpage
->[null]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT (Session #1614232119)
2019-07-27 21:55:04,425 WARN [http-nio-8090-exec-6] [confluence.impl.hibernate.ConfluenceHibernateTransactionManager] doRollback Performing rollback. Transactions:
java.lang.NullPointerException
– url: /display/testspace/test | page: 3080260 | traceId: 68dac98d21017ece | userName: admin | referer: http://localhost:8090/pages/resumedraft.action?draftId=3080260&draftShareId=006aa5fe-759e-4152-9813-451632c3f721& | action: viewpage
2019-07-27 21:55:04,422 ERROR [http-nio-8090-exec-6] [xhtml.view.macro.ViewMacroMarshaller] handleMacroExecutionException Error rendering macro: Test Macro
– url: /display/testspace/test | page: 3080260 | traceId: 76be1a2499db2279 | userName: admin | referer: http://localhost:8090/pages/resumedraft.action?draftId=3080260&draftShareId=006aa5fe-759e-4152-9813-451632c3f721& | action: viewpage
->[null]: PROPAGATION_REQUIRED,ISOLATION_DEFAULT (Session #1309099359)
2019-07-27 21:55:03,710 WARN [http-nio-8090-exec-4] [confluence.impl.hibernate.ConfluenceHibernateTransactionManager] doRollback Performing rollback. Transactions:
java.lang.NullPointerException
– url: /display/testspace/test | page: 3080260 | traceId: 76be1a2499db2279 | userName: admin | referer: http://localhost:8090/pages/resumedraft.action?draftId=3080260&draftShareId=006aa5fe-759e-4152-9813-451632c3f721& | action: viewpage
2019-07-27 21:55:03,707 ERROR [http-nio-8090-exec-4] [xhtml.view.macro.ViewMacroMarshaller] handleMacroExecutionException Error rendering macro: Test Macro

it doesn’t work either:

activeObjects.executeInTransaction(() -> {
            ContentPermission permission = ContentPermission
                    .createGroupPermission(ContentPermission.VIEW_PERMISSION, "testspace_k3");
            contentPermissionManager.addContentPermission(permission, pageManager.getPage(contentEntityObject.getId()));
            return null;

        });

What is the correct way to set page restrictions programmatically?