5bbaf97fa44a4c50042f5ff354550dd9337103de
[policy/apex-pdp.git] /
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2016-2018 Ericsson. All rights reserved.
4  *  Modifications Copyright (C) 2020 Nordix Foundation
5  * ================================================================================
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.apex.plugins.context.schema.avro;
23
24 import static org.assertj.core.api.Assertions.assertThatThrownBy;
25
26 import org.junit.After;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.onap.policy.apex.context.impl.schema.SchemaHelperFactory;
30 import org.onap.policy.apex.context.parameters.ContextParameterConstants;
31 import org.onap.policy.apex.context.parameters.SchemaParameters;
32 import org.onap.policy.apex.model.basicmodel.concepts.AxArtifactKey;
33 import org.onap.policy.apex.model.basicmodel.concepts.AxKey;
34 import org.onap.policy.apex.model.basicmodel.service.ModelService;
35 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchema;
36 import org.onap.policy.apex.model.contextmodel.concepts.AxContextSchemas;
37 import org.onap.policy.common.parameters.ParameterService;
38
39 /**
40  * The Class TestAvroSchemaHelperBadSchemas.
41  *
42  * @author Liam Fallon (liam.fallon@ericsson.com)
43  * @version 
44  */
45 public class AvroSchemaHelperBadSchemasTest {
46     private final AxKey testKey = new AxArtifactKey("AvroTest", "0.0.1");
47     private AxContextSchemas schemas;
48
49     /**
50      * Inits the test.
51      */
52     @Before
53     public void initTest() {
54         schemas = new AxContextSchemas(new AxArtifactKey("AvroSchemas", "0.0.1"));
55         ModelService.registerModel(AxContextSchemas.class, schemas);
56     }
57
58     /**
59      * Inits the context.
60      */
61     @Before
62     public void initContext() {
63         SchemaParameters schemaParameters = new SchemaParameters();
64         schemaParameters.setName(ContextParameterConstants.SCHEMA_GROUP_NAME);
65         schemaParameters.getSchemaHelperParameterMap().put("AVRO", new AvroSchemaHelperParameters());
66         ParameterService.register(schemaParameters);
67         
68     }
69
70     /**
71      * Clear context.
72      */
73     @After
74     public void clearContext() {
75         ParameterService.deregister(ContextParameterConstants.SCHEMA_GROUP_NAME);
76     }
77
78     /**
79      * Bad schema test.
80      */
81     @Test
82     public void badSchemaTest() {
83         final AxContextSchema avroBadSchema0 = new AxContextSchema(new AxArtifactKey("AvroBad0", "0.0.1"), "AVRO", "}");
84         schemas.getSchemasMap().put(avroBadSchema0.getKey(), avroBadSchema0);
85
86         assertThatThrownBy(() -> new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema0.getKey()))
87             .hasMessageStartingWith("AvroTest:0.0.1: avro context schema \"AvroBad0:0.0.1\" schema is invalid");
88         final AxContextSchema avroBadSchema1 = new AxContextSchema(new AxArtifactKey("AvroBad1", "0.0.1"), "AVRO", "");
89         schemas.getSchemasMap().put(avroBadSchema1.getKey(), avroBadSchema1);
90
91         assertThatThrownBy(() -> new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema1.getKey()))
92             .hasMessageStartingWith("AvroTest:0.0.1: avro context schema \"AvroBad1:0.0.1\" schema is invalid");
93         final AxContextSchema avroBadSchema2 =
94                 new AxContextSchema(new AxArtifactKey("AvroBad2", "0.0.1"), "AVRO", "{}");
95         schemas.getSchemasMap().put(avroBadSchema2.getKey(), avroBadSchema2);
96
97         assertThatThrownBy(() -> new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema2.getKey()))
98             .hasMessageStartingWith("AvroTest:0.0.1: avro context schema \"AvroBad2:0.0.1\" schema is invalid");
99         final AxContextSchema avroBadSchema3 =
100                 new AxContextSchema(new AxArtifactKey("AvroBad3", "0.0.1"), "AVRO", "{zooby}");
101         schemas.getSchemasMap().put(avroBadSchema3.getKey(), avroBadSchema3);
102
103         assertThatThrownBy(() -> new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema3.getKey()))
104             .hasMessageStartingWith("AvroTest:0.0.1: avro context schema \"AvroBad3:0.0.1\" schema is invalid");
105         final AxContextSchema avroBadSchema4 =
106                 new AxContextSchema(new AxArtifactKey("AvroBad4", "0.0.1"), "AVRO", "{\"zooby\"}");
107         schemas.getSchemasMap().put(avroBadSchema4.getKey(), avroBadSchema4);
108
109         assertThatThrownBy(() -> new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema4.getKey()))
110             .hasMessageStartingWith("AvroTest:0.0.1: avro context schema \"AvroBad4:0.0.1\" schema is invalid");
111         final AxContextSchema avroBadSchema5 =
112                 new AxContextSchema(new AxArtifactKey("AvroBad5", "0.0.1"), "AVRO", "{\"type\": \"zooby\"}");
113         schemas.getSchemasMap().put(avroBadSchema5.getKey(), avroBadSchema5);
114
115         assertThatThrownBy(() -> new SchemaHelperFactory().createSchemaHelper(testKey, avroBadSchema5.getKey()))
116             .hasMessageStartingWith("AvroTest:0.0.1: avro context schema \"AvroBad5:0.0.1\" schema is invalid");
117     }
118 }